More stuff added for Windows development (silly)
[jprojects-scripts.git] / fix-project-remotes.sh
1 #!/bin/bash
2
3 . ./.jprojects.sh || exit 255
4
5 UNWANTED_NAME="stick"
6 WANTED_NAME1="upstream"
7 WANTED_NAME2="daedalus"
8 WANTED_NAME3="transport"
9
10 GIT_USER="quix0r"
11 GIT_SERVER="git.mxchange.org"
12 GIT_PATH="/var/cache/git/repos"
13 GIT_BRANCH="master"
14 GIT_ORIGIN="origin"
15
16 MEDIA_BASE="/media"
17 MEDIA_NAME1="INTENSO"
18 MEDIA_NAME2="Transport"
19 MEDIA_PATH="Java Project"
20
21 for project in ${LIST}; do
22         if [ ! -d "${JPROJECTS_HOME}/${project}" ]
23         then
24                 continue
25         fi
26
27         echo "$0: project='${project}'"
28
29         GIT_URL1="${GIT_USER}@${GIT_SERVER}:${GIT_PATH}/${project}.git"
30         GIT_URL2="${GIT_PATH}/${project}.git"
31         GIT_URL3="${MEDIA_BASE}/${USER}/${MEDIA_NAME2}/${MEDIA_PATH}/${project}.git"
32         GIT_URL4="file:///E:/Java%20Project/${project}.git"
33         GIT_URL5="E:/Java Project/${project}.git"
34         echo "$0: GIT_URL1='${GIT_URL1}',GIT_URL2='${GIT_URL2}',GIT_URL3='${GIT_URL3}',GIT_URL4='${GIT_URL4}',GIT_URL5='${GIT_URL5}'"
35
36         cd "${JPROJECTS_HOME}/${project}" || exit 255
37
38         if [ "$1" = "l" ]
39         then
40                 echo "$0: Listing remotes for '${project}' ..."
41                 git remote -v
42                 continue
43         fi
44
45         REMOTES=`git remote | wc -l`
46         echo "$0: REMOTES='${REMOTES}'"
47
48         if [ "${REMOTES}" = "1" ]
49         then
50                 echo "$0: Only one remote found for '${project}'"
51
52                 REMOTE=`git remote`
53                 REMOTE_PUSH=`git remote -v | grep "push"`
54                 echo "$0: REMOTE='${REMOTE}',REMOTE_PUSH='${REMOTE_PUSH}'"
55
56                 MEDIA1=`echo "${REMOTE_PUSH}" | grep "${MEDIA_BASE}/${USER}/${MEDIA_NAME1}/"`
57                 echo "$0: MEDIA1='${MEDIA1}'"
58
59                 if [ -n "${MEDIA1}" ]
60                 then
61                         echo "$0: Found '${MEDIA_BASE}/${USER}/${MEDIA_NAME1}' remote for '${project}'."
62                         if [ "${REMOTE}" = "${GIT_ORIGIN}" ]
63                         then
64                                 echo "$0: Project '${project}' has already fixed '${GIT_ORIGIN}'."
65                         else
66                                 echo "$0: Renaming '${REMOTE}' to '${GIT_ORIGIN}' ..."
67                                 git remote rename "${REMOTE}" "${GIT_ORIGIN}" || exit 255
68                         fi
69                 else
70                         echo "$0: Not a media remote."
71                         MXCHANGE_REMOTE=`echo "${REMOTE_PUSH}" | grep "${GIT_URL1}"`
72                         WINDOWS_FILE_REMOTE=`echo "${REMOTE_PUSH}" | grep "${GIT_URL4}"`
73                         WINDOWS_PATH_REMOTE=`echo "${REMOTE_PUSH}" | grep "${GIT_URL5}"`
74                         echo "$0: MXCHANGE_REMOTE='${MXCHANGE_REMOTE}',WINDOWS_FILE_REMOTE='${WINDOWS_FILE_REMOTE}',WINDOWS_PATH_REMOTE='${WINDOWS_PATH_REMOTE}'"
75
76                         if [ -n "${MXCHANGE_REMOTE}" ]
77                         then
78                                 echo "$0: Found a '${GIT_SERVER}' remote."
79
80                                 if [ -n "${WANTED_NAME1}" -a "${REMOTE}" != "${WANTED_NAME1}" ]
81                                 then
82                                         echo "$0: Project '${project}' has one correct '${WANTED_NAME1}'."
83                                 else
84                                         echo "$0: Renaming '${REMOTE}' to '${WANTED_NAME1}' ..."
85                                         git remote rename "${REMOTE}" "${WANTED_NAME1}" || exit 255
86                                 fi
87
88                                 echo "$0: Adding '${GIT_ORIGIN}' to '${project}' ..."
89                                 git remote add "${GIT_ORIGIN}" "${MEDIA_BASE}/${USER}/${MEDIA_NAME1}/${MEDIA_PATH}/${project}.git" --track "${GIT_BRANCH}" || exit 255
90                         elif [ -n "${WINDOWS_FILE_REMOTE}" -o -n "${WINDOWS_PATH_REMOTE}" ]
91                         then
92                                 echo "$0: Found a Windows (USB stick) remote."
93
94                                 if [ -n "${WANTED_NAME3}" -a "${REMOTE}" == "${WANTED_NAME3}" ]
95                                 then
96                                         echo "$0: Project '${project}' has one correct '${WANTED_NAME3}'."
97                                 else
98                                         echo "$0: Renaming '${REMOTE}' to '${WANTED_NAME3}' ..."
99                                         git remote rename "${REMOTE}" "${WANTED_NAME3}" || exit 255
100                                 fi
101
102                                 echo "$0: Adding '${GIT_ORIGIN}' to '${project}' ..."
103                                 git remote add "${GIT_ORIGIN}" "${GIT_URL1}" --track "${GIT_BRANCH}" || exit 255
104                         else
105                                 echo "$0: Unknown remote '${REMOTE_PUSH}' found. Exiting ..."
106                                 exit 1
107                         fi
108                 fi
109         fi
110
111         UNWANTED_REMOTE=`git remote | grep "${UNWANTED_NAME}"`
112         echo "$0: UNWANTED_REMOTE='${UNWANTED_REMOTE}'"
113
114         if [ -n "${UNWANTED_REMOTE}" ]
115         then
116                 echo "$0: Found remote '${UNWANTED_NAME}' in '${project}'. Renaming '${GIT_ORIGIN}'->'${WANTED_NAME1}' and '${UNWANTED_NAME}'->'${GIT_ORIGIN}' ..."
117
118                 UPSTREAM_REMOTE=`git remote | grep "${WANTED_NAME1}"`
119                 echo "$0: UPSTREAM_REMOTE='${UPSTREAM_REMOTE}'"
120
121                 if [ -n "${UPSTREAM_REMOTE}" ]
122                 then
123                         echo "$0: Remotes '${UNWANTED_NAME}' and '${WANTED_NAME1}' exist! Removing '${WANTED_NAME1}' ..."
124                         git remote rm "${WANTED_NAME1}" || exit 255
125                 fi
126
127                 git remote rename "${GIT_ORIGIN}" "${WANTED_NAME1}" || exit 255
128                 git remote rename "${UNWANTED_NAME}" "${GIT_ORIGIN}" || exit 255
129         else
130                 echo "$0: No unwanted '${UNWANTED_NAME}' remote in '${project}' ..."
131                 UPSTREAM_REMOTE1=`git remote | grep "${WANTED_NAME1}"`
132                 UPSTREAM_REMOTE2=`git remote | grep "${WANTED_NAME2}"`
133                 UPSTREAM_REMOTE3=`git remote | grep "${WANTED_NAME3}"`
134                 echo "$0: UPSTREAM_REMOTE1='${UPSTREAM_REMOTE1}',UPSTREAM_REMOTE2='${UPSTREAM_REMOTE2}',UPSTREAM_REMOTE3='${UPSTREAM_REMOTE3}'"
135
136                 if [ -n "${UPSTREAM_REMOTE1}" ]
137                 then
138                         echo "$0: Remote '${WANTED_NAME1}' found in '${project}'."
139                 else
140                         echo "$0: No wanted '${WANTED_NAME1}' in '${project}'. Adding ..."
141                         git remote add "${WANTED_NAME1}" "${GIT_URL1}" --track "${GIT_BRANCH}" || exit 255
142                 fi
143
144                 if [ -n "${UPSTREAM_REMOTE2}" ]
145                 then
146                         echo "$0: Remote '${WANTED_NAME2}' found in '${project}'."
147                 elif [ ! -d "${GIT_URL2}" ]
148                 then
149                         echo "$0: Remote '${WANTED_NAME2}' cannot be created, path '${GIT_URL2}' missing."
150                 elif [ -n "${WANTED_NAME2}" ]
151                 then
152                         echo "$0: No wanted '${WANTED_NAME2}' in '${project}'. Adding ..."
153                         git remote add "${WANTED_NAME2}" "${GIT_URL2}" --track "${GIT_BRANCH}" || exit 255
154                 fi
155
156                 if [ -n "${UPSTREAM_REMOTE3}" ]
157                 then
158                         echo "$0: Remote '${WANTED_NAME3}' found in '${project}'."
159                 elif [ ! -d "${GIT_URL3}" ]
160                 then
161                         echo "$0: Remote '${WANTED_NAME3}' cannot be created, path '${GIT_URL3}' missing."
162                 else
163                         echo "$0: No wanted '${WANTED_NAME3}' in '${project}'. Adding ..."
164                         git remote add "${WANTED_NAME3}" "${GIT_URL3}" --track "${GIT_BRANCH}" || exit 255
165                 fi
166         fi
167 done
168
169 echo "$0: All done."
170 exit 0