allow packages to be provided locally or fetched from alternate branches

refs #1895

Change-Id: Idae33f3af5091413c915c5081d505bacea957c5d
diff --git a/install_helpers/common.sh b/install_helpers/common.sh
new file mode 100644
index 0000000..5a78668
--- /dev/null
+++ b/install_helpers/common.sh
@@ -0,0 +1,50 @@
+#!/bin/bash
+
+if [[ -f repos.conf ]]; then
+  source repos.conf
+fi
+
+# Check out the repository of the specified package from Gerrit or a local repository.
+#
+# Parameters:
+#   name of package used in repository names (e.g., ndn-cxx or NFD)
+#   name of package used in variable names (e.g., NDNCXX or NFD)
+#
+# This function changes the working directory to the directory containing the package source.
+prepare_repo() {
+  local PACKAGE_NAME=$1
+  local PACKAGE_VAR=PACKAGE_$2
+  local PACKAGE_SOURCE=${!PACKAGE_VAR}
+  if [[ -z $PACKAGE_SOURCE ]]; then
+    PACKAGE_SOURCE=branch:master
+  fi
+
+  local SOURCE=${PACKAGE_SOURCE%%:*}
+  local LOCATION=${PACKAGE_SOURCE#*:}
+  if [[ $SOURCE == branch || $SOURCE == gerrit ]]; then
+    if [[ $SOURCE == branch ]]; then
+      local GERRIT_BRANCH=$LOCATION
+    else
+      local GERRIT_CHANGE=${LOCATION%%,*}
+      local GERRIT_PATCHSET=${LOCATION#*,}
+      local GERRIT_BRANCH="refs/changes/$(printf '%02d' $(($GERRIT_CHANGE % 100)))/$GERRIT_CHANGE/$GERRIT_PATCHSET"
+    fi
+
+    echo "Checking out from Gerrit: $GERRIT_BRANCH"
+    cd prepare
+    git init $PACKAGE_NAME
+    cd $PACKAGE_NAME
+    git pull --depth 1 https://gerrit.named-data.net/$PACKAGE_NAME $GERRIT_BRANCH
+  elif [[ $SOURCE == local ]]; then
+    echo "Using local copy of $PACKAGE_NAME"
+    if [[ ! -d packages/$PACKAGE_NAME ]]; then
+      echo "ERROR: No local copy of package $PACKAGE_NAME could be found in prepare"
+      exit 1
+    fi
+    cp -r packages/$PACKAGE_NAME prepare
+    cd prepare/$PACKAGE_NAME
+  else
+    echo "SYNTAX ERROR: Package $PACKAGE_NAME has incorrectly formatted source: $PACKAGE_SOURCE"
+    exit 2
+  fi
+}