Added support for creating all repositories on Linux/Uni*.
[jprojects-scripts.git] / fix-project-remotes.sh
index 6fff377..f4bd38b 100755 (executable)
@@ -5,6 +5,7 @@
 UNWANTED_NAME="stick"
 WANTED_NAME1="upstream"
 WANTED_NAME2="daedalus"
+WANTED_NAME3="transport"
 
 GIT_USER="quix0r"
 GIT_SERVER="git.mxchange.org"
@@ -12,29 +13,53 @@ GIT_PATH="/var/cache/git/repos"
 GIT_BRANCH="master"
 GIT_ORIGIN="origin"
 
-MEDIA_NAME="INTENSO"
-MEDIA_PATH="Java Project"
 MEDIA_BASE="/media"
+MEDIA_NAME1="INTENSO"
+MEDIA_NAME2="Transport"
+MEDIA_PATH="Java Project"
 
 for project in ${LIST}; do
+       if [ ! -d "${JPROJECTS_HOME}/${project}" ]
+       then
+               continue
+       fi
+
+       echo "$0: project='${project}'"
+
        GIT_URL1="${GIT_USER}@${GIT_SERVER}:${GIT_PATH}/${project}.git"
        GIT_URL2="${GIT_PATH}/${project}.git"
-       echo "$0: GIT_URL1='${GIT_URL1}',GIT_URL2='${GIT_URL2}'"
+       GIT_URL3="${MEDIA_BASE}/${USER}/${MEDIA_NAME2}/${MEDIA_PATH}/${project}.git"
+       GIT_URL4="file:///E:/Java%20Project/${project}.git"
+       GIT_URL5="E:/Java Project/${project}.git"
+       GIT_URL6="${MEDIA_BASE}/${USER}/${MEDIA_NAME1}/${MEDIA_PATH}/${project}.git"
+       echo "$0: GIT_URL1='${GIT_URL1}',GIT_URL2='${GIT_URL2}',GIT_URL3='${GIT_URL3}',GIT_URL4='${GIT_URL4}',GIT_URL5='${GIT_URL5}',GIT_URL6='${GIT_URL6}'"
 
-       cd "${project}"
+       cd "${JPROJECTS_HOME}/${project}" || exit 255
 
        if [ "$1" = "l" ]
        then
                echo "$0: Listing remotes for '${project}' ..."
                git remote -v
-               cd -
-               continue;
+               continue
        fi
 
        REMOTES=`git remote | wc -l`
        echo "$0: REMOTES='${REMOTES}'"
 
-       if [ "${REMOTES}" = "1" ]
+       if [ "${REMOTES}" = "0" ]
+       then
+               if [ -n "${SHELL}" -a -f "/proc/version" ]
+               then
+                       echo "$0: Found no repositories in '${project}'. Creating all (Linux) ..."
+                       git remote add "${GIT_ORIGIN}" "${GIT_URL6}" --track "${GIT_BRANCH}"
+                       git remote add "${WANTED_NAME1}" "${GIT_URL1}"
+                       git remote add "${WANTED_NAME2}" "${GIT_URL2}"
+                       git remote add "${WANTED_NAME3}" "${GIT_URL3}"
+               else
+                       echo "$0: Don't know how to create repositories, maybe unsupported."
+                       exit 1
+               fi
+       elif [ "${REMOTES}" = "1" ]
        then
                echo "$0: Only one remote found for '${project}'"
 
@@ -42,12 +67,12 @@ for project in ${LIST}; do
                REMOTE_PUSH=`git remote -v | grep "push"`
                echo "$0: REMOTE='${REMOTE}',REMOTE_PUSH='${REMOTE_PUSH}'"
 
-               MEDIA=`echo "${REMOTE_PUSH}" | grep "${MEDIA_BASE}/"`
-               echo "$0: MEDIA='${MEDIA}'"
+               MEDIA1=`echo "${REMOTE_PUSH}" | grep "${MEDIA_BASE}/${USER}/${MEDIA_NAME1}/"`
+               echo "$0: MEDIA1='${MEDIA1}'"
 
-               if [ -n "${MEDIA}" ]
+               if [ -n "${MEDIA1}" ]
                then
-                       echo "$0: Found ${MEDIA_BASE}/ remote for '${project}'."
+                       echo "$0: Found '${MEDIA_BASE}/${USER}/${MEDIA_NAME1}' remote for '${project}'."
                        if [ "${REMOTE}" = "${GIT_ORIGIN}" ]
                        then
                                echo "$0: Project '${project}' has already fixed '${GIT_ORIGIN}'."
@@ -58,13 +83,16 @@ for project in ${LIST}; do
                else
                        echo "$0: Not a media remote."
                        MXCHANGE_REMOTE=`echo "${REMOTE_PUSH}" | grep "${GIT_URL1}"`
-                       echo "$0: MXCHANGE_REMOTE='${MXCHANGE_REMOTE}'"
+                       LINUX_MEDIA_PATH_REMOTE=`echo "${REMOTE_PUSH}" | grep "${GIT_URL3}"`
+                       WINDOWS_FILE_REMOTE=`echo "${REMOTE_PUSH}" | grep "${GIT_URL4}"`
+                       WINDOWS_PATH_REMOTE=`echo "${REMOTE_PUSH}" | grep "${GIT_URL5}"`
+                       echo "$0: MXCHANGE_REMOTE='${MXCHANGE_REMOTE}',WINDOWS_FILE_REMOTE='${WINDOWS_FILE_REMOTE}',WINDOWS_PATH_REMOTE='${WINDOWS_PATH_REMOTE}',LINUX_MEDIA_PATH_REMOTE='${LINUX_MEDIA_PATH_REMOTE}'"
 
                        if [ -n "${MXCHANGE_REMOTE}" ]
                        then
                                echo "$0: Found a '${GIT_SERVER}' remote."
 
-                               if [ "${REMOTE}" != "${WANTED_NAME1}" ]
+                               if [ -n "${WANTED_NAME1}" -a "${REMOTE}" != "${WANTED_NAME1}" ]
                                then
                                        echo "$0: Project '${project}' has one correct '${WANTED_NAME1}'."
                                else
@@ -73,7 +101,36 @@ for project in ${LIST}; do
                                fi
 
                                echo "$0: Adding '${GIT_ORIGIN}' to '${project}' ..."
-                               git remote add "${GIT_ORIGIN}" "${MEDIA_BASE}/${USER}/${MEDIA_NAME}/${MEDIA_PATH}/${project}.git" --track "${GIT_BRANCH}" || exit 255
+                               git remote add "${GIT_ORIGIN}" "${MEDIA_BASE}/${USER}/${MEDIA_NAME1}/${MEDIA_PATH}/${project}.git" --track "${GIT_BRANCH}" || exit 255
+                       elif [ -n "${WINDOWS_FILE_REMOTE}" -o -n "${WINDOWS_PATH_REMOTE}" ]
+                       then
+                               echo "$0: Found a Windows (USB stick) remote."
+
+                               if [ -n "${WANTED_NAME3}" -a "${REMOTE}" == "${WANTED_NAME3}" ]
+                               then
+                                       echo "$0: Project '${project}' has one correct '${WANTED_NAME3}'."
+                               else
+                                       echo "$0: Renaming '${REMOTE}' to '${WANTED_NAME3}' ..."
+                                       git remote rename "${REMOTE}" "${WANTED_NAME3}" || exit 255
+                               fi
+
+                               echo "$0: Adding '${GIT_ORIGIN}' to '${project}' ..."
+                               git remote add "${GIT_ORIGIN}" "${GIT_URL1}" --track "${GIT_BRANCH}" || exit 255
+                       elif [ -n "${LINUX_MEDIA_PATH_REMOTE}" ]
+                       then
+                               echo "$0: Found a Linux (USB stick) remote."
+
+                               if [ -n "${GIT_ORIGIN}" -a "${REMOTE}" = "${GIT_ORIGIN}" ]
+                               then
+                                       echo "$0: Need to fix remote repositories ..."
+                                       git remote rename origin transport || exit 255
+                                       git remote add "${GIT_ORIGIN}" "${GIT_URL6}" --track "${GIT_BRANCH}" || exit 255
+                                       git remote add "${WANTED_NAME1}" "${GIT_URL1}" --track "${GIT_BRANCH}" || exit 255
+                                       git remote add "${WANTED_NAME2}" "${GIT_URL2}" --track "${GIT_BRANCH}" || exit 255
+                               else
+                                       echo "$0: Don't know how to fix '${REMOTE}' (${REMOTE_PUSH})."
+                                       exit 1
+                               fi
                        else
                                echo "$0: Unknown remote '${REMOTE_PUSH}' found. Exiting ..."
                                exit 1
