#! /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"

# 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"
# e.g. to make it automatically launch at system start
# or using mouse events to start and stop piMoo

# /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>

# Aktionen
case "$1" in

    # -- ---------------------- --
    # -- for piMoo's own control --

    run)
        if [ ! "$(pidof mpg123)" ]
        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
    ;;



    # -- -------------------------- --
    # -- 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)" ]
      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
    ;;



    # -- ------------------------------------------- --
    # -- 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
      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}"
      exit 1
    ;;

esac

exit 0

