#!/usr/bin/env sh
set -eu

DAY_SHIFT_COMMAND_NAME='day-shift'
DAY_SHIFT_INSTALL_DIR=${DAY_SHIFT_INSTALL_DIR:-$HOME/.local/day-shift}
DAY_SHIFT_BIN_DIR=${DAY_SHIFT_BIN_DIR:-$HOME/.local/bin}

desktop_module="$DAY_SHIFT_INSTALL_DIR/current/dist/lib/launcher/desktop-entry.js"
if [ -f "$desktop_module" ]; then
  desktop_node=$(cat "$DAY_SHIFT_INSTALL_DIR/current/.day-shift-desktop-node")
  current_dir=$(CDPATH= cd -- "$DAY_SHIFT_INSTALL_DIR/current" && pwd -P)
  "$desktop_node" "$desktop_module" remove "$current_dir" || { printf 'Desktop removal failed; installation retained. Restore the pinned Node runtime and retry.\n' >&2; exit 1; }
elif [ -f "$DAY_SHIFT_INSTALL_DIR/current/.day-shift-desktop-node" ]; then
  printf 'Desktop helper is missing; restore the package before removal.\n' >&2
  exit 1
fi

rm -f "$DAY_SHIFT_BIN_DIR/$DAY_SHIFT_COMMAND_NAME"
rm -rf "$DAY_SHIFT_INSTALL_DIR"
printf 'Removed Day Shift installation directory: %s\n' "$DAY_SHIFT_INSTALL_DIR"
printf 'Removed command shim or symlink: %s\n' "$DAY_SHIFT_BIN_DIR/$DAY_SHIFT_COMMAND_NAME"
