#! /bin/sh
### BEGIN INIT INFO
# Provides:          piMood
# Required-Start:
# Required-Stop:
# Default-Start:     2 3 4 5
# Default-Stop:      0 1 6
# Short-Description: start and stop piMoo with gpio support
# Description:       starts and stops the piMoo cli player as a Daemon, for PI with gpio support using triggerhappy. Some extra handling is included: adding a new random song to playlist when calling "next"

# -- old SysV init script --
# -- must be placed at /etc/init.d/piMooPCN --

# for Debian users:
# copy this script to /etc/init.d/
# so you can easily control piMoo's cli_player.php with the runlevel editor "rcconf" [1]
# e.g. to make it automatically launch at system start
# or using mouse events to start and stop piMoo

# -- [1] since Debian "Trixie" use systemctl (rcconf is not available any longer) --
# cp more/runlevel_piMood /etc/init.d/piMood
# chmod u+x /etc/init.d/piMood
# -- required to make newly copied runlevel scripts accessible --
# systemctl daemon-reload
# -- then control like this --
# systemctl start piMood, systemctl status piMood or systemctl stop piMood
# -- making permanent --
# systemctl enable piMood or systemctl disable piMood

# /var/www/html/piMoo/cli_player.php &
# sudo -u www-data /var/www/html/piMoo/cli_player.php &
# this needs the www-data user to contain the group [x]audio


### END INIT INFO
# Author: Name <info@craft4fun.de>


LOGFILE="/var/log/piMoo.log"

# actions
case "$1" in

    # -- ---------------------- --
    # -- for piMoo's own (web) control --

    run)
      # if [ ! "$(pidof mpg123)" ]
      if [ -z "$(pidof mpg123)" ] && [ -z "$(pidof mpv)" ];
      then
        sudo -u www-data /var/www/html/piMoo/cli_player.php &
        sudo -u www-data /var/www/html/piMoo/cli_common.php &
      fi
    ;;

    break)
      pkill -x cli_player.php
      pkill -x cli_common.php
      pkill -x mpg123
      pkill -x mpv

      # Using multiple pkill lines like this, even if only one of those processes is running, does no harm:
      # - If the process isn't running, pkill -x just exits with status 1 and moves on.
      # - There's no error, no crash, no side effects.
      # - This way, you don't have to check "which one is running" beforehand — it's clean and simple.

      # -- fancy, not really required (so only the really existing is trying to be killed) --
      # for proc in cli_player.php cli_common.php mpg123 mpv; do
      #     pkill -x "$proc"
      # done
    ;;



    # -- -------------------------- --
    # -- for init runlevel handling --

    start)
      if [ -f /etc/rpi-issue ] # -- if running on raspberry pi, also care about gpios
      then
        # gpio -g mode 17 out
        # gpio -g write 17 1

        # -- check if pigs is installed at all, because it could run on Raspberry without gpios set up --
        if [ -x "$(command -v pigs)" ]; then
          sudo -u www-data /var/www/html/piMoo/cli_ext_gpio_on.php
        fi
      fi

      sudo -u www-data /var/www/html/piMoo/cli_ext_setPlayerState_playing.php
      # if [ ! "$(pidof mpg123)" ]
      if [ -z "$(pidof mpg123)" ] && [ -z "$(pidof mpv)" ];
      then
        sudo -u www-data /var/www/html/piMoo/cli_player.php &
        sudo -u www-data /var/www/html/piMoo/cli_common.php &
      fi
    ;;

    stop)
      if [ -f /etc/rpi-issue ] # -- if running on raspberry pi, also care about gpios
      then
        # gpio -g write 17 0
        # gpio -g mode 17 in

        # -- check if pigs is installed at all, because it could run on Raspberry without gpios set up --
        if [ -x "$(command -v pigs)" ]; then
          sudo -u www-data /var/www/html/piMoo/cli_ext_gpio_off.php
        fi
      fi

      sudo -u www-data /var/www/html/piMoo/cli_ext_setPlayerState_off.php
      sudo -u www-data pkill -x cli_player.php
      sudo -u www-data pkill -x cli_common.php
      sudo -u www-data pkill -x mpg123
      sudo -u www-data pkill -x mpv
    ;;

  restart)
    echo "$(date): Restarting piMoo backend player..." >> "$LOGFILE"
    $0 stop >> "$LOGFILE" 2>&1
    if [ $? -ne 0 ]; then
        echo "$(date): Failed to stop piMoo backend player." >> "$LOGFILE"
        exit 1
    fi

    sleep 2
    $0 start >> "$LOGFILE" 2>&1
    if [ $? -ne 0 ]; then
        echo "$(date): Failed to start piMoo backend player after restart." >> "$LOGFILE"
        exit 1
    fi

    echo "$(date): Restart complete." >> "$LOGFILE"
    ;;



    # -- ------------------------------------------- --
    # -- for special handling like external triggers --

    next)
      if [ "$(pgrep cli_player.php)" ]  # -- if player is playing: call next song ... --
      then
        # for every "next" call a new song must be added to the playlist ...
        sudo -u www-data /var/www/html/piMoo/cli_ext_song_addRandom.php
        # ... now call next
        sudo -u www-data pkill -USR1 -x mpg123
        sudo -u www-data pkill -USR1 -x mpv
      else  # -- ... if not, shutdown --
        # nonsense (after reboot this file is gone): sudo -u www-data /var/www/html/piMoo/cli_ext_setPlayerState_off.php
        sudo -u www-data /var/www/html/piMoo/cli_ext_shutdown.php # -- worked before: sudo /sbin/shutdown -h now --
      fi
    ;;



    *)
      echo "Usage: /etc/init.d/piMood-pi {start|next|stop|run|break|restart}"
      exit 1
    ;;

esac

exit 0

