#!/bin/sh
#
# chkconfig: 2345 20 80
# description: Starts and stops OneDrive Client for Linux
#

### BEGIN INIT INFO
# Provides: postfix $mail-transfer-agent
# Required-Start: $local_fs $network $remote_fs
# Required-Stop: $local_fs $network $remote_fs
# Short-Description: start and stop OneDrive Client
# Description: OneDrive Client for Linux.
### END INIT INFO

# Source function library.
if [ -f /etc/init.d/functions ] ; then
  . /etc/init.d/functions
elif [ -f /etc/rc.d/init.d/functions ] ; then
  . /etc/rc.d/init.d/functions
else
  exit 1
fi

# Source networking configuration.
. /etc/sysconfig/network

# Source Onedrive configuration.
. /etc/sysconfig/onedrive

# Check that networking is up.
[ ${NETWORKING} = "no" ] && exit 1

APP_NAME="OneDrive Client for Linux"
STOP_TIMEOUT=${STOP_TIMEOUT-5}
RETVAL=0

start() {
        echo -n "Starting ${APP_NAME}: "
        daemon --user root onedrive "${APP_OPTIONS}" > /dev/null 2>&1 &
        RETVAL=$?
        echo
        [ $RETVAL -eq 0 ] && touch /var/lock/subsys/onedrive || \
           RETVAL=1
        return $RETVAL
}

stop() {
        echo -n "Shutting down $APP_NAME: "
        killproc onedrive
        RETVAL=$?
        echo
        [ $RETVAL = 0 ] && rm -f /var/lock/subsys/onedrive ${pidfile}
}

restart() {
        stop
        start
}

rhstatus() {
        status onedrive
        return $?
}

# Allow status as non-root.
if [ "$1" = status ]; then
       rhstatus
       exit $?
fi

case "$1" in
  start)
        start
        ;;
  stop)
        stop
        ;;
  restart)
        restart
        ;;
  reload)
        reload
        ;;
  status)
        rhstatus
        ;;
  *)
        echo "Usage: $0 {start|stop|restart|reload|status}"
        exit 2
esac

exit $?