@@ -86,24 +143,25 @@ for project in ${LIST}; do
 
        if [ -n "${UNWANTED_REMOTE}" ]
        then
-               echo "$0: Found remote '${UNWANTED_NAME1}' in '${project}'. Renaming '${GIT_ORIGIN}'->'${WANTED_NAME1}' and '${UNWANTED_NAME1}'->'${GIT_ORIGIN}' ..."
+               echo "$0: Found remote '${UNWANTED_NAME}' in '${project}'. Renaming '${GIT_ORIGIN}'->'${WANTED_NAME1}' and '${UNWANTED_NAME}'->'${GIT_ORIGIN}' ..."
 
                UPSTREAM_REMOTE=`git remote | grep "${WANTED_NAME1}"`
                echo "$0: UPSTREAM_REMOTE='${UPSTREAM_REMOTE}'"
 
                if [ -n "${UPSTREAM_REMOTE}" ]
                then
-                       echo "$0: Remotes '${UNWANTED_NAME1}' and '${WANTED_NAME1}' exist! Removing '${WANTED_NAME1}' ..."
+                       echo "$0: Remotes '${UNWANTED_NAME}' and '${WANTED_NAME1}' exist! Removing '${WANTED_NAME1}' ..."
                        git remote rm "${WANTED_NAME1}" || exit 255
                fi
 
                git remote rename "${GIT_ORIGIN}" "${WANTED_NAME1}" || exit 255
-               git remote rename "${UNWANTED_NAME1}" "${GIT_ORIGIN}" || exit 255
+               git remote rename "${UNWANTED_NAME}" "${GIT_ORIGIN}" || exit 255
        else
-               echo "$0: No unwanted '${UNWANTED_NAME1}' remote in '${project}' ..."
+               echo "$0: No unwanted '${UNWANTED_NAME}' remote in '${project}' ..."
                UPSTREAM_REMOTE1=`git remote | grep "${WANTED_NAME1}"`
                UPSTREAM_REMOTE2=`git remote | grep "${WANTED_NAME2}"`
-               echo "$0: UPSTREAM_REMOTE1='${UPSTREAM_REMOTE1}',UPSTREAM_REMOTE2='${UPSTREAM_REMOTE2}'"
+               UPSTREAM_REMOTE3=`git remote | grep "${WANTED_NAME3}"`
+               echo "$0: UPSTREAM_REMOTE1='${UPSTREAM_REMOTE1}',UPSTREAM_REMOTE2='${UPSTREAM_REMOTE2}',UPSTREAM_REMOTE3='${UPSTREAM_REMOTE3}'"
 
                if [ -n "${UPSTREAM_REMOTE1}" ]
                then
@@ -116,11 +174,27 @@ for project in ${LIST}; do
                if [ -n "${UPSTREAM_REMOTE2}" ]
                then
                        echo "$0: Remote '${WANTED_NAME2}' found in '${project}'."
-               else
+               elif [ ! -d "${GIT_URL2}" ]
+               then
+                       echo "$0: Remote '${WANTED_NAME2}' cannot be created, path '${GIT_URL2}' missing."
+               elif [ -n "${WANTED_NAME2}" ]
+               then
                        echo "$0: No wanted '${WANTED_NAME2}' in '${project}'. Adding ..."
                        git remote add "${WANTED_NAME2}" "${GIT_URL2}" --track "${GIT_BRANCH}" || exit 255
                fi
-       fi
 
-       cd -
+               if [ -n "${UPSTREAM_REMOTE3}" ]
+               then
+                       echo "$0: Remote '${WANTED_NAME3}' found in '${project}'."
+               elif [ ! -d "${GIT_URL3}" ]
+               then
+                       echo "$0: Remote '${WANTED_NAME3}' cannot be created, path '${GIT_URL3}' missing."
+               else
+                       echo "$0: No wanted '${WANTED_NAME3}' in '${project}'. Adding ..."
+                       git remote add "${WANTED_NAME3}" "${GIT_URL3}" --track "${GIT_BRANCH}" || exit 255
+               fi
+       fi
 done
+
+echo "$0: All done."
+exit 0