#!/bin/sh
### BEGIN INIT INFO
# Provides:          aftertouch-service
# Required-Start:    $network $local_fs
# Required-Stop:     $network $local_fs
# Default-Start:     2 3 4 5
# Default-Stop:      0 1 6
# Short-Description: Run AfterTouch on this device
# Description:       Start/stop AfterTouch soundtouch-service
### END INIT INFO


NAME="aftertouch-service"
DESC="Bose AfterTouch service"
DAEMON="/opt/aftertouch/aftertouch-service"
PIDFILE="/var/run/$NAME.pid"
DATADIR="/opt/aftertouch/data"
SCRIPTNAME="/etc/init.d/$NAME"
USER="root"
LOG_TAG="aftertouch"


# Export PATH
export PATH="/usr/local/sbin:/usr/local/bin:/sbin:/bin:/usr/sbin:/usr/bin"


# Sanity check executable
test -x "$DAEMON" || {
  echo "ERROR: Cannot execute $DAEMON (check path and permissions)." >&2
  exit 1
}


case "$1" in
  start)
    echo "Starting $DESC..."

    mount -o remount,rw / >/dev/null 2>&1 || {
      echo "ERROR: remount failed." >&2
      exit 1
    }

    mkdir -p "$DATADIR"

    # Pipe stdout + stderr through `logger -t $LOG_TAG` so the
    # daemon's output lands in busybox syslog (bounded ring buffer,
    # never grows on disk). Users diagnose with:
    #
    #   logread        | grep aftertouch | tail -20
    #   logread -f     | grep aftertouch     # live tail
    #
    # `exec` on the daemon replaces /bin/sh so --make-pidfile records
    # the daemon's own PID (not the shell wrapper). The `logger`
    # process sits on the read end of the pipe and exits cleanly
    # when the daemon dies and closes its end.
    start-stop-daemon --start \
        --quiet \
        --pidfile "$PIDFILE" \
        --background \
        --make-pidfile \
        --chuid "$USER" \
        --startas "/bin/sh" \
        -- -c "exec \"$DAEMON\" --data-dir '$DATADIR' --record-interactions=false --discovery-interval=60m 2>&1 | logger -t $LOG_TAG"

    tries=0
    max_tries=60
    while [ $tries -lt $max_tries ]; do
        if curl -fsS http://localhost:8000 >/dev/null 2>&1; then
          exit 0
        fi
        sleep 2
        tries=$((tries + 1))
    done

    echo "ERROR: daemon started but http://localhost:8000 never responded within $((max_tries * 2))s." >&2
    echo "       Inspect the daemon's syslog output:" >&2
    echo "         logread | grep $LOG_TAG | tail -20" >&2
    exit 1
    ;;

  stop)
    echo "Stopping $DESC..."
    if [ -f "$PIDFILE" ]; then
      PID=$(cat "$PIDFILE")
      start-stop-daemon --stop \
        --quiet \
        --oknodo \
        --pidfile "$PIDFILE"
      # Wait up to 15 s for SIGTERM to take effect before escalating.
      # The Go HTTP server exits promptly on SIGTERM in normal conditions;
      # the loop handles the rare case where it is stuck in a blocking syscall.
      tries=0
      while [ $tries -lt 15 ] && kill -0 "$PID" 2>/dev/null; do
        sleep 1
        tries=$((tries + 1))
      done
      if kill -0 "$PID" 2>/dev/null; then
        echo "Warning: $NAME (PID $PID) still alive after ${tries}s; sending SIGKILL..." >&2
        kill -9 "$PID" 2>/dev/null || true
        sleep 1
      fi
      rm -f "$PIDFILE"
    else
      echo "No $NAME running (no PID file)." >&2
    fi
    ;;

  restart|force-reload)
    "$0" stop
    sleep 2
    "$0" start
    ;;

  status)
    if [ -f "$PIDFILE" ]; then
      PID=$(cat "$PIDFILE")
      if kill -0 "$PID" 2>/dev/null; then
        # PID is alive — does it actually serve HTTP? A live process
        # with a dead listener is the symptom behind issue #250
        # (Gustour's ST30: status said running, curl said
        # connection-refused). Distinguish the two states here so
        # status isn't a false-positive.
        if curl -fsS --max-time 3 http://localhost:8000 >/dev/null 2>&1; then
          echo "$NAME is running (PID $PID, http://localhost:8000 responding)."
          exit 0
        else
          echo "$NAME PID $PID is alive but http://localhost:8000 is not responding." >&2
          echo "Recent log:" >&2
          logread 2>/dev/null | grep "$LOG_TAG" | tail -10 >&2
          exit 3
        fi
      else
        echo "$NAME is not running (PID file exists but process is dead)." >&2
        exit 1
      fi
    else
      echo "$NAME is not running."
      exit 3
    fi
    ;;

  *)
    echo "Usage: $SCRIPTNAME {start|stop|restart|force-reload|status}"
    exit 1
    ;;
esac


exit 0
