#!/bin/bash

set -e

SOFTWARE='tidal'
SOFTWARE_VERSION="latest"
SOURCE="https://get.tidal.sh"
TEMP_INSTALL_PATH="./.tidal-tmp-install"
FIXING_SSL_SUPPORT_URL="https://guides.tidal.cloud/tidal-tools.html"
INSTALL_DIR="$HOME/bin/tidal"

remove_previous () {
 if [ -f "$INSTALL_DIR" ]; then
   echo "Removing previous installation.."
   rm -r "$INSTALL_DIR"
   echo "Previous version successfully removed."
 fi
}

log()  { printf "%b\n" "$*"; }

__flags=("--location")

__tidal_curl()
(
  hash curl >/dev/null ||
  {
    log "Installing tidal requires 'curl'. Install 'curl' first and try again."
    return 200
  }
  curl "${__flags[@]}" "$@" || return $?
)

get_unix_type () {
  if [ "$(uname)" == "Darwin" ] ; then
    echo "macos"
  else
    echo "linux"
  fi
}

get_architecture () {
  case "$(uname -m)" in
    (x86_64|ia64|amd64)
      echo "64"
      ;;
    (arm64)
      echo "arm64"
      ;;
    (i386|i686)
      echo "32"
      ;;
  esac
}

generate_url () {
  a=$(get_architecture)
  u=$(get_unix_type)
  echo "$SOURCE/$SOFTWARE-$u-$a-$SOFTWARE_VERSION"
}

get_package() {
  mkdir -p $TEMP_INSTALL_PATH
  URL=$1
  log "Downloading $URL"
  __tidal_curl -sS "$URL" > $TEMP_INSTALL_PATH/download ||
  {
    _return=$?
    case $_return in
      (60)
        log "
Could not download '$URL', you can read more about it here:
$FIXING_SSL_SUPPORT_URL
To continue in insecure mode run 'echo insecure >> ~/.curlrc'.
"
        ;;
      (77)
        log "
It looks like you have old certificates, you can read more about it here:
$FIXING_SSL_SUPPORT_URL
"
        ;;
      (141)
        log "
Curl returned 141 - it is result of a segfault which means it's Curls fault.
Try again and if it crashes more than a couple of times you either need to
reinstall Curl or consult with your distribution manual and contact support.
"
        ;;
      (*)
        log "
Could not download '$URL'.
  curl returned status '$_return'.
"
        ;;
    esac
    return $_return
  }
}

get_and_unpack() {
  url=$(generate_url)
  get_package "$url" || return $?
  tar xzf $TEMP_INSTALL_PATH/download -C $TEMP_INSTALL_PATH ||
  {
    _return=$?
    log "Could not extract $SOFTWARE."
    return $_return
  }
}

clean_up () {
  rm -r $TEMP_INSTALL_PATH
}

install () {
  log "Installing $SOFTWARE.."
  mkdir -p ~/bin

  cp $TEMP_INSTALL_PATH/tidal-v*/tidal ~/bin/tidal

  cmds="\n# added by Tidal Tools\nexport PATH=\"\$PATH:\$HOME/bin\""
  added=false
  already=false
  if [[ "$PATH" == *"$HOME/bin"* ]]; then
    already=true
  else
    case $(basename "$SHELL") in
      zsh)
        echo -e "$cmds" >> ~/.zshrc
        added="$HOME/.zshrc"
        ;;
      *)
        if [ "$(get_unix_type)" = "macos" ]; then
          echo -e "$cmds" >> ~/.bash_profile
          added="$HOME/.bash_profile"
        else
          echo -e "$cmds" >> ~/.bashrc
          added="$HOME/.bashrc"
        fi
    esac
  fi

  msg=$( cat <<DOC

------------------------------------------------------------
$SOFTWARE successfully installed!

By using this software you are agreeing to the terms here:
https://tidalcloud.com/customer-terms

To get started restart your shell (exec -l \$SHELL)
or open a new terminal window and run '$SOFTWARE'

If it does not work, you may need to:
 - close your terminal completely
 - restart your computer
 - If you are using a shell other than bash or zsh you may
   need to ensure that your shell has the PATH set to
   include $HOME/bin.
DOC
)

  if [ "$already" == true ]; then
    echo "Your path already contains $HOME/bin"
    echo "$msg"
  elif ! [ "$added" == false ] ; then
    echo "Your path was updated in $added"
    echo "$msg"
  else
    echo "Error! Your PATH was not updated. $SOFTWARE has been installed but you need to manually add $HOME/bin to your PATH variable in order to run $SOFTWARE."
  fi
}

tidal_install () {
  get_and_unpack
  remove_previous
  install
  clean_up
}

tidal_install
