post_install() {
  echo -e "\e[1;33m==>\e[0m Read setup instructions at \e[1;36mhttps://wiki.archlinux.org/index.php/Pi-hole\e[0m"

  # pre apply sys user creation and rights
  /usr/bin/systemd-sysusers > /dev/null 2>&1
  /usr/bin/chown -R pihole:pihole /etc/pihole

  # /etc/pihole/versions file generation
  echo -e "\e[1;33m==>\e[0m Creating 'versions' file...\e[0m"
  sudo -u pihole bash /opt/pihole/updatecheck.sh;

  # the gravity timer is not enabled by default
  echo -e "\e[1;33m==>\e[0m For weekly blocklist updates: \e[1;36msystemctl enable --now pi-hole-gravity.timer\e[0m"
}

post_upgrade() {
  # clean old/unused/deprecated files
  [ -e /etc/pihole/.useIPv6 ] && rm /etc/pihole/.useIPv6
  [ -e /etc/pihole/adlists.list ] && rm /etc/pihole/adlists.list
  [ -e /etc/pihole/dns-servers.conf ] && rm /etc/pihole/dns-servers.conf

  # /etc/pihole/versions file generation
  echo -e "\e[1;33m==>\e[0m Updating 'versions' file...\e[0m"
  sudo -u pihole bash /opt/pihole/updatecheck.sh;

  # 6.5-4 and older shipped an enable symlink: keep the timer on for those installs
  if [ "$(vercmp "$2" 6.5-5)" -lt 0 ] &&
     [ "$(systemctl is-enabled pi-hole-gravity.timer 2>/dev/null)" != masked ]; then
    systemctl enable pi-hole-gravity.timer
  fi

  # keep pacman from throwing errors should none them be on the filesystem
  return 0
}

post_remove() {
  # Do not delete anything in /etc/pihole and do not touch the pihole user:
  # the config/data in there may have been created or changed by the user and
  # the user itself is managed by the pi-hole-ftl package (sysusers).
  if ! rmdir /etc/pihole 2>/dev/null && [[ -d /etc/pihole ]]; then
    echo -e "\e[1;33m==>\e[0m Your Pi-hole configuration and databases were left untouched in \e[1;36m/etc/pihole\e[0m."
    echo -e "\e[1;33m==>\e[0m Remove them manually if you no longer need them:"
    echo -e "      \e[1;36mrm -rf /etc/pihole\e[0m"
  fi

  # keep pacman from throwing errors should none them be on the filesystem
  return 0
}
