From 9fd696036b6c53f8b440c2a21f64c3178992b139 Mon Sep 17 00:00:00 2001
From: zomars <zomars@me.com>
Date: Fri, 25 Feb 2022 12:12:41 -0700
Subject: [PATCH] Adds support for multiple submodules

---
 vercel-submodule-workaround.sh | 62 +++++++++++++++++++---------------
 1 file changed, 34 insertions(+), 28 deletions(-)

diff --git a/vercel-submodule-workaround.sh b/vercel-submodule-workaround.sh
index d0a3939..b6d9297 100755
--- a/vercel-submodule-workaround.sh
+++ b/vercel-submodule-workaround.sh
@@ -1,14 +1,13 @@
-# github submodule repo address without https:// prefix
-SUBMODULE_GITHUB=github.com/beeinger/vercel-private-submodule
-
-# .gitmodules submodule path
-SUBMODULE_PATH=library
+# github submodule repo addresses without https:// prefix
+declare -A remotes=(
+    ["library"]="github.com/beeinger/vercel-private-submodule"
+)
 
 # github access token is necessary
 # add it to Environment Variables on Vercel
 if [ "$GITHUB_ACCESS_TOKEN" == "" ]; then
-  echo "Error: GITHUB_ACCESS_TOKEN is empty"
-  exit 1
+    echo "Error: GITHUB_ACCESS_TOKEN is empty"
+    exit 1
 fi
 
 # stop execution on error - don't let it build if something goes wrong
@@ -16,24 +15,31 @@ set -e
 
 # get submodule commit
 output=`git submodule status --recursive` # get submodule info
-no_prefix=${output#*-} # get rid of the prefix
-COMMIT=${no_prefix% *} # get rid of the suffix
-
-# set up an empty temporary work directory
-rm -rf tmp || true # remove the tmp folder if exists
-mkdir tmp # create the tmp folder
-cd tmp # go into the tmp folder
-
-# checkout the current submodule commit
-git init # initialise empty repo
-git remote add origin https://$GITHUB_ACCESS_TOKEN@$SUBMODULE_GITHUB # add origin of the submodule
-git fetch --depth=1 origin $COMMIT # fetch only the required version
-git checkout $COMMIT # checkout on the right commit
-
-# move the submodule from tmp to the submodule path
-cd .. # go folder up
-rm -rf tmp/.git # remove .git 
-mv tmp/* $SUBMODULE_PATH/ # move the submodule to the submodule path
-
-# clean up
-rm -rf tmp # remove the tmp folder
\ No newline at end of file
+
+# Extract each submodule commit hash and path
+submodules=$(echo $output | sed "s/ -/__/g" | sed "s/ /=/g" | sed "s/-//g" | tr "__" "\n")
+
+for submodule in $submodules; do
+    IFS="=" read COMMIT SUBMODULE_PATH <<<"$submodule"
+
+    SUBMODULE_GITHUB=$remotes[$SUBMODULE_PATH]
+    
+    # set up an empty temporary work directory
+    rm -rf tmp || true # remove the tmp folder if exists
+    mkdir tmp          # create the tmp folder
+    cd tmp             # go into the tmp folder
+
+    # checkout the current submodule commit
+    git init                                                                      # initialise empty repo
+    git remote add $SUBMODULE_PATH https://$GITHUB_ACCESS_TOKEN@$SUBMODULE_GITHUB # add origin of the submodule
+    git fetch --depth=1 $SUBMODULE_PATH $COMMIT                                   # fetch only the required version
+    git checkout $COMMIT                                                          # checkout on the right commit
+
+    # move the submodule from tmp to the submodule path
+    cd ..                     # go folder up
+    rm -rf tmp/.git           # remove .git
+    mv tmp/* $SUBMODULE_PATH/ # move the submodule to the submodule path
+
+    # clean up
+    rm -rf tmp # remove the tmp folder
+done