#!/bin/sh

#######################################################
#                                                     #
# v2.1 pegasus (07-Sep-2026)               #
#                                                     #
# To activate this pegasus repository:                #
# - save this file as: pegasus-install-script.sh      #
# - run command: chmod +x pegasus-install-script.sh   #
# - run command: ./pegasus-install-script.sh          #
#                                                     #
# Note: this script requires root permissions to      #
# perform the installation;                           #
# if not root, it requires 'sudo' or 'doas' installed #
#                                                     #
#######################################################

if [ $EUID -ne 0 ]; then
  if command -v doas 1>/dev/null; then
    runasroot=doas
  elif command -v sudo 1>/dev/null; then
    runasroot=sudo
  else
    printf '\n'
    printf '%s\n' 'ERROR: No root permissions, sudo or doas are not installed, exiting'
    exit 1
  fi
else
  runasroot=""
fi

if ! pacman-key -f 9C149FFF687526C7 1> /dev/null 2>&1 ; then
  printf '\n'
  printf '%s\n' 'Obtaning, importing and adding pacman key'
  $runasroot sh -c 'curl -sS https://pegasus.xubi.org/pegasus/repos/pegasus-pgpkey-9C149FFF687526C7.asc | pacman-key --add -'
  $runasroot pacman-key --finger 9C149FFF687526C7
  $runasroot pacman-key --lsign-key 9C149FFF687526C7
fi

if ! grep -q '^\[pegasus\]' /etc/pacman.conf ; then
  printf '\n'
  printf '%s\n' 'Adding the pegasus repository to file /etc/pacman.conf'
  $runasroot sh -c "printf '\n' >> /etc/pacman.conf"
  $runasroot sh -c "printf '%s\n' '[pegasus]'  >> /etc/pacman.conf"
  $runasroot sh -c "printf '%s\n' ''' Server = https://pegasus.xubi.org/pegasus/repos/\$repo/os/\$arch/''' >> /etc/pacman.conf"
fi 

printf '\n'
printf '%s\n' 'Refreshing pacman database caches'
$runasroot pacman -Syy

printf '\n'
printf '%s\n' 'All done.'

printf '\n'
printf '%s\n' 'Check this URL for available packages:'
printf '%s\n' ' https://pegasus.xubi.org/pegasus/os/x86_64/1-pegasus-package-overview.html'

printf '%s\n' 'Attention: in case Arch Linux repositories are present in /etc/pacman.conf move the entry'
printf '%s\n' 'for the pegasus one to a place above these.'

