#!/bin/dash
### BEGIN INIT INFO
# Provides:          kurento-media-server
# Required-Start:    $remote_fs $network
# Required-Stop:     $remote_fs
# Should-Start:      coturn
# Default-Start:     2 3 4 5
# Default-Stop:      0 1 6
# Short-Description: Kurento Media Server daemon
### END INIT INFO

# Use ShellCheck: https://www.shellcheck.net/

if [ -r /lib/lsb/init-functions ]; then
  # shellcheck source=/lib/lsb/init-functions
  . /lib/lsb/init-functions
else
  echo "E: /lib/lsb/init-functions not found, package lsb-base needed"
  exit 1
fi

# Default daemon settings
START_DAEMON="false"
DAEMON_ARGS=""
DAEMON_USER="kurento"

# Default program settings
export GST_DEBUG="2,Kurento*:4,kms*:4,sdp*:4,webrtc*:4,*rtpendpoint:4,rtp*handler:4,rtpsynchronizer:4,agnosticbin:4"
export GST_DEBUG_NO_COLOR="1"
export KURENTO_LOGS_PATH="/var/log/kurento-media-server"

# Load user-configurable settings
if [ -r /etc/default/kurento-media-server ]; then
  # shellcheck source=/etc/default/kurento-media-server
  . /etc/default/kurento-media-server
fi

# Non-configurable daemon settings
DAEMON_LOG_DIR="$KURENTO_LOGS_PATH"
DAEMON_ERR_FILE="$DAEMON_LOG_DIR/errors.log"
DAEMON_BIN="/usr/bin/kurento-media-server"
PID_FILE="/var/run/kurento-media-server.pid"

[ "${START_DAEMON:-}" != "true" ] && {
    log_failure_msg "Startup is disabled; enable it in /etc/default/kurento-media-server"
    exit 1
}

[ ! -x "$DAEMON_BIN" ] && {
    log_failure_msg "File is not executable: $DAEMON_BIN"
    exit 1
}

# Only root can start Kurento
verify_user() {
    [ "$(id -u)" -eq 0 ] || {
        log_failure_msg "Only root can start or stop Kurento Media Server"
        exit 1
    }
}

start_kurento() {
    verify_user

    # Verify pid file directory exists
    if [ ! -e /var/run ]; then
        install -m 755 -d /var/run || {
            log_failure_msg "Unable to access /var/run directory"
            exit 1
        }
    fi

    # Verify logs destination directory ($DAEMON_LOG_DIR)
    [ -d "$DAEMON_LOG_DIR" ] || {
        log_warning_msg "Directory '$DAEMON_LOG_DIR' does not exist"
        install -m 755 -o "$DAEMON_USER" -d "$DAEMON_LOG_DIR" || {
            log_failure_msg "Cannot create directory '$DAEMON_LOG_DIR'"
            exit 1
        }
        log_success_msg "Created directory '$DAEMON_LOG_DIR'"
    }
    [ "$(stat --printf='%U' "$DAEMON_LOG_DIR")" = "$DAEMON_USER" ] || {
        log_warning_msg "'$DAEMON_LOG_DIR' is not owned by '$DAEMON_USER'"
        chown "$DAEMON_USER" "$DAEMON_LOG_DIR" || {
            log_failure_msg "Cannot change owner of '$DAEMON_LOG_DIR' to '$DAEMON_USER'"
            exit 1
        }
        log_success_msg "Changed owner of '$DAEMON_LOG_DIR' to '$DAEMON_USER'"
    }

    # Verify errors log file ($DAEMON_ERR_FILE)
    [ -f "$DAEMON_ERR_FILE" ] || {
        log_warning_msg "File '$DAEMON_ERR_FILE' does not exist"
        install -m 644 -o "$DAEMON_USER" /dev/null "$DAEMON_ERR_FILE" || {
            log_failure_msg "Cannot create file '$DAEMON_ERR_FILE'"
            exit 1
        }
        log_success_msg "Created file '$DAEMON_ERR_FILE'"
    }
    [ "$(stat --printf='%U' "$DAEMON_ERR_FILE")" = "$DAEMON_USER" ] || {
        log_warning_msg "'$DAEMON_ERR_FILE' is not owned by '$DAEMON_USER'"
        chown "$DAEMON_USER" "$DAEMON_ERR_FILE" || {
            log_failure_msg "Cannot change owner of '$DAEMON_ERR_FILE' to '$DAEMON_USER'"
            exit 1
        }
        log_success_msg "Changed owner of '$DAEMON_ERR_FILE' to '$DAEMON_USER'"
    }

    # If set and non-null, configure Kernel core dump output pattern
    [ -n "${DAEMON_CORE_PATTERN:-}" ] && {
        echo "$DAEMON_CORE_PATTERN" | tee /proc/sys/kernel/core_pattern >/dev/null
    }

    # Update process' resource limits
    log_action_msg "Set Kernel resource limits for Kurento Media Server"

    # Enable generation of Kernel core dumps / crash reports
    ulimit -c unlimited || {
        log_warning_msg "Cannot set limit: unlimited core dump size"
    }

    # If set and not null, configure per-process max open file descriptors
    [ -n "${DAEMON_MAX_FILES:-}" ] && {
        # Maximum limit value allowed by Ubuntu: 2^20 = 1048576
        MAXIMUM_LIMIT=1048576
        [ "$DAEMON_MAX_FILES" -gt "$MAXIMUM_LIMIT" ] && DAEMON_MAX_FILES="$MAXIMUM_LIMIT"

        ulimit -n "$DAEMON_MAX_FILES" || {
            log_warning_msg "Cannot set limit: $DAEMON_MAX_FILES max open file descriptors"
        }
    }

    # If set and not null, configure per-user max threads
    [ -n "${DAEMON_MAX_THREADS:-}" ] && {
        ulimit -Sp "$DAEMON_MAX_THREADS" || {
            log_warning_msg "Cannot set limit: $DAEMON_MAX_THREADS max threads"
        }
    }

    # Add new section in error log
    # Note: This is "echo" Dash builtin, not the "/bin/echo" GNU command, so
    # escape sequences can be used directly, no need to enable them
    echo "\n\n$(date --iso-8601=seconds) -- New execution" >>"$DAEMON_ERR_FILE"

    /sbin/start-stop-daemon \
        --start \
        --pidfile "$PID_FILE" \
        --make-pidfile \
        --exec "$DAEMON_BIN" \
        --chuid "$DAEMON_USER" \
        --background \
        --no-close \
        -- ${DAEMON_ARGS:-} \
        2>>"$DAEMON_ERR_FILE" \
    || {
        log_warning_msg "Kurento Media Server already started"
        return
    }
}

stop_kurento() {
    verify_user

    /sbin/start-stop-daemon \
        --stop \
        --pidfile "$PID_FILE" \
        --remove-pidfile \
        --exec "$DAEMON_BIN" \
        --retry=TERM/25/KILL/5 \
    || {
        log_failure_msg "Kurento Media Server not running"
    }

    if [ -f "$PID_FILE" ]; then
        rm -f "$PID_FILE"
    fi
}

status() {
    log_action_begin_msg "Checking Kurento Media Server"

    rc="$(pidofproc -p "$PID_FILE" "$DAEMON_BIN" >/dev/null 2>&1)"
    if [ "$rc" -eq 0 ]; then
        read -r pid < "$PID_FILE"
        log_action_cont_msg "$DAEMON_BIN is running with pid $pid"
    elif [ "$rc" -eq 1 ]; then
        log_action_cont_msg "$DAEMON_BIN is not running but the pid file exists"
    elif [ "$rc" -eq 3 ]; then
        log_action_cont_msg "$DAEMON_BIN is not running"
    else
        log_action_cont_msg "Unable to determine $DAEMON_BIN status"
    fi
    log_action_end_msg "$rc"
    return "$rc"
}

case "$1" in
  start)
    log_daemon_msg "Start Kurento Media Server"
    start_kurento
    log_end_msg $?;
    ;;

  stop)
    log_daemon_msg "Stop Kurento Media Server";
    stop_kurento
    log_end_msg $?;
    ;;

  restart)
    $0 stop
    $0 start
    ;;

  force-reload)
    $0 stop
    $0 start
    ;;

  status)
    status
    ;;

  *)
    echo "Usage: $0 {start|stop|restart|status}" >&2
    ;;
esac
