#!/usr/bin/env bash
#
# auto-cpufreq daemon removal script
# reference: https://codeberg.org/Zamanhuseyinli/auto-cpufreq-rust
# Thanks to https://github.com/errornonamer for openrc fix
set -uo pipefail

MID="$((`tput cols` / 2))"
echo
printf "%0.s─" $(seq $(( (MID-2) / 2 )))
printf " Running auto-cpufreq daemon removal script "
printf "%0.s─" $(seq $(( (MID-2) / 2 )))
echo; echo

# root check
if ((EUID != 0)); then
  echo; echo "Must be run as root (i.e: 'sudo $0')."; echo
  exit 1
fi

REPO="Zamanhuseyinli/auto-cpufreq-rust"

# First argument is the init name, second argument is the stop command,
# third argument is the disable command, and remaining arguments are the
# paths to remove.
function auto_cpufreq_remove {
    local init_name="$1" stop_cmd="$2" disable_cmd="$3"
    shift 3

    if [ -n "$stop_cmd" ]; then
      echo -e "\n* Stopping auto-cpufreq daemon (${init_name}) service"
      eval "$stop_cmd" || true
    fi

    if [ -n "$disable_cmd" ]; then
      echo -e "\n* Disabling auto-cpufreq daemon (${init_name}) at boot"
      eval "$disable_cmd" || true
    fi

    if [ "$#" -gt 0 ]; then
      echo -e "\n* Removing auto-cpufreq daemon (${init_name}) unit file"
      # "$@" is the literal path list - quoted properly, no word-splitting
      # surprises, and missing files are silently ignored rather than
      # spamming a scary-looking (but harmless) "No such file" error.
      rm -rf -- "$@"
    fi
}

case "$(ps h -o comm 1)" in
  dinit)
    auto_cpufreq_remove "dinit" \
      "dinitctl stop auto-cpufreq" \
      "dinitctl disable auto-cpufreq" \
      "/etc/dinit.d/auto-cpufreq"
    ;;
  init)
    auto_cpufreq_remove "openrc" \
      "rc-service auto-cpufreq stop" \
      "rc-update del auto-cpufreq default" \
      "/etc/init.d/auto-cpufreq"
    ;;
  runit)
    # First argument is the "sv" path, second argument is the "service" path
    rm_sv() {
      auto_cpufreq_remove "runit" \
        "sv stop auto-cpufreq" \
        "" \
        "$1/sv/auto-cpufreq" "$2/service/auto-cpufreq"
    }
    if [ -f /etc/os-release ]; then
      . /etc/os-release
      case $ID in
        void) rm_sv /etc /var;;
        artix) rm_sv /etc/runit /run/runit;;
        *)
          echo -e "\n* Runit init detected but your distro is not supported\n"
          echo -e "\n* Please open an issue on https://codeberg.org/${REPO}\n"
        ;;
      esac
    fi
  ;;
  systemd)
    auto_cpufreq_remove "systemd" \
      "systemctl stop auto-cpufreq" \
      "systemctl disable auto-cpufreq" \
      "/etc/systemd/system/auto-cpufreq.service"
    echo -e "\n* Reloading systemd manager configuration"
    systemctl daemon-reload || true
    echo "reset failed"
    systemctl reset-failed || true
  ;;
  s6-svscan)
    auto_cpufreq_remove "s6" \
      "" \
      "s6-service delete default auto-cpufreq" \
      "/etc/s6/sv/auto-cpufreq"

    echo -e "\n* Update daemon service bundle (s6)"
    s6-db-reload || true
  ;;
  *)
    echo -e "\n* Unsupported init system detected, could not remove the daemon"
    echo -e "\n* Please open an issue on https://codeberg.org/${REPO}\n"
  ;;
esac

# ---------------------------------------------------------------------------
# Full removal - wipe everything the install script put on disk, not just
# the init unit. This mirrors every location auto-cpufreq-install.sh writes
# to, so `remove` leaves no trace behind.
# ---------------------------------------------------------------------------
echo -e "\n* Removing auto-cpufreq binaries"
for bin in auto-cpufreq auto-cpufreq-gtk auto-cpufreq-tray; do
  rm -f -- "/usr/local/bin/${bin}"
  # only remove /usr/bin/<name> if it's actually a symlink to our binary,
  # never if it turned out to be a real file (e.g. a distro package)
  if [ -L "/usr/bin/${bin}" ]; then
    rm -f -- "/usr/bin/${bin}"
  fi
done

echo -e "\n* Removing cpufreqctl helper"
rm -f -- "/usr/local/bin/cpufreqctl.auto-cpufreq"

echo -e "\n* Removing scripts and images directories"
rm -rf -- "/usr/local/share/auto-cpufreq"
# /usr/local/scripts is expected to be a symlink to the dir just removed
if [ -L "/usr/local/scripts" ]; then
  rm -f -- "/usr/local/scripts"
elif [ -d "/usr/local/scripts" ]; then
  # leftover real directory from a pre-symlink install - remove it too
  rm -rf -- "/usr/local/scripts"
fi

echo -e "\n* Removing desktop entry, polkit policy, and udev rule"
rm -f -- "/usr/share/applications/auto-cpufreq-gtk.desktop"
rm -f -- "/usr/share/polkit-1/actions/org.auto-cpufreq.pkexec.policy"
if [ -f "/etc/udev/rules.d/50-auto-cpufreq.rules" ]; then
  rm -f -- "/etc/udev/rules.d/50-auto-cpufreq.rules"
  udevadm control --reload-rules 2>/dev/null || true
fi

echo
echo "* auto-cpufreq daemon removal complete."
echo
