#!/bin/bash
# Licensed to the Apache Software Foundation (ASF) under one or more
# contributor license agreements.  See the NOTICE file distributed with
# this work for additional information regarding copyright ownership.
# The ASF licenses this file to You under the Apache License, Version 2.0
# (the "License"); you may not use this file except in compliance with
# the License.  You may obtain a copy of the License at
#
#     http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

### BEGIN INIT INFO
# Provides:        ranger-usersync
# Required-Start:  $local_fs $remote_fs $network $named $syslog $time
# Required-Stop:   $local_fs $remote_fs $network $named $syslog $time
# Default-Start:   2 3 4 5
# Default-Stop:
# Short-Description: Start/Stop Ranger Usersync
### END INIT INFO

NAME='Ranger Usersync Service'
BIN_PATH=/usr/bin
MOD_NAME=ranger-usersync
USER='{{user.name}}'
CONF_DIR='{{conf_dir}}'
PID_FILE='/var/run/ranger/usersync.pid'
CMD="/bin/su --login  $USER -c \"${BIN_PATH}/${MOD_NAME} start\""
KILL_FORCE=0
KILL_SLEEP=10

function start {
  if [ -f $PID_FILE ]; then
    pid=`cat $PID_FILE`
    if kill -0 $pid >/dev/null 2>&1 $pid; then
       echo "$NAME already running [$pid]"
       exit 0
    else
      rm -rf $PID_FILE
    fi
  fi
  echo "Starting Ranger Usersync."
  eval $CMD
  echo "$NAME running [`cat $PID_FILE`]"
}

function stop {
  if [ ! -f $PID_FILE ]; then
    echo "$NAME already stopped"
    return
  fi

  pid=`cat $PID_FILE`
  echo "Stopping Ranger Usersync."
  /bin/su --login  $LINUX_USER -c "${BIN_PATH}/${MOD_NAME} stop"
  # pid=$(<$PID_FILE)
  i=0
  while kill -0 $pid 2>/dev/null && [ $i -lt $KILL_SLEEP ]; do
    (( i++ ))
    sleep 1
  done
  if ! kill -0 $pid 2>/dev/null; then
    rm  $PID_FILE 2>/dev/null || true
    echo "$NAME stopped"
    return
  fi
  force_stop
}

function force_stop {
  pid=$(<$PID_FILE)
  kill -9 $pid 2>/dev/null
  sleep 1
  if kill -0 $pid; then
    echo "$NAME failed to stop"
    exit 1
  fi
  rm $PID_FILE
  echo "$NAME forced stopped after ${KILL_SLEEP}s"
}

function status {
  if [ -f $PID_FILE ]; then
    pid=`cat $PID_FILE`
    if kill -0 >/dev/null 2>&1 $pid; then
       echo "$NAME started [$pid]"
       exit 0
    fi
  fi
  echo "$NAME stopped"
  exit 3
}

if [[ $1 == "start" ]]; then
  start "$@"
elif [[ $1 == "stop" ]]; then
  stop "$@"
elif [[ $1 == "restart" ]]; then
  stop "$@"
  sleep 1
  start "$@"
elif [[ $1 == "status" ]]; then
  status "$@"
else
  N=/etc/init.d/$NAME
  echo "Usage: $N {start|stop|restart|status}" >&2
fi
