#!/bin/bash
. ./lib

options=()
if [ ! -f /usr/bin/pacdiff ]; then
  options+=("Install pacman-contrib" "")
  options+=("" "")
fi

if [ -f /usr/bin/trizen ]; then
  options+=("Upgrade with trizen" "trizen -Syu")
  aurhelper=1
fi
if [ -f /usr/bin/yay ]; then
  options+=("Upgrade with yay" "yay -Syu")
  aurhelper=1
fi
if [ -f /usr/bin/aurman ]; then
  options+=("Upgrade with aurman" "aurman -Syu")
  aurhelper=1
fi
if [ -f /usr/bin/yaourt ]; then
  options+=("Upgrade with yaourt" "yaourt -Syua")
  aurhelper=1
fi

if [ ! "$aurhelper" = "1" ]; then
  options+=("Install trizen" "")
  options+=("Install yay" "")
  options+=("Install aurman" "")
  options+=("Install yaourt" "(End of life)")
  options+=("" "")
fi
options+=("Upgrade" "pacman -Syu")
options+=("Clean orphan" "pacman -Rns \$(pacman -Qqtd)")
options+=("Clean cache" "pacman -Sc")
options+=("" "")
options+=("Edit pacman.conf" "")
options+=("Edit mirrorlist" "")
options+=("" "")
options+=("Update keyring" "pacman -S archlinux-keyring")
options+=("Refresh pacman keys" "pacman-key --refresh-keys")
options+=("Add GPG key" "gpg --recv-keys")
options+=("" "")
if [ -f /var/lib/pacman/db.lck ]; then
  options+=("Remove db.lck" "rm /var/lib/pacman/db.lck")
  options+=("" "")
fi
if [ "$aurhelper" = "1" ]; then
  if [ ! -f /usr/bin/downgrade ]; then
    options+=("Install downgrade" "")
  fi
  if [ ! -f /usr/bin/pkgstats ]; then
    options+=("Install pkgstats" "")
  fi
  if [ ! -f /usr/bin/aurvote ]; then
    options+=("Install aurvote" "")
  fi
  if [ ! -f /usr/bin/trizen ]; then
    options+=("Install trizen" "")
  fi
  if [ ! -f /usr/bin/yay ]; then
    options+=("Install yay" "")
  fi
  if [ ! -f /usr/bin/aurman ]; then
    options+=("Install aurman" "")
  fi
  if [ ! -f /usr/bin/yaourt ]; then
    options+=("Install yaourt" "(End of life)")
  fi
fi

sel=$(whiptail --backtitle "$apptitle" --title "Updates Menu :" --menu "" --cancel-button "Back" 0 0 0 \
  "${options[@]}" \
  3>&1 1>&2 2>&3)
if [ ! "$?" = "0" ]; then
  exit 1
fi

checkaurdependencies(){
  if [ ! -f /usr/bin/automake ]; then
    pacman -S --needed base-devel
  fi
  if [ ! -f /usr/bin/git ]; then
    pacman -S --needed git
  fi
  if [ ! $(id -u "aurbuilder") ]; then
    newpass=$(< /dev/urandom tr -dc "@#*%&_A-Z-a-z-0-9" | head -c16)
    useradd -r -N -M -d /tmp/aurbuilder -s /usr/bin/nologin aurbuilder
    echo -e "$newpass\n$newpass\n"|passwd aurbuilder
    newpass=""
    mkdir /tmp/aurbuilder 1&>/dev/null
    chmod 777 /tmp/aurbuilder
  else
    mkdir /tmp/aurbuilder 1&>/dev/null
    chmod 777 /tmp/aurbuilder
    usermod -d /tmp/aurbuilder aurbuilder
  fi
  if [ ! -f /etc/sudoers.d/aurbuilder ]; then
    echo "aurbuilder ALL=(ALL) NOPASSWD: ALL" > /etc/sudoers.d/aurbuilder
    echo "root ALL=(aurbuilder) NOPASSWD: ALL" >> /etc/sudoers.d/aurbuilder
  fi
}
checkpackagequerydependencies(){
  if [ ! -f /usr/bin/package-query ]; then
    cd /tmp/aurbuilder
    sudo -u aurbuilder git clone https://aur.archlinux.org/package-query.git
    cd package-query
    sudo -u aurbuilder makepkg -si
  fi
}

