#!/bin/sh
#
# /etc/rc.d/rc.ympd
#

YMPD_USER="ympd"
YMPD_BIN="/usr/bin/ympd"
YMPD_PORT="8080"
MPD_HOST="localhost"
MPD_PORT="6600"

YMPD_CMD="$YMPD_BIN -h $MPD_HOST -p $MPD_PORT -w $YMPD_PORT"

ympd_start() {
  if [ -x "$YMPD_BIN" ]; then
    if pgrep -f "$YMPD_CMD$" > /dev/null; then
      echo "YMPD Instance for MPD Port $MPD_PORT is already running."
      return
    fi

    if [ -n "$YMPD_USER" ]; then
      echo "Starting YMPD for MPD $MPD_PORT as user: $YMPD_USER"
      su -s /bin/sh -c "$YMPD_CMD" "$YMPD_USER" > /dev/null 2>&1 &
    else 
      echo "Starting YMPD for MPD $MPD_PORT as user: $USER"
      su -s /bin/sh -c "$YMPD_CMD" > /dev/null 2>&1 &
    fi
  fi
}

ympd_stop() {
  echo "Stopping ympd daemon for MPD Port $MPD_PORT..."
  pkill -f "$YMPD_CMD$" 2> /dev/null
}

ympd_restart() {
  ympd_stop
  sleep 1
  ympd_start
}

case "$1" in
'start')   ympd_start ;;
'stop')    ympd_stop ;;
'restart') ympd_restart ;;
*) echo "usage $0 start|stop|restart" ;;
esac
