#!/bin/sh

timestamp=$( date +%F-%T )
desktop=$(ps -e | grep kwin)
# exclude Mac/Apple devices with their own rEfind from running bootctl
FSTAB=$(cat /etc/fstab | grep /boot)

add_privs() {
  if ! setcap "$2" "$1" 2>/dev/null; then
    echo "==> Warning: setcap failed, falling back to setuid root on /$1"
    chmod u+s "$1"
  fi
}

post_common() {
  systemd-machine-id-setup

  systemd-hwdb update
  journalctl --update-catalog

}

_242_changes() {
  if [[ -n ${desktop} ]]; then
  
        kdialog --yesno "Systemd 242 changed the reboot and shutdown service files.<br>
        First reboot after this update will have to be done manually.<br>
        <b>sudo reboot --force</b>, after this, normal shutdown/reboot will work again.<br><br>
        Do you want to save this output?"
        
        save=${?}

            if [[ ${save} == 1 ]]; then

                kdialog --msgbox "Output not saved"

            else

                echo "Systemd 242 changed the reboot and shutdown service files.<br>
                      First reboot after this update will have to be done manually.<br>
                      <b>sudo reboot --force</b>, after this, normal shutdown/reboot will work again." > /var/log/systemd_install${timestamp}.txt

                kdialog --msgbox "Output is saved in /var/log/systemd_install${timestamp}.txt"
                
            fi   

    else

        echo -e "Systemd 242 changed the reboot and shutdown service files."
        echo -e "First reboot after this update will have to be done manually."
        echo -e "<b>sudo reboot --force</b>, after this, normal shutdown/reboot will work again. \e[0m"

    fi
}

post_install() {
  post_common 

  # systemd-boot install
  if [ -d sys/firmware/efi/efivars ]; then
    /usr/bin/bootctl update
    echo ":: Run '/usr/bin/bootctl install' to make systemd-boot your default bootloader"
  fi
  mkdir -p /boot/efi/systemd-boot
}

post_upgrade() {
  post_common
  
  #if [[ $(vercmp 242-1 "$2") -eq 1 ]]; then
  #  _242_changes
  #fi
  
  if [ -d sys/firmware/efi/efivars ] && [ "$FSTAB" != "" ]; then
    echo ":: Updating bootctl"
    /usr/bin/bootctl update
  fi
}

