#!/usr/bin/env bash

# -u: treat unset variables as an error and exit immediately
# -e: exit when command fails
set -e

# print commands prior to executing (for debuging)
#set -x


PP_GITREPO="https://github.com/txoof/PaperPi.git"
#PP_GITREPO="git@github.com:txoof/PaperPi.git"
#PP_GITBRANCH="manage_modules"
PP_GITBRANCH="main"

long_bit=$(getconf LONG_BIT)


if [[ $1 == "-b" && ! -z $2 ]]
then
  PP_GITBRANCH="$2"
else  
  PP_GITBRANCH="main"
fi

echo "installing PaperPi from branch: $PP_GITBRANCH"

INSTALLER="/install/install.sh"

# string formatters
if [[ -t 1 ]]
then
  tty_escape() { printf "\033[%sm" "$1"; }
else
  tty_escape() { :; }
fi
tty_mkbold() { tty_escape "1;$1"; }
tty_underline="$(tty_escape "4;39")"
tty_blue="$(tty_mkbold 34)"
tty_red="$(tty_mkbold 31)"
tty_bold="$(tty_mkbold 39)"
tty_reset="$(tty_escape 0)"


abort() {
  printf "%s\n" "$@"
  exit 1
}

shell_join() {
  local arg
  printf "%s" "$1"
  shift
  for arg in "$@"
  do
    printf " "
    printf "%s" "${arg// /\ }"
  done
}


ohai() {
  printf "${tty_blue}==>${tty_bold} %s${tty_reset}\n" "$(shell_join "$@")"
}




# Fail fast with a concise message when not using bash
# Single brackets are needed here for POSIX compatibility
if [ -z "${BASH_VERSION:-}" ]
then
  abort "Bash is required to interpret this script."
fi

# # fail if not 32 bit os
# if [ ! "$long_bit" == "32" ]
# then
#   abort "PaperPi is officially supported only on 32 bit versions of RaspberryPi OS. See the README for manual install instructions."
# fi

# check if git is available
if ! command -v git > /dev/null
then
  abort "$(
    cat <<EOABORT
You must install Git before installing paperpi.
try:
  sudo apt install git
EOABORT
  )"
fi

git_temp=$(mktemp -d -t PaperPi_XXXXX)
echo "cloning git repo into $git_temp"

git_output=$(git clone -b $PP_GITBRANCH --single-branch $PP_GITREPO $git_temp)
git_exitcode=$?

if [ $git_exitcode -eq 0 ]
then
  echo "sucessfully cloned branch $PP_GITREPO into $git_temp"
else
  echo "failed to clone branch $PP_GITREPO with the following errors:"
  echo $git_output
  echo ""
  abort "$(printf "Failed during: %s" "$(shell_join "$@")")"
fi

unset HAVE_SUDO_ACCESS # unset this from the environment

have_sudo_access() {
  if [[ ! -x "/usr/bin/sudo" ]]
  then
    return 1
  fi

  local -a SUDO=("/usr/bin/sudo")
  if [[ -n "${SUDO_ASKPASS-}" ]]
  then
    SUDO+=("-A")
  elif [[ -n "${NONINTERACTIVE-}" ]]
  then
    SUDO+=("-n")
  fi

  if [[ -z "${HAVE_SUDO_ACCESS-}" ]]
  then
    if [[ -n "${NONINTERACTIVE-}" ]]
    then
      "${SUDO[@]}" -l mkdir &>/dev/null
    else
      "${SUDO[@]}" -v && "${SUDO[@]}" -l mkdir &>/dev/null
    fi
    HAVE_SUDO_ACCESS="$?"
  fi

  return "${HAVE_SUDO_ACCESS}"
}

execute() {
  if ! "$@"
  then
    abort "$(printf "Failed during: %s" "$(shell_join "$@")")"
  fi
}

execute_sudo() {
  local -a args=("$@")
  if have_sudo_access
  then
    if [[ -n "${SUDO_ASKPASS-}" ]]
    then
      args=("-A" "${args[@]}")
    fi
    ohai "/usr/bin/sudo" "${args[@]}"
    execute "/usr/bin/sudo" "${args[@]}"
  else
    ohai "${args[@]}"
    execute "${args[@]}"
  fi
}
echo "$git_temp"
execute_sudo "$git_temp/install/install.sh"

rm -rf $git_temp
