#!/bin/sh

{ # Prevent execution if this script was only partially downloaded

  set -eu

  DEBUG=${DEBUG:-false}
  if ${DEBUG}; then
      set -x
  fi

  install_dir='/usr/local/bin'
  install_path='/usr/local/bin/tuna'
  OS=$(uname | tr '[:upper:]' '[:lower:]')
  ARCH=$(uname -m | tr '[:upper:]' '[:lower:]')
  cmd_exists() {
    command -v "$@" >/dev/null 2>&1
  }

  bin_file=

  case "$OS" in
  darwin)
    case "$ARCH" in
    x86_64)
      bin_file=tuna_darwin_amd64
      ;;
    arm64)
      bin_file=tuna_darwin_arm64
      ;;
    *)
      printf '\033[31m> The architecture (%s) is not supported by this installation script.\n\033[0m' "$ARCH"
      exit 1
      ;;
    esac
    ;;
  linux)
    case "$ARCH" in
    x86_64)
      bin_file=tuna_linux_amd64
      ;;
    amd64)
      bin_file=tuna_linux_amd64
      ;;
    armv7*)
      bin_file=tuna_linux_arm
      ;;
    armhf)
      bin_file=tuna_linux_arm
      ;;
    armv8*)
      bin_file=tuna_linux_arm64
      ;;
    aarch64)
      bin_file=tuna_linux_arm64
      ;;
    *)
      printf '\033[31m> The architecture (%s) is not supported by this installation script.\n\033[0m' "$ARCH"
      exit 1
      ;;
    esac
    ;;
  freebsd)
    case "$ARCH" in
    x86_64)
      bin_file=tuna_freebsd_amd64
      ;;
    amd64)
      bin_file=tuna_freebsd_amd64
      ;;
    armv7*)
      bin_file=tuna_freebsd_arm
      ;;
    armhf)
      bin_file=tuna_freebsd_arm
      ;;
    armv8*)
      bin_file=tuna_freebsd_arm64
      ;;
    aarch64)
      bin_file=tuna_freebsd_arm64
      ;;
    *)
      printf '\033[31m> The architecture (%s) is not supported by this installation script.\n\033[0m' "$ARCH"
      exit 1
      ;;
    esac
    ;;
  *)
    printf '\033[31m> The OS (%s) is not supported by this installation script.\n\033[0m' "$OS"
    exit 1
    ;;
  esac

  sh_c='sh -c'
  if [ ! -w "$install_dir" ]; then
    # use sudo if $USER doesn't have write access to the path
    if [ "$USER" != 'root' ]; then
      if cmd_exists sudo; then
        sh_c='sudo -E sh -c'
      elif cmd_exists su; then
        sh_c='su -c'
      else
        echo 'This script requires to run commands as sudo. We are unable to find either "sudo" or "su".'
        exit 1
      fi
    fi
  fi

  install_manual() {
    URL="https://releases.tuna.am/tuna/latest/$bin_file.tar.gz"
    printf '> Downloading %s\n' "$URL"
    download_path=$(mktemp -d)
    curl -sfSL "$URL" | tar -f - -xz -C "$download_path"
    chmod +x "$download_path/tuna"
    printf '> Installing %s\n' "$install_path"
    $sh_c "mv -f $download_path/tuna $install_path"
    printf '\033[32m> Tuna successfully installed!\n\033[0m'
  }

  prune_install_manual() {
    if [ -f "$install_path" ]; then
      printf '> Remove manual install %s\n' "$install_path"
      $sh_c "rm -f $install_path"
    fi
  }

  install_deb() {
    echo "Install dependends ..."
    $sh_c "apt-get update -q"
    $sh_c "apt-get install -y ca-certificates curl gnupg"
    echo "Setup APT repository ..."
    $sh_c "curl -fsSL https://repo.tuna.am/apt/gpg.key | gpg --dearmor --batch --yes -o /etc/apt/trusted.gpg.d/tuna.gpg"
    $sh_c "echo 'deb [signed-by=/etc/apt/trusted.gpg.d/tuna.gpg] https://repo.tuna.am/apt/ /' >/etc/apt/sources.list.d/tuna.list"
    $sh_c "apt-get update -q"
    echo "Install tuna ..."
    $sh_c "apt-get install -y tuna.am"
    prune_install_manual
  }

  install_rhel() {
    echo "Setup YUM/DNF repository ..."
    $sh_c "dnf install -y dnf-plugins-core"
    $sh_c "dnf config-manager --add-repo https://releases.tuna.am/tuna-rpm.repo"
    echo "Install tuna ..."
    $sh_c "dnf install -y tuna.am"
    prune_install_manual
  }

  install_suse() {
    echo "Setup ZYPPER repository ..."
    $sh_c "zypper addrepo --repo https://releases.tuna.am/tuna-rpm.repo"
    echo "Install tuna ..."
    $sh_c "zypper install -y tuna.am"
    prune_install_manual
  }

  install_altlinux() {
    echo "Install dependends ..."
    $sh_c "apt-get update"
    $sh_c "apt-get install -y curl apt-repo"
    echo "Setup APT repository ..."
    $sh_c "rpm --import https://releases.tuna.am/tuna.asc"
    $sh_c "apt-repo add rpm https://altlinux.space/api/packages/tuna/alt/current.repo $(arch) classic"
    $sh_c "apt-get update -q"
    echo "Install tuna ..."
    $sh_c "apt-get install -y tuna.am"
    prune_install_manual
  }

  install_arch() {
    echo "Install tuna ..."
    if cmd_exists yay; then
      echo "Found yay, use yay to install tuna ..."
      yay -S --noconfirm --needed tuna.am-bin
    else
      echo "Use pacman to install tuna ..."
      tmpfile=$(mktemp)
      curl -sSLf "https://releases.tuna.am/tuna/latest/${bin_file}.pkg.tar.zst" -o "${tmpfile}"
      $sh_c "pacman -U --noconfirm ${tmpfile}"
      rm -f "${tmpfile}"
    fi
    prune_install_manual
  }

  main() {
    case "${1}" in
    linux)
      if [ ! -f /etc/os-release ]; then
        install_manual
        exit 0
      fi
      . /etc/os-release
      if [ -n "${ID_LIKE}" ]; then
        if echo "${ID_LIKE}" | grep -q "debian"; then
          install_deb
        elif echo "${ID_LIKE}" | grep -q "rhel"; then
          install_rhel
        elif echo "${ID_LIKE}" | grep -q "suse"; then
          install_suse
        elif echo "${ID_LIKE}" | grep -q "arch"; then
          install_arch
        fi
      else
        if echo "${ID}" | grep -q "debian"; then
          install_deb
        elif echo "${ID}" | grep -q "fedora"; then
          install_rhel
        elif echo "${ID}" | grep -q "suse"; then
          install_suse
        elif echo "${ID}" | grep -q "arch"; then
          install_arch
        elif echo "${ID}" | grep -q "altlinux"; then
          install_altlinux
        else
          install_manual
        fi
      fi
      ;;
    *) install_manual ;;
    esac
  }

  main "${OS}"

} # End of wrapping
