From a6e4b0fea567d83e9667b12f1a96a1d3c0ed0493 Mon Sep 17 00:00:00 2001
From: Adam Warner <adamw@rner.email>
Date: Tue, 1 May 2018 21:59:12 +0100
Subject: [PATCH] - Test for existence of remote non-master branch of FTL
 before attemtping to check for update - Move some functions from checkout to
 basic-install - provide helpful error message on downlaod not existing

Signed-off-by: Adam Warner <adamw@rner.email>
---
 advanced/Scripts/piholeCheckout.sh | 88 ++---------------------------
 advanced/Scripts/update.sh         | 11 +++-
 automated install/basic-install.sh | 89 ++++++++++++++++++++++++++++++
 3 files changed, 104 insertions(+), 84 deletions(-)

diff --git a/advanced/Scripts/piholeCheckout.sh b/advanced/Scripts/piholeCheckout.sh
index 71457f9b..65bb5b7a 100644
--- a/advanced/Scripts/piholeCheckout.sh
+++ b/advanced/Scripts/piholeCheckout.sh
@@ -17,92 +17,14 @@ source "${PI_HOLE_FILES_DIR}/automated install/basic-install.sh"
 # piholeGitURL set in basic-install.sh
 # is_repo() sourced from basic-install.sh
 # setupVars set in basic-install.sh
+# check_download_exists sourced from basic-install.sh
+# fully_fetch_repo sourced from basic-install.sh
+# get_available_branches sourced from basic-install.sh
+# fetch_checkout_pull_branch sourced from basic-install.sh
+# checkout_pull_branch sourced from basic-install.sh
 
 source "${setupVars}"
 
-coltable="/opt/pihole/COL_TABLE"
-source ${coltable}
-
-check_download_exists() {
-  status=$(curl --head --silent "https://ftl.pi-hole.net/${1}" | head -n 1)
-  if grep -q "404" <<< "$status"; then
-    return 1
-  else
-    return 0
-  fi
-}
-
-fully_fetch_repo() {
-  # Add upstream branches to shallow clone
-  local directory="${1}"
-
-  cd "${directory}" || return 1
-  if is_repo "${directory}"; then
-    git remote set-branches origin '*' || return 1
-    git fetch --quiet || return 1
-  else
-    return 1
-  fi
-  return 0
-}
-
-get_available_branches() {
-  # Return available branches
-  local directory
-  directory="${1}"
-  local output
-
-  cd "${directory}" || return 1
-  # Get reachable remote branches, but store STDERR as STDOUT variable
-  output=$( { git remote show origin | grep 'tracked' | sed 's/tracked//;s/ //g'; } 2>&1 )
-  echo "$output"
-  return
-}
-
-fetch_checkout_pull_branch() {
-  # Check out specified branch
-  local directory
-  directory="${1}"
-  local branch
-  branch="${2}"
-
-  # Set the reference for the requested branch, fetch, check it put and pull it
-  cd "${directory}" || return 1
-  git remote set-branches origin "${branch}" || return 1
-  git stash --all --quiet &> /dev/null || true
-  git clean --quiet --force -d || true
-  git fetch --quiet || return 1
-  checkout_pull_branch "${directory}" "${branch}" || return 1
-}
-
-checkout_pull_branch() {
-  # Check out specified branch
-  local directory
-  directory="${1}"
-  local branch
-  branch="${2}"
-  local oldbranch
-
-  cd "${directory}" || return 1
-
-  oldbranch="$(git symbolic-ref HEAD)"
-
-  str="Switching to branch: '${branch}' from '${oldbranch}'"
-  echo -ne "  ${INFO} $str"
-  git checkout "${branch}" --quiet || return 1
-  echo -e "${OVER}  ${TICK} $str"
-
-  git_pull=$(git pull || return 1)
-
-  if [[ "$git_pull" == *"up-to-date"* ]]; then
-    echo -e "  ${INFO} ${git_pull}"
-  else
-    echo -e "$git_pull\\n"
-  fi
-
-  return 0
-}
-
 warning1() {
   echo "  Please note that changing branches severely alters your Pi-hole subsystems"
   echo "  Features that work on the master branch, may not on a development branch"
diff --git a/advanced/Scripts/update.sh b/advanced/Scripts/update.sh
index 89e97fdf..2279d9c7 100755
--- a/advanced/Scripts/update.sh
+++ b/advanced/Scripts/update.sh
@@ -133,8 +133,17 @@ main() {
     FTL_update=true
     echo -e "  ${INFO} FTL:\\t\\t${COL_YELLOW}update available${COL_NC}"
   else
+    case $? in
+    1)
+      echo -e "  ${INFO} FTL:\\t\\t${COL_LIGHT_GREEN}up to date${COL_NC}"
+      ;;
+    2)
+      echo -e "  ${INFO} FTL:\\t\\t${COL_LIGHT_RED}Branch is not available.${COL_NC}\\n\\t\\t\\tUse ${COL_LIGHT_GREEN}pihole checkout ftl [branchname]${COL_NC} to switch to a valid branch."
+      ;;
+    *)
+      echo -e "  ${INFO} FTL:\\t\\t${COL_LIGHT_RED}Something has gone wrong, contact support${COL_NC}"
+    esac
     FTL_update=false
