#!/usr/bin/env bash

# omarchy:summary=Install Angry IP Scanner
# omarchy:requires-sudo=true

# Based: the vendor's deb from its GitHub releases on amd64, the only Linux
# build; the macOS cask fails Gatekeeper, so a Mac unpacks the release zip
# for its architecture into /Applications and clears the quarantine flag.

set -eo pipefail

version=3.9.1
release="https://github.com/angryip/ipscan/releases/download/$version"

echo "Installing Angry IP Scanner..."

case $(omarchy-os) in
darwin)
  if [[ ! -d "/Applications/Angry IP Scanner.app" ]]; then
    case $(uname -m) in
    arm64) file="ipscan-macArm64-$version.zip" ;;
    *) file="ipscan-macX86-$version.zip" ;;
    esac
    tmp=$(mktemp -d)
    trap 'rm -rf "$tmp"' EXIT
    curl -fL --progress-bar "$release/$file" -o "$tmp/ipscan.zip"
    unzip -qo "$tmp/ipscan.zip" -d /Applications
    xattr -dr com.apple.quarantine "/Applications/Angry IP Scanner.app" 2>/dev/null || true
  fi
  ;;
*)
  if [[ $(dpkg --print-architecture) != amd64 ]]; then
    echo "Angry IP Scanner has no Linux build for $(dpkg --print-architecture); nothing to install"
    exit 0
  fi
  if omarchy-pkg-missing ipscan; then
    tmp=$(mktemp -d)
    trap 'rm -rf "$tmp"' EXIT
    curl -fL --progress-bar "$release/ipscan_${version}_amd64.deb" -o "$tmp/ipscan.deb"
    sudo apt-get install -y "$tmp/ipscan.deb"
  fi
  ;;
esac
