#!/usr/bin/env bash set -euo pipefail readonly repoName="stormux" readonly repoKeyUrl="https://packages.stormux.org/stormux_repo.pub" readonly repoKeyId="52ADA49000F1FF0456F8AEEFB4CDE1CD56EF8E82" readonly pacmanConf="${STORMUX_PACMAN_CONF:-/etc/pacman.conf}" readonly osRelease="${STORMUX_OS_RELEASE:-/etc/os-release}" readonly effectiveUid="${STORMUX_TEST_EUID:-${EUID}}" keyFile="" cleanup() { if [[ -n "$keyFile" && -f "$keyFile" ]]; then rm -f "$keyFile" fi } die() { printf 'Error: %s\n' "$1" >&2 exit 1 } require_root() { [[ "$effectiveUid" == "0" ]] || die "This script must be run as root." } require_command() { local commandName="$1" command -v "$commandName" >/dev/null 2>&1 || die "Required command not found: ${commandName}" } is_arch_based_system() { local osId="" local osIdLike="" [[ -f "$osRelease" ]] || return 1 while IFS='=' read -r keyName keyValue; do keyValue="${keyValue%\"}" keyValue="${keyValue#\"}" case "$keyName" in ID) osId="$keyValue" ;; ID_LIKE) osIdLike="$keyValue" ;; esac done < "$osRelease" [[ "$osId" == "arch" || "$osId" == "archarm" || " ${osIdLike} " == *" arch "* ]] } check_prerequisites() { require_root require_command curl require_command pacman require_command pacman-key is_arch_based_system || die "This script supports Arch-based pacman systems only." [[ -f "$pacmanConf" ]] || die "pacman.conf not found: ${pacmanConf}" } download_and_trust_key() { keyFile="$(mktemp)" trap cleanup EXIT printf 'Downloading Stormux repository key...\n' curl -fsSL "$repoKeyUrl" > "$keyFile" printf 'Adding Stormux repository key to pacman keyring...\n' pacman-key --add "$keyFile" printf 'Locally signing Stormux repository key...\n' pacman-key --lsign-key "$repoKeyId" } update_pacman_conf() { local tempConf tempConf="$(mktemp)" awk -v repoName="$repoName" ' function print_repo_block() { print "[" repoName "]" print "SigLevel = Required DatabaseOptional" print "Server = https://packages.stormux.org/$arch" } /^\[stormux\][[:space:]]*$/ { inStormux = 1 next } inStormux && /^\[[^]]+\][[:space:]]*$/ { inStormux = 0 } inStormux { next } !inserted && /^\[core\][[:space:]]*$/ { print_repo_block() print "" inserted = 1 } { print } END { if (!inserted) { print "" print_repo_block() } } ' "$pacmanConf" > "$tempConf" cat "$tempConf" > "$pacmanConf" rm -f "$tempConf" } refresh_databases() { printf 'Refreshing package databases...\n' pacman -Sy } main() { check_prerequisites download_and_trust_key update_pacman_conf refresh_databases printf 'Stormux repository is configured.\n' } main "$@"