#!/bin/sh
#
# nextcloud-notify-push startup script for Slackware Linux

# Source function library.
. /etc/rc.d/init.d/functions

NOTIFY_PUSH=/usr/bin/notify_push
PIDFILE=/var/run/nextcloud-notify-push.pid
LOGFILE=/var/log/nextcloud-notify-push.log
CONFFILE=/etc/nextcloud/config.php
PARAMS="--no-ansi"
USER=apache

# Load configuration
if [ -r /etc/default/nextcloud-notify-push ]; then
  . /etc/default/nextcloud-notify-push
fi

nextcloud_notify_push_start() {
  if [ -s $PIDFILE ]; then
    echo "nextcloud-notify-push appears to be already running?"
    exit 1
  fi

  if ! [ -e "$LOGFILE" ]; then
    touch $LOGFILE
    chown $USER:root $LOGFILE
  fi

  echo -n "Starting nextcloud-notify-push server..."
  su -l -c "$NOTIFY_PUSH $PARAMS $CONFFILE &> $LOGFILE &" $USER
  RETVAL=$?
  echo "ok"

  pidof notify_push > $PIDFILE

  return $RETVAL
}

nextcloud_notify_push_stop() {
  if [ ! -s $PIDFILE ]; then
    echo "$PIDFILE does not exist or is empty."
    exit 1
  fi

  echo -n "Stopping nextcloud-notify-push server..."
  killproc -p $PIDFILE notify_push
  RETVAL=$?
  echo
  [ $RETVAL = 0 ] && rm -f $PIDFILE
}

nextcloud_notify_push_restart() {
  nextcloud_notify_push_stop
  sleep 3
  nextcloud_notify_push_start
}

case "$1" in
  start)
    nextcloud_notify_push_start
    ;;
  stop)
    nextcloud_notify_push_stop
    ;;
  restart)
    nextcloud_notify_push_restart
    ;;
  *)
    echo "Usage: $0 {start|stop|restart}"
    exit 1
esac