-    echo -e "  ${INFO} FTL:\\t\\t${COL_LIGHT_GREEN}up to date${COL_NC}"
   fi
 
   if [[ "${core_update}" == false && "${web_update}" == false && "${FTL_update}" == false ]]; then
diff --git a/automated install/basic-install.sh b/automated install/basic-install.sh
index e7c5cd55..4059ea1d 100755
--- a/automated install/basic-install.sh	
+++ b/automated install/basic-install.sh	
@@ -1762,6 +1762,86 @@ update_dialogs() {
     esac
 }
 
+check_download_exists() {
+  status=$(curl --head --silent "https://ftl.pi-hole.net/${1}" | head -n 1)
+  if grep -q "404" <<< "$status"; then
+    return 1
+  else
+    return 0
+  fi
+}
+
+fully_fetch_repo() {
+  # Add upstream branches to shallow clone
+  local directory="${1}"
+
+  cd "${directory}" || return 1
+  if is_repo "${directory}"; then
+    git remote set-branches origin '*' || return 1
+    git fetch --quiet || return 1
+  else
+    return 1
+  fi
+  return 0
+}
+
+get_available_branches() {
+  # Return available branches
+  local directory
+  directory="${1}"
+  local output
+
+  cd "${directory}" || return 1
+  # Get reachable remote branches, but store STDERR as STDOUT variable
+  output=$( { git remote show origin | grep 'tracked' | sed 's/tracked//;s/ //g'; } 2>&1 )
+  echo "$output"
+  return
+}
+
+fetch_checkout_pull_branch() {
+  # Check out specified branch
+  local directory
+  directory="${1}"
+  local branch
+  branch="${2}"
+
+  # Set the reference for the requested branch, fetch, check it put and pull it
+  cd "${directory}" || return 1
+  git remote set-branches origin "${branch}" || return 1
+  git stash --all --quiet &> /dev/null || true
+  git clean --quiet --force -d || true
+  git fetch --quiet || return 1
+  checkout_pull_branch "${directory}" "${branch}" || return 1
+}
+
+checkout_pull_branch() {
+  # Check out specified branch
+  local directory
+  directory="${1}"
+  local branch
+  branch="${2}"
+  local oldbranch
+
+  cd "${directory}" || return 1
+
+  oldbranch="$(git symbolic-ref HEAD)"
+
+  str="Switching to branch: '${branch}' from '${oldbranch}'"
+  echo -ne "  ${INFO} $str"
+  git checkout "${branch}" --quiet || return 1
+  echo -e "${OVER}  ${TICK} $str"
+
+  git_pull=$(git pull || return 1)
+
+  if [[ "$git_pull" == *"up-to-date"* ]]; then
+    echo -e "  ${INFO} ${git_pull}"
+  else
+    echo -e "$git_pull\\n"
+  fi
+
+  return 0
+}
+
 clone_or_update_repos() {
   # If the user wants to reconfigure,
   if [[ "${reconfigure}" == true ]]; then
@@ -1977,6 +2057,15 @@ FTLcheckUpdate()
   fi
 
   if [[ ! "${ftlBranch}" == "master" ]]; then
+    #Check whether or not the binary for this FTL branch actually exists. If not, then there is no update!
+    local path
+    path="${ftlBranch}/${binary}"
+    # shellcheck disable=SC1090
+    if ! check_download_exists "$path"; then
+      echo -e "  ${INFO} Branch \"${ftlBranch}\" is not available.\\n  ${INFO} Use ${COL_LIGHT_GREEN}pihole checkout ftl [branchname]${COL_NC} to switch to a valid branch."
+      return 2
+    fi
+
     if [[ ${ftlLoc} ]]; then
       # We already have a pihole-FTL binary downloaded.
       # Alt branches don't have a tagged version against them, so just confirm the checksum of the local vs remote to decide whether we download or not