#!/usr/bin/env bash

# omarchy:summary=Install Private Internet Access, the VPN client
# omarchy:requires-sudo=true

# Based: PIA ships a self-extracting installer for Linux and no package, so
# it is fetched and run, as the former dev tooling did; macOS takes the
# cask.

set -eo pipefail

version=3.7-08412

echo "Installing Private Internet Access..."

case $(omarchy-os) in
darwin)
  omarchy-pkg-add private-internet-access cask=private-internet-access
  ;;
*)
  if [[ ! -x /opt/piavpn/bin/pia-client ]]; then
    case $(dpkg --print-architecture) in
    arm64) file="pia-linux-arm64-$version.run" ;;
    *) file="pia-linux-$version.run" ;;
    esac
    tmp=$(mktemp -d)
    trap 'rm -rf "$tmp"' EXIT
    curl -fL --progress-bar "https://installers.privateinternetaccess.com/download/$file" -o "$tmp/pia-installer.run"
    chmod +x "$tmp/pia-installer.run"
    "$tmp/pia-installer.run"
  fi
  ;;
esac
