#!/bin/sh set -eu KARLDNS_INSTALL_MODE="${KARLDNS_INSTALL_MODE:-auto}" KARLDNS_DELETE_CONFIG="${KARLDNS_DELETE_CONFIG:-0}" usage() { cat <<'EOF' KarlDNS Linux uninstaller Usage: uninstall-linux.sh [options] Options: --mode MODE auto, system, or user. Default: auto --delete-config Also delete saved private URLs and the last-IP cache -h, --help Show this help EOF } while [ "$#" -gt 0 ]; do case "$1" in --mode) KARLDNS_INSTALL_MODE="${2:?missing value for --mode}"; shift 2 ;; --delete-config) KARLDNS_DELETE_CONFIG=1; shift ;; -h|--help) usage; exit 0 ;; *) echo "KarlDNS uninstaller: unknown option: $1" >&2; usage >&2; exit 1 ;; esac done remove_user() { config_dir="${XDG_CONFIG_HOME:-$HOME/.config}/karldns" cache_dir="${XDG_CACHE_HOME:-$HOME/.cache}/karldns" updater_path="${HOME}/.local/bin/karldns-update" cron_file="$(mktemp)" if command -v crontab >/dev/null 2>&1; then crontab -l 2>/dev/null | awk ' $0 == "# karldns-update start" {skip = 1; next} $0 == "# karldns-update end" {skip = 0; next} skip != 1 {print} ' > "$cron_file" || true crontab "$cron_file" fi rm -f "$cron_file" "$updater_path" if [ "$KARLDNS_DELETE_CONFIG" = "1" ]; then rm -rf "$config_dir" "$cache_dir" fi } remove_marked_crontab_file() { cron_file="$1" [ -f "$cron_file" ] || return 0 tmp_file="$(mktemp)" awk ' $0 == "# karldns-update start" {skip = 1; next} $0 == "# karldns-update end" {skip = 0; next} skip != 1 {print} ' "$cron_file" > "$tmp_file" cat "$tmp_file" > "$cron_file" rm -f "$tmp_file" } remove_marked_root_crontab() { if ! command -v crontab >/dev/null 2>&1; then return 0 fi tmp_file="$(mktemp)" crontab -l 2>/dev/null | awk ' $0 == "# karldns-update start" {skip = 1; next} $0 == "# karldns-update end" {skip = 0; next} skip != 1 {print} ' > "$tmp_file" || true crontab "$tmp_file" rm -f "$tmp_file" } remove_system() { if [ "$(id -u)" -ne 0 ]; then echo "System uninstall needs root. Re-run with sudo or set --mode user." >&2 exit 1 fi rm -f /etc/cron.d/karldns-updater /usr/local/bin/karldns-update remove_marked_crontab_file /etc/crontabs/root remove_marked_root_crontab if [ "$KARLDNS_DELETE_CONFIG" = "1" ]; then rm -rf /etc/karldns /var/cache/karldns fi } case "$KARLDNS_INSTALL_MODE" in auto) if [ "$(id -u)" -eq 0 ]; then KARLDNS_INSTALL_MODE="system" else KARLDNS_INSTALL_MODE="user" fi ;; user|system) ;; *) echo "KARLDNS_INSTALL_MODE must be auto, user, or system" >&2; exit 1 ;; esac if [ "$KARLDNS_INSTALL_MODE" = "system" ]; then remove_system else remove_user fi echo "KarlDNS uninstalled from $KARLDNS_INSTALL_MODE mode." if [ "$KARLDNS_DELETE_CONFIG" = "0" ]; then echo "Saved private URLs were kept. Re-run with --delete-config to remove them." fi