#!/bin/sh
# CronDeck installer: https://crondeck.vercel.app
#
#   curl -fsSL https://crondeck.vercel.app/install.sh | sh
#
# Debian, Ubuntu, Mint and other apt-based systems: adds the signed CronDeck
# apt repository and installs the "crondeck" package. Updates then come with
# `sudo apt upgrade`.
# Any other distro: installs the AppImage to ~/.local/bin/crondeck for the
# current user, with an app-menu entry. Run the script again to update.
#
# Set CRONDECK_METHOD=appimage to use the AppImage on an apt-based system too.

set -eu

REPO_URL="https://byabhisutar.github.io/crondeck/apt"
KEY_URL="$REPO_URL/crondeck.gpg"
KEYRING="/usr/share/keyrings/crondeck.gpg"
LIST="/etc/apt/sources.list.d/crondeck.list"
RELEASE_API="https://api.github.com/repos/byabhisutar/crondeck/releases/latest"
ICON_URL="https://crondeck.vercel.app/icon.svg"

say() { printf '\033[1;38;5;209m==>\033[0m %s\n' "$*"; }
die() { printf '\033[1;31merror:\033[0m %s\n' "$*" >&2; exit 1; }

need() { command -v "$1" >/dev/null 2>&1 || die "$1 is required but not installed."; }

as_root() {
  if [ "$(id -u)" -eq 0 ]; then "$@"
  elif command -v sudo >/dev/null 2>&1; then sudo "$@"
  else die "this step needs root. Install sudo, or run this script as root."
  fi
}

install_apt() {
  say "Adding the CronDeck apt repository (you may be asked for your password)"
  tmp="$(mktemp)"
  curl -fsSL "$KEY_URL" -o "$tmp" || die "could not download the signing key from $KEY_URL"
  as_root install -m 0644 "$tmp" "$KEYRING"
  rm -f "$tmp"
  echo "deb [arch=amd64 signed-by=$KEYRING] $REPO_URL stable main" | as_root tee "$LIST" >/dev/null

  say "Installing CronDeck"
  # A broken third-party repository elsewhere on the system shouldn't stop the install.
  as_root apt-get update || say "apt-get update reported problems with another repository; continuing"
  as_root apt-get install -y crondeck

  say "Done. Open CronDeck from your app menu, or run: crondeck"
  echo "    Updates arrive with: sudo apt update && sudo apt upgrade"
}

install_appimage() {
  bin="$HOME/.local/bin"
  apps="${XDG_DATA_HOME:-$HOME/.local/share}/applications"
  icons="${XDG_DATA_HOME:-$HOME/.local/share}/icons/hicolor/scalable/apps"
  mkdir -p "$bin" "$apps" "$icons"

  say "Finding the latest release"
  url="$(curl -fsSL "$RELEASE_API" | grep -o '"browser_download_url": *"[^"]*\.AppImage"' | head -n 1 | sed 's/.*"\(https[^"]*\)"/\1/')"
  [ -n "$url" ] || die "could not find an AppImage in the latest release."

  say "Downloading $(basename "$url")"
  curl -fL --progress-bar "$url" -o "$bin/crondeck.part" || die "download failed."
  chmod +x "$bin/crondeck.part"
  mv "$bin/crondeck.part" "$bin/crondeck"

  curl -fsSL "$ICON_URL" -o "$icons/crondeck.svg" 2>/dev/null || true
  cat > "$apps/crondeck.desktop" <<EOF
[Desktop Entry]
Type=Application
Name=CronDeck
Comment=See every cron job and systemd timer on your machine
Exec=$bin/crondeck
Icon=crondeck
Categories=Utility;System;
Terminal=false
EOF

  say "Done. Open CronDeck from your app menu, or run: $bin/crondeck"
  case ":$PATH:" in *":$bin:"*) ;; *) echo "    Tip: add $bin to your PATH to run it as just 'crondeck'.";; esac
  echo "    To update, run this installer again."
  echo "    If it won't start, install FUSE 2 (libfuse2 / fuse2) for AppImage support."
}

main() {
  [ "$(uname -s)" = "Linux" ] || die "CronDeck runs on Linux only."
  case "$(uname -m)" in
    x86_64|amd64) ;;
    *) die "CronDeck is only built for 64-bit x86 (amd64) so far. You have $(uname -m)." ;;
  esac
  need curl

  method="${CRONDECK_METHOD:-}"
  if [ -z "$method" ]; then
    if command -v apt-get >/dev/null 2>&1 && command -v dpkg >/dev/null 2>&1; then method=apt; else method=appimage; fi
  fi

  case "$method" in
    apt) install_apt ;;
    appimage) install_appimage ;;
    *) die "unknown CRONDECK_METHOD '$method' (use apt or appimage)." ;;
  esac
}

# Everything runs from main, so a half-downloaded script does nothing.
main "$@"
