#!/usr/bin/env bash set -euo pipefail FORGEJO=forgejo.kjr.sh REPO=https://$FORGEJO/api/packages/KJR.sh/debian SUITE="noble main" APPS=(kjr-forgejo kjr-reposilite kjr-pelican kjr-nextcloud) AUTH=/etc/apt/auth.conf.d/forgejo.conf KEY=/etc/apt/keyrings/forgejo.asc LIST=/etc/apt/sources.list.d/forgejo.list die() { echo "error: $*" >&2; exit 1; } info() { echo "==> $*"; } ask() { local reply read -r ${2:+-s} -p "$1: " reply [[ -z ${2:-} ]] || echo >&2 [[ -n $reply ]] || die "$1 is required" printf '%s' "$reply" } add_source() { local fresh= user token status tmp install -d -m 755 /etc/apt/keyrings /etc/apt/auth.conf.d if [[ ! -s $AUTH ]]; then info "Signing in to $FORGEJO to read packages" user=$(ask "Forgejo username") token=$(ask "Forgejo token with read:package" secret) printf 'machine %s login %s password %s\n' "$FORGEJO" "$user" "$token" | install -m 600 /dev/stdin "$AUTH" fresh=1 fi tmp=$(mktemp) status=$(curl -sS --netrc-file "$AUTH" -o "$tmp" -w '%{http_code}' "$REPO/repository.key") || status=000 [[ $status != 200 ]] || install -m 644 "$tmp" "$KEY" rm -f "$tmp" case $status in 200) ;; 401|403) if [[ -n $fresh ]]; then rm -f "$AUTH" die "$FORGEJO rejected that username and token" fi die "$FORGEJO rejected the token in $AUTH. Delete that file and run this again" ;; *) die "could not reach $REPO (HTTP $status)" ;; esac echo "deb [signed-by=$KEY] $REPO $SUITE" > "$LIST" } main() { [[ $EUID -eq 0 ]] || die "run this as root: curl -fsSL https://get.kjr.sh | sudo bash" command -v apt-get >/dev/null || die "this needs Ubuntu or Debian" (( $# )) || set -- "${APPS[@]}" local pkgs=() name for name; do pkgs+=("kjr-${name#kjr-}") done add_source info "Installing ${pkgs[*]}" apt-get update apt-get install -y "${pkgs[@]}" [[ -f /var/lib/kjr-core/.configured ]] || kjr-core setup } { : /dev/null || die "run this from a terminal, because setup asks for an admin account and a Cloudflare login" main "$@"