#!/bin/sh
# SoundNinja installer for Ubuntu, Debian and derivatives.
# Usage: curl -fsSL https://soundninja.marcus-universe.com/install/ubuntu.sh | sh
set -eu

# Everything runs from main, so a download cut off half way executes nothing.
main() {
  base="https://github.com/marcus-universe/SoundNinja/releases/latest/download"

  command -v dpkg >/dev/null 2>&1 || fail "dpkg not found. This script is for Debian based systems."
  command -v curl >/dev/null 2>&1 || fail "curl not found. Install it with: sudo apt install curl"

  arch=$(dpkg --print-architecture)
  case "$arch" in
    amd64 | arm64 | armhf) ;;
    *) fail "No .deb for CPU architecture '$arch'." ;;
  esac

  tmp=$(mktemp -d)
  trap 'rm -rf "$tmp"' EXIT
  # apt runs as the _apt user and must be able to read the package.
  chmod 755 "$tmp"

  file="soundninja-linux-$arch.deb"
  echo "Downloading $file ..."
  curl -fL --progress-bar -o "$tmp/$file" "$base/$file"

  echo "Installing (asks for your password) ..."
  sudo apt-get install -y "$tmp/$file"

  echo "Done. Start Sound Ninja from the app menu, or run: soundninja"
}

fail() {
  echo "Error: $1" >&2
  exit 1
}

main "$@"
