#!/usr/bin/env bash

set -u

[[ "$@" =~ --pre ]] && version=0.2.0 pre=1 ||
                       version=0.2.0 pre=0

# If stdin is a tty, we are "interactive".
interactive=
[ -t 0 ] && interactive=yes

ask() {
  # non-interactive shell: wait for a linefeed
  #     interactive shell: continue after a single keypress
  [ -n "$interactive" ] && read_n='-n 1' || read_n=

  read -p "$1 ([y]/n) " $read_n -r
  echo
  [[ $REPLY =~ ^[Nn]$ ]]
}

symlink() {
  echo "  - Creating symlink: bin/$1 -> bin/boilr"
  (cd "$HOME"/bin &&
   rm -f boilr &&
   ln -sf $1 boilr)
  if [ $? -ne 0 ]; then
    binary_error="Failed to create symlink"
    return 1
  fi
}

configure() {
  $HOME/bin/boilr init
  if [ $? -ne 0 ]; then
    binary_error="Failed to complete boilr initialization"
    return
  fi

  # Auto-completion prompt
  if [ -z "$auto_completion" ]; then
    ask "Do you want to enable auto-completion for boilr commmands?"
    auto_completion=$?
  fi

  if [ $auto_completion -eq 1 ]; then
    $HOME/bin/boilr configure-bash-completion
  fi
}

check_binary() {
  echo -n "  - Checking boilr executable ... "
  local output
  output=$("$HOME"/bin/boilr version --dont-prettify 2>&1)
  if [ $? -ne 0 ]; then
    echo "Error: $output"
    binary_error="Invalid binary"
  elif [ "$version" != "$output" ]; then
    echo "$output != $version"
    binary_error="Invalid version"
  else
    echo "$output"
    binary_error=""
    return 0
  fi
  rm -f "$HOME"/bin/boilr
  return 1
}

download() {
  echo "Downloading boilr ..."

  if [ -x "$HOME"/bin/boilr ]; then
    echo "  - Already exists"
    check_binary && return
  fi
  if [ -x "$HOME"/bin/$1 ]; then
    symlink $1 && check_binary && return
  fi
  if which_boilr="$(which boilr 2> /dev/null)"; then
    echo "  - Found in \$PATH"
    echo "  - Creating symlink: $which_boilr -> bin/boilr"
    (cd "$HOME"/bin && rm -f boilr && ln -sf "$which_boilr" boilr)
    check_binary && return
  fi

  mkdir -p "$HOME"/bin && cd "$HOME"/bin
  if [ $? -ne 0 ]; then
    binary_error="Failed to create bin directory"
    return
  fi

  local url=https://github.com/tmrts/boilr/releases/download/$version/${1}.tgz
  if which curl > /dev/null; then
    curl -fL $url | tar -xz
  elif which wget > /dev/null; then
    wget -O - $url | tar -xz
  else
    binary_error="curl or wget not found"
    return
  fi

  if [ ! -f $1 ]; then
    binary_error="Failed to download ${1}"
    return
  fi

  chmod +x $1 && symlink $1 && check_binary
  configure
}

# Try to download binary executable
archi=$(uname -sm)
binary_available=1
binary_error=""
case "$archi" in
  Darwin\ x86_64) download boilr-$version-darwin_${binary_arch:-amd64}   ;;
  Darwin\ i*86)   download boilr-$version-darwin_${binary_arch:-386}     ;;
  Linux\ x86_64)  download boilr-$version-linux_${binary_arch:-amd64}  ;;
  Linux\ i*86)    download boilr-$version-linux_${binary_arch:-386}    ;;
  *)              binary_available=0 binary_error=1  ;;
esac

cat << EOF
Completed installation

Boilr executable is installed to ~/bin/boilr

For more information, see: https://github.com/tmrts/boilr
EOF
