#!/system/bin/sh
# UltraOpt service.sh (AGGRESSIVE)
# - Generates/repairs mod.cfg
# - Starts hibernation daemon
# – Disable log

MODDIR="${0%/*}"

# Module name folder (adjust if yours differs)
MODNAME="UltrOpt"
CFG="/data/adb/modules/${MODNAME}/mod.cfg"
MARK="/data/adb/modules/${MODNAME}/.cfg_inited"
PIDFILE="/data/adb/modules/${MODNAME}/.hib.pid"
LOG="/data/local/tmp/UltraOpt_service.log"

HIB_BIN="${MODDIR}/system/bin/hibernation"
[ -x "$HIB_BIN" ] || HIB_BIN="hibernation"

log(){ echo "[UltraOpt][$(date '+%Y-%m-%d %H:%M:%S')] $*" >> "$LOG" 2>/dev/null; }

# Pick first governor from priority list that exists in available list
choose_gov(){
  local avail="$1"
  local prio="$2"
  local g
  for g in $prio; do
    echo " $avail " | grep -q " $g " && { echo "$g"; return 0; }
  done
  return 1
}

cpu_avail(){
  if [ -f /sys/devices/system/cpu/cpufreq/policy0/scaling_available_governors ]; then
    cat /sys/devices/system/cpu/cpufreq/policy0/scaling_available_governors 2>/dev/null; return
  fi
  if [ -f /sys/devices/system/cpu/cpu0/cpufreq/scaling_available_governors ]; then
    cat /sys/devices/system/cpu/cpu0/cpufreq/scaling_available_governors 2>/dev/null; return
  fi
  echo ""
}

cpu_cur(){
  if [ -f /sys/devices/system/cpu/cpufreq/policy0/scaling_governor ]; then
    cat /sys/devices/system/cpu/cpufreq/policy0/scaling_governor 2>/dev/null; return
  fi
  if [ -f /sys/devices/system/cpu/cpu0/cpufreq/scaling_governor ]; then
    cat /sys/devices/system/cpu/cpu0/cpufreq/scaling_governor 2>/dev/null; return
  fi
  echo ""
}

cfg_has_key(){ grep -q "^$1=" "$CFG" 2>/dev/null; }

cfg_set_if_missing(){
  local k="$1" v="$2"
  [ -z "$v" ] && return 0
  [ -f "$CFG" ] || return 1
  cfg_has_key "$k" && return 0
  echo "$k=$v" >> "$CFG"
  return 0
}

make_cfg(){
  umask 077
  mkdir -p "/data/adb/modules/${MODNAME}" 2>/dev/null

  local avail cur def slp ks
  avail="$(cpu_avail)"
  cur="$(cpu_cur)"

  # Priorities (no userspace, as you asked)
  local DEFAULT_PRIORITY="schedutil blu_schedutil ondemand interactive blu_active conservative smartmax alucard intelliactive intellimm smartassh3 smartassv2 zzmoove darkness lionheart yankactive interactive_pro interactivex impulse pegasusq nightmare relaxed barrier powersave performance"
  local SLEEP_PRIORITY="powersave conservative ondemand schedutil interactive blu_schedutil blu_active darkness zzmoove lionheart yankactive alucard smartmax smartassh3 smartassv2 intelliactive intellimm interactive_pro interactivex impulse pegasusq nightmare relaxed barrier"

  def="$(choose_gov "$avail" "$DEFAULT_PRIORITY" 2>/dev/null)" || def=""
  slp="$(choose_gov "$avail" "$SLEEP_PRIORITY" 2>/dev/null)" || slp=""

  [ -z "$def" ] && def="$cur"
  [ -z "$slp" ] && slp="$cur"

  {
    echo "# ================================"
    echo "# UltraOpt Configuration File"
    echo "# ================================"
    echo "# Auto-generated on $(date)"
    echo ""
    echo "# CPU governors"
    echo "DEFAULT_GOV=$def"
    echo "SLEEP_GOV=$slp"
    echo ""
    echo "# General"
    echo "CHECK_INTERVAL=5"
  } > "$CFG"
}

repair_cfg(){
  # Fill only missing keys

  cfg_set_if_missing "CHECK_INTERVAL" "5"

  # If governors missing, add from current/available
  if ! cfg_has_key "DEFAULT_GOV" || ! cfg_has_key "SLEEP_GOV"; then
    local avail cur def slp
    avail="$(cpu_avail)"
    cur="$(cpu_cur)"

    local DEFAULT_PRIORITY="schedutil blu_schedutil ondemand interactive blu_active conservative smartmax alucard intelliactive intellimm smartassh3 smartassv2 zzmoove darkness lionheart yankactive interactive_pro interactivex impulse pegasusq nightmare relaxed barrier powersave performance"
    local SLEEP_PRIORITY="powersave conservative ondemand schedutil interactive blu_schedutil blu_active darkness zzmoove lionheart yankactive alucard smartmax smartassh3 smartassv2 intelliactive intellimm interactive_pro interactivex impulse pegasusq nightmare relaxed barrier"

    def="$(choose_gov "$avail" "$DEFAULT_PRIORITY" 2>/dev/null)" || def=""
    slp="$(choose_gov "$avail" "$SLEEP_PRIORITY" 2>/dev/null)" || slp=""
    [ -z "$def" ] && def="$cur"
    [ -z "$slp" ] && slp="$cur"

    cfg_set_if_missing "DEFAULT_GOV" "$def"
    cfg_set_if_missing "SLEEP_GOV" "$slp"
  fi
}

start_daemon(){
  if [ ! -x "$HIB_BIN" ]; then
    log "hibernation binary not found/executable: $HIB_BIN"
    return 1
  fi

  # Kill stale
  if [ -f "$PIDFILE" ]; then
    oldpid="$(cat "$PIDFILE" 2>/dev/null)"
    [ -n "$oldpid" ] && kill "$oldpid" 2>/dev/null
    rm -f "$PIDFILE" 2>/dev/null
  fi

  "$HIB_BIN" >/dev/null 2>&1 &
  echo $! > "$PIDFILE" 2>/dev/null
  log "hibernation started (pid=$(cat "$PIDFILE" 2>/dev/null))"
  return 0
}

log "service start"

# Wait boot complete
until [ "$(getprop sys.boot_completed 2>/dev/null)" = "1" ]; do
  sleep 2
done
sleep 2

if [ ! -f "$CFG" ]; then
  log "config missing -> creating"
  make_cfg
  touch "$MARK" 2>/dev/null
else
  log "config exists -> repairing missing keys (safe)"
  repair_cfg
  touch "$MARK" 2>/dev/null
fi

start_daemon

pkill -9 statsd
pkill -9 incidentd

setprop ctl.stop statsd
setprop ctl.stop incidentd
setprop ctl.stop dumpstate

exit 0
