#!/usr/bin/env bash
# Run on Kali amd64: sudo bash setup-loggraphic-apt.sh
set -euo pipefail
[[ $(id -u) == 0 ]] || { echo 'Run with sudo bash setup-loggraphic-apt.sh' >&2; exit 1; }
[[ $(dpkg --print-architecture) == amd64 ]] || { echo 'This repository supports amd64 only' >&2; exit 1; }
command -v curl >/dev/null || { echo 'Install curl first: sudo apt install curl' >&2; exit 1; }
repo_tmp=$(mktemp -d)
trap 'rm -rf "$repo_tmp"' EXIT
curl -fsS https://repo.sb-i.jp/loggraphic/loggraphic-archive-keyring.gpg -o "$repo_tmp/key.gpg"
printf '%s  %s\n' '53f4110b3125a346569704ede87fead29da85965c1a8403698521111131c15b1' "$repo_tmp/key.gpg" | sha256sum -c -
printf '%s\n' 'deb [arch=amd64 signed-by=/usr/share/keyrings/loggraphic-archive-keyring.gpg] https://repo.sb-i.jp/loggraphic/ ./' > "$repo_tmp/loggraphic.list"
# Explicit migration of this application's dedicated source and key only.
for dest in /usr/share/keyrings/loggraphic-archive-keyring.gpg /etc/apt/sources.list.d/loggraphic.list; do
    if [[ -e "$dest" ]]; then cp -p "$dest" "$dest.backup-$(date +%Y%m%d%H%M%S)"; fi
done
install -m 0644 "$repo_tmp/key.gpg" /usr/share/keyrings/loggraphic-archive-keyring.gpg
install -m 0644 "$repo_tmp/loggraphic.list" /etc/apt/sources.list.d/loggraphic.list
apt-get update -o Dir::Etc::sourcelist=/etc/apt/sources.list.d/loggraphic.list -o Dir::Etc::sourceparts=- -o APT::Get::List-Cleanup=0
apt-cache policy loggraphic
printf '\nReady. Install: sudo apt install loggraphic\nUpdate: sudo apt update && sudo apt install --only-upgrade loggraphic\n'