clear
case $sel in
  'Upgrade') pacman -Syu;;
  'Upgrade with trizen')
    checkaurdependencies
    sudo -u aurbuilder trizen -Syu
  ;;
  'Upgrade with aurman')
    checkaurdependencies
    sudo -u aurbuilder aurman -Syu
  ;;
  'Upgrade with yay')
    checkaurdependencies
    sudo -u aurbuilder yay -Syu
  ;;
  'Upgrade with yaourt')
    checkaurdependencies
    sudo -u aurbuilder yaourt -Syua
  ;;
  'Clean orphan') pacman -Rns $(pacman -Qqtd);;
  'Clean cache') pacman -Sc;;
  'Edit pacman.conf') editfile /etc/pacman.conf;;
  'Edit mirrorlist') editfile /etc/pacman.d/mirrorlist;;
  'Update keyring') pacman -S archlinux-keyring;;
  'Refresh pacman keys') pacman-key --refresh-keys;;
  'Add GPG key')
    gpgservers=()
    gpgservers+=("pool.sks-keyservers.net" "")
    gpgservers+=("Other" "")
    sel=$(whiptail --backtitle "$apptitle" --title "Add GPG key" --menu "GPG key server" 0 0 0 "${gpgservers[@]}" 3>&1 1>&2 2>&3)
    if [ "$?" = "0" ]; then
      case $sel in
        "Other")
          gpgserver=$(whiptail --backtitle "$apptitle" --title "Add GPG key" --inputbox "Enter the GPG key server :" 0 0 "" 3>&1 1>&2 2>&3)
          if [ "$?" = "0" ]; then
            gpgkey=$(whiptail --backtitle "$apptitle" --title "Add GPG key" --inputbox "Enter the GPG key :" 0 0 3>&1 1>&2 2>&3)
            if [ "$?" = "0" ]; then
              echo "sudo -u aurbuilder gpg --keyserver "$gpgserver" --recv-keys "$gpgkey
              sudo -u aurbuilder gpg --keyserver $gpgserver --recv-keys $gpgkey
              pressanykey
            fi
          fi
        ;;
        *)
          gpgserver=$sel
          gpgkey=$(whiptail --backtitle "$apptitle" --title "Add GPG key" --inputbox "Enter the GPG key :" 0 0 3>&1 1>&2 2>&3)
          if [ "$?" = "0" ]; then
            echo "sudo -u aurbuilder gpg --keyserver "$gpgserver" --recv-keys "$gpgkey
            sudo -u aurbuilder gpg --keyserver $gpgserver --recv-keys $gpgkey
            pressanykey
          fi
        ;;
      esac
    fi
  ;;
  'Remove db.lck') rm /var/lib/pacman/db.lck;;
  'Install pacman-contrib')
    instpkg "pacman-contrib" ""
  ;;
  'Install downgrade')
    instpkg "" "downgrade"
  ;;
  'Install trizen')
    if(confirm "Trizen allow you to access the AUR applications.\nBecause is not possible to compile applications as root,\nthis installer create an aurbuilder user.\n\nInstall trizen ?") then
      clear
      checkaurdependencies
      cd /tmp/aurbuilder
      sudo -u aurbuilder git clone https://aur.archlinux.org/trizen.git
      cd trizen
      sudo -u aurbuilder makepkg -si
    fi
  ;;
  'Install aurman')
    if(confirm "Aurman allow you to access the AUR applications.\nBecause is not possible to compile applications as root,\nthis installer create an aurbuilder user.\n\nInstall aurman ?") then
      clear
      checkaurdependencies
      cd /tmp/aurbuilder
      sudo -u aurbuilder git clone https://aur.archlinux.org/aurman.git
      cd aurman
      sudo -u aurbuilder gpg --recv-key 465022E743D71E39
      sudo -u aurbuilder makepkg -si
      #sudo -u aurbuilder makepkg -si --skippgpcheck
    fi
  ;;
  'Install yay')
    if(confirm "Yay allow you to access the AUR applications.\nBecause is not possible to compile applications as root,\nthis installer create an aurbuilder user.\n\nInstall yay ?") then
      clear
      checkaurdependencies
      cd /tmp/aurbuilder
      sudo -u aurbuilder git clone https://aur.archlinux.org/yay.git
      cd yay
      sudo -u aurbuilder makepkg -si
    fi
  ;;
  'Install yaourt')
    if(confirm "Yaourt allow you to access the AUR applications.\nBecause is not possible to compile applications as root,\nthis installer create an aurbuilder user.\n\nInstall yaourt ?") then
      clear
      checkaurdependencies
      checkpackagequerydependencies
      cd /tmp/aurbuilder
      sudo -u aurbuilder git clone https://aur.archlinux.org/yaourt.git
      cd yaourt
      sudo -u aurbuilder makepkg -si
    fi
  ;;
  'Install pkgstats')
    pacman -S --needed pkgstats
  ;;
  'Install aurvote')
    pacman -S --needed aurvote
  ;;
esac

exit 0