#!/bin/sh

{ # Prevent execution if this script was only partially downloaded

  set -eu

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

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

  case "$OS" in
  darwin)
    printf '\033[31m> For macOS download and install DMG from here - https://releases.tuna.am/desktop/latest/tuna-desktop_darwin-universal_latest.dmg\n\033[0m'
    exit 1
    ;;
  linux)
    printf '\033[32m> Linux OS detected, will try find out distro ...\n\033[0m'
    ;;
  freebsd)
    printf '\033[31m> FreeBSD is not supported.\n\033[0m'
    exit 1
    ;;
  *)
    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 [ "$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

  skip_install() {
    echo "Unable to detect distribution, please follow manual installation instructions."
    echo "===> https://tuna.am/docs/"
    exit 0
  }

  install_deb() {
    echo "Debian based detected, will try to install via APT ..."
    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-desktop ..."
    $sh_c "apt-get install -y tuna-desktop"
  }

  install_rhel() {
    echo "RHEL based detected, will try to install via YUM/DNF ..."
    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-desktop ..."
    $sh_c "dnf install -y tuna-desktop"
  }

  install_suse() {
    echo "SUSE based detected, will try to install via ZYPPER ..."
    echo "Setup ZYPPER repository ..."
    $sh_c "zypper addrepo --repo https://releases.tuna.am/tuna-rpm.repo"
    echo "Install tuna-desktop ..."
    $sh_c "zypper install -y tuna-desktop"
  }

  install_altlinux() {
    echo "ALT Linux detected, will try to install via APT-RPM ..."
    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"
    if [ "$VERSION_ID" -ge 11 ]; then
      $sh_c "apt-repo add rpm https://altlinux.space/api/packages/tuna/alt/current.repo $(arch) classic"
    else
      $sh_c "apt-repo add rpm https://altlinux.space/api/packages/tuna/alt/old.repo $(arch) classic"
    fi
    $sh_c "apt-get update -q"
    echo "Install tuna-desktop ..."
    $sh_c "apt-get install -y tuna-desktop"
  }

  install_arch() {
    echo "Arch Linux detected, will try to install via PACMAN ..."
    echo "Install tuna ..."
    ARCH=$(uname -m)
    URL="https://releases.tuna.am"
    TMP_DIR=$(mktemp -d)
    cd "${TMP_DIR}"
    curl -sSLfO "${URL}/tuna.asc"
    curl -sSLfO "${URL}/desktop/latest/tuna-desktop_latest_checksums_linux-pkgs.txt"
    curl -sSLfO "${URL}/desktop/latest/current/tuna-desktop-latest-1-${ARCH}.pkg.tar.zst"
    curl -sSLf "${URL}/desktop/latest/current/tuna-desktop-latest-1-${ARCH}.pkg.tar.zst.sig" -o tuna-desktop_latest.sig
    sha256sum -c --ignore-missing tuna-desktop_latest_checksums_linux-pkgs.txt
    gpg --batch --import tuna.asc
    gpg --batch --verify "tuna-desktop_latest.sig tuna-desktop-latest-1-${ARCH}.pkg.tar.zst"
    $sh_c "pacman -Sy --noconfirm"
    echo "Install tuna-desktop ..."
    $sh_c "pacman -U --noconfirm tuna-desktop-latest-1-${ARCH}.pkg.tar.zst"
    rm -rf "${TMP_DIR}"
  }

  main() {
    case "${1}" in
    linux)
      if [ ! -f /etc/os-release ]; then
        skip_install
      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
          skip_install
        fi
      fi
      ;;
    *) skip_install ;;
    esac
  }

  main "${OS}"

} # End of wrapping
