#!/bin/bash
# alpha-cacheengine — install / enable / disable / remove cache engines from the
# panel. Root-owned; called by www-data via NOPASSWD sudoers. Heavy/JVM engines
# are refused (guided instead); apt-installable ones run detached + logged.
#     install <engine>   apt-install a supported engine
#     enable  <engine>   systemctl enable --now <service>
#     disable <engine>   systemctl disable --now <service>
#     remove  <engine>   apt purge <engine>
set -eu
ACTION="${1:-}"; ENGINE="${2:-}"
LOG=/var/log/alpha-cacheengine.log
[[ "$ENGINE" =~ ^[a-z]+$ ]] || { echo "BAD_ENGINE: $ENGINE"; exit 2; }

svc_of() { case "$1" in redis) echo redis-server;; memcached) echo memcached;; tarantool) echo tarantool;; aerospike) echo aerospike;; ignite) echo ignite;; hazelcast) echo hazelcast;; *) echo "";; esac; }
pkg_of() { case "$1" in redis) echo redis-server;; memcached) echo memcached;; tarantool) echo tarantool;; *) echo "";; esac; }

case "$ACTION" in
  enable|disable)
    SVC=$(svc_of "$ENGINE")
    [ -n "$SVC" ] || { echo "NO_SERVICE: $ENGINE has no manageable service"; exit 3; }
    systemctl list-unit-files "${SVC}.service" 2>/dev/null | grep -q "$SVC" || { echo "NOT_INSTALLED: $ENGINE"; exit 5; }
    if [ "$ACTION" = enable ]; then systemctl enable --now "$SVC" >/dev/null 2>&1 && echo "OK: $ENGINE enabled + started"
    else systemctl disable --now "$SVC" >/dev/null 2>&1 && echo "OK: $ENGINE disabled + stopped"; fi
    ;;
  install)
    case "$ENGINE" in
      caffeine) echo "LIBRARY: Caffeine is an in-process Java library — add it to your app's build, nothing to install here"; exit 7 ;;
      ignite|hazelcast) echo "MANUAL: $ENGINE is a JVM platform — install a JDK + the official distribution/cluster config, not a single apt package"; exit 7 ;;
      aerospike) echo "MANUAL: Aerospike installs from its own Community repo/.deb (heavy) — not in the base apt sources"; exit 7 ;;
      redis|memcached|tarantool)
        PKG=$(pkg_of "$ENGINE")
        # already present?
        dpkg-query -W -f='${db:Status-Abbrev}' "$PKG" 2>/dev/null | grep -q '^ii' && { echo "OK: $ENGINE already installed"; exit 0; }
        CAND=$(apt-cache policy "$PKG" 2>/dev/null | awk '/Candidate:/{print $2; exit}')
        { [ -n "$CAND" ] && [ "$CAND" != "(none)" ]; } || { echo "NO_CANDIDATE: $PKG is not available from apt on this system"; exit 6; }
        setsid bash -c "{ echo \"== \$(date -Is) install $ENGINE\"; DEBIAN_FRONTEND=noninteractive apt-get update -q || true; DEBIAN_FRONTEND=noninteractive apt-get install -y -q $PKG; RC=\$?; [ \"\$RC\" = 0 ] && systemctl enable --now $(svc_of "$ENGINE"); echo done rc=\$RC; } >> $LOG 2>&1" </dev/null &
        echo "STARTED: installing $ENGINE (background, ~1 min)" ;;
      *) echo "UNKNOWN_ENGINE: $ENGINE"; exit 1 ;;
    esac ;;
  remove)
    PKG=$(pkg_of "$ENGINE")
    [ -n "$PKG" ] || { echo "NOT_REMOVABLE: $ENGINE is not an apt-managed engine here"; exit 5; }
    dpkg-query -W -f='${db:Status-Abbrev}' "$PKG" 2>/dev/null | grep -q '^ii' || { echo "NOT_INSTALLED: $ENGINE"; exit 5; }
    setsid bash -c "{ echo \"== \$(date -Is) remove $ENGINE\"; systemctl disable --now $(svc_of "$ENGINE") 2>/dev/null; DEBIAN_FRONTEND=noninteractive apt-get purge -y -q $PKG; DEBIAN_FRONTEND=noninteractive apt-get autoremove -y -q; echo done rc=\$?; } >> $LOG 2>&1" </dev/null &
    echo "STARTED: removing $ENGINE (background)" ;;
  *) echo "BAD_ACTION: $ACTION"; exit 1 ;;
esac
