#!/bin/sh
#
# misery-daemon - pornOS init script (runs under porn-init, NOT systemd)
#
# pornOS has no systemd. Its root init (porn-init) launches services from
# /etc/init.d. This script plays "Misery" by Pupsies every 20 minutes so you
# truly feel the weight of the distro.
#
# Usage: /etc/init.d/misery-daemon {start|stop|restart|status}
#

DAEMON=/usr/bin/misery-daemon
PIDFILE=/run/misery-daemon.pid

start() {
    if [ -f "$PIDFILE" ] && kill -0 "$(cat "$PIDFILE")" 2>/dev/null; then
        echo "  misery is already playing."
        return 0
    fi
    echo "  starting misery daemon..."
    "$DAEMON" &
    echo $! > "$PIDFILE"
    echo "  misery enabled."
}

stop() {
    if [ -f "$PIDFILE" ]; then
        kill "$(cat "$PIDFILE")" 2>/dev/null
        rm -f "$PIDFILE"
        echo "  misery stopped. you almost feel at peace."
    else
        echo "  misery not running."
    fi
}

restart() { stop; start; }

status() {
    if [ -f "$PIDFILE" ] && kill -0 "$(cat "$PIDFILE")" 2>/dev/null; then
        echo "  misery daemon running (pid $(cat "$PIDFILE"))."
    else
        echo "  misery daemon not running."
    fi
}

case "${1:-start}" in
    start)   start ;;
    stop)    stop ;;
    restart) restart ;;
    status)  status ;;
    *) echo "usage: $0 {start|stop|restart|status}" ;;
esac
