#!/bin/sh
### BEGIN INIT INFO
# Provides: resilio-connect-tracker
# Required-Start: $local_fs $remote_fs
# Required-Stop: $local_fs $remote_fs
# Should-Start: $network
# Should-Stop: $network
# Default-Start: 2 3 4 5
# Default-Stop: 0 1 6
# Short-Description: daemonized version of Resilio Connect Tracker
# Description: Starts the resilio-connect-tracker daemon
### END INIT INFO

set -e

USER="rsltracker"
PKG_NAME="resilio-connect-tracker"
PRODUCT_NAME="Resilio Connect Tracker"
BIN_NAME="tracker"
BIN_DIR="/opt/${PKG_NAME}"
BIN_PATH="${BIN_DIR}/${BIN_NAME}"
PID_FILE="/var/run/${BIN_NAME}.pid"
DAEMON="${BIN_PATH} -p 3000"

START_DEB_SYS_CMD="start-stop-daemon --start --background --make-pidfile --pidfile ${PID_FILE} --name ${USER} --chuid ${USER} --user ${USER} --umask ${UMASK} --exec ${DAEMON}"
STOP_DEB_SYS_CMD="start-stop-daemon --oknodo --stop --quiet --pidfile ${PID_FILE} --name ${USER}"

START_RPM_SYS_CMD="daemon --user ${USER} ${BIN_PATH}"
STOP_RPM_SYS_CMD="killproc ${BIN_NAME}"

START_CMD=""
STOP_CMD=""

deprecation_warning()
{
    COMMAND=$1
    printf "\033[1;33mWARNING: init.d script is deprecated and may be removed in a future release.\033[0m\n"
    printf "\033[1;33mPlease use '%s' instead.\033[0m\n" "${COMMAND}"
    printf "\033[1;33mGet help: https://helpfiles.resilio.com/ConnectTicket\033[0m\n"
}

check_superuser_permissions() {
    if [ "$(id -u)" -ne 0 ]; then
        if ! which sudo >/dev/null 2>&1; then
            run_as="root"
        else
            run_as="root or via sudo"
        fi
        echo "This script must be run as ${run_as}" 1>&2
        deprecation_warning systemctl
        exit 1
    fi
}

import_initd_functions()
{
    # the only crossplatform way to check if file exists
    ls /lib/lsb/init-functions >/dev/null 2>&1
    if [ $? = 0 ]; then
        . /lib/lsb/init-functions
    fi
    ls /etc/init.d/functions >/dev/null 2>&1
    if [ $? = 0 ]; then
        . /etc/init.d/functions
    fi
    ls /etc/rc.d/init.d/functions >/dev/null 2>&1
    if [ $? = 0 ]; then
        . /etc/rc.d/init.d/functions
    fi
}

determine_init_sys()
{
    if [ -x /sbin/start-stop-daemon ]; then
        START_CMD="${START_DEB_SYS_CMD}"
        STOP_CMD="${STOP_DEB_SYS_CMD}"
    else
        START_CMD="${START_RPM_SYS_CMD}"
        STOP_CMD="${STOP_RPM_SYS_CMD}"
    fi
}

check_superuser_permissions
set +e
import_initd_functions
set -e
determine_init_sys

start()
{
    eval " ${START_CMD}"
}

stop()
{
    eval " ${STOP_CMD}"
}

case "$1" in
  start)
    start
    echo "${PRODUCT_NAME} has started"
    deprecation_warning "systemctl start ${PKG_NAME}"
    ;;
  stop)
    stop
    echo "${PRODUCT_NAME} has stopped"
    deprecation_warning "systemctl stop ${PKG_NAME}"
    ;;
  restart|reload|force-reload)
      stop
      start
      echo "${PRODUCT_NAME} has restarted"
      deprecation_warning "systemctl restart ${PKG_NAME}"
    ;;
  *)
    echo "Usage: /etc/init.d/${PKG_NAME} {start|stop|restart}"
    echo "Or: service ${PKG_NAME} {start|stop|restart}"
    deprecation_warning "systemctl {start|stop|restart} ${PKG_NAME}"
    exit 1
    ;;
esac

exit 0
