#!/bin/sh
# Installs or updates the v2 CLI: curl -fsSL https://v2.video/install.sh | sh
set -eu

SITE="${V2_INSTALL_SITE:-https://v2.video/releases}"
BIN="$HOME/.local/bin"

os="$(uname -s)"
case "$os" in
  Darwin) file=v2-macos.zip ;;
  Linux)
    case "$(uname -m)" in
      x86_64 | amd64) file=v2-linux-amd64 ;;
      aarch64 | arm64) file=v2-linux-arm64 ;;
      *) echo "v2 runs on 64-bit Intel, AMD and ARM Linux, not $(uname -m)." >&2; exit 1 ;;
    esac
    ;;
  *) echo "v2 runs on macOS and Linux only for now." >&2; exit 1 ;;
esac

# Alpine has wget but no curl.
if command -v curl >/dev/null 2>&1; then get() { curl -fsSL -o "$1" "$2"; }
elif command -v wget >/dev/null 2>&1; then get() { wget -qO "$1" "$2"; }
else echo "Installing v2 needs curl or wget; install one and try again." >&2; exit 1
fi

# hint <package>: how to install it here, with the package manager this machine has.
hint() {
  sudo=sudo
  [ "$(id -u)" = 0 ] && sudo=
  for m in "apt:apt install" "dnf:dnf install" "pacman:pacman -S" "apk:apk add" "zypper:zypper install"; do
    command -v "${m%%:*}" >/dev/null 2>&1 || continue
    echo "run ${sudo:+$sudo }${m#*:} $1"
    return
  done
  echo "install $1 with your package manager"
}

tmp="$(mktemp -d)"
trap 'rm -rf "$tmp"' EXIT

echo "Downloading v2..."
get "$tmp/$file" "$SITE/latest/$file" && get "$tmp/checksums.txt" "$SITE/latest/checksums.txt" ||
  { echo "Couldn't download $file from v2.video. Nothing was installed; try again in a few minutes." >&2; exit 1; }
# checksums.txt lists every platform's file; check only the one downloaded here. Alpine has sha256sum but no shasum.
want="$(awk -v f="$file" '$2 == f { print $1 }' "$tmp/checksums.txt")"
if command -v sha256sum >/dev/null 2>&1; then got="$(sha256sum "$tmp/$file")"; else got="$(shasum -a 256 "$tmp/$file")"; fi
if [ -z "$want" ] || [ "$want" != "${got%% *}" ]; then
  echo "The download doesn't match its checksum. Nothing was installed; try again." >&2
  exit 1
fi

if [ "$os" = Darwin ]; then unzip -q -o "$tmp/$file" v2 -d "$tmp"; else mv "$tmp/$file" "$tmp/v2"; fi
mkdir -p "$BIN"
chmod +x "$tmp/v2"
mv -f "$tmp/v2" "$BIN/v2"

case ":$PATH:" in
  *":$BIN:"*) ;;
  *)
    line='export PATH="$HOME/.local/bin:$PATH"'
    # A plain `docker run` sets no SHELL; the login shell is still in /etc/passwd.
    shell="${SHELL:-$(awk -F: -v u="$(id -un)" '$1 == u { print $7 }' /etc/passwd 2>/dev/null)}"
    case "$os:$shell" in
      Darwin:*/bash) rc="$HOME/.bash_profile" ;;
      Darwin:*) rc="$HOME/.zshrc" ;;
      *:*/bash) rc="$HOME/.bashrc" ;;
      *:*/zsh) rc="$HOME/.zshrc" ;;
      *) rc="$HOME/.profile" ;;
    esac
    grep -qsF "$line" "$rc" || echo "$line" >> "$rc"
    echo "Added ~/.local/bin to PATH in $rc. Until you open a new terminal, run v2 as ~/.local/bin/v2."
    ;;
esac

if [ "$os" = Darwin ]; then
  # /usr/bin/git exists on every Mac but only opens an install dialog until the developer tools are in.
  if [ "$(command -v git)" = /usr/bin/git ] && ! xcode-select -p >/dev/null 2>&1; then echo "v2 needs git: run xcode-select --install" >&2; fi
  command -v ffmpeg >/dev/null 2>&1 || echo "v2 needs ffmpeg: run brew install ffmpeg" >&2
else
  command -v git >/dev/null 2>&1 || echo "v2 needs git: $(hint git)" >&2
  command -v ffmpeg >/dev/null 2>&1 || echo "v2 needs ffmpeg: $(hint ffmpeg)" >&2
fi

echo "Installed $("$BIN/v2" --version) to $BIN/v2"
