#!/usr/bin/bash
# Copyright (c) 2025, Oracle and/or its affiliates. All rights reserved.
#
#    NAME
#        sql-scheduler - SQL scheduler service 
#
#    DESCRIPTION
#        Starts, stops and restarts, a SQL scheduler service
#
#    NOTES
#
#    CHANGE LOG
#        MODIFIED    VERSION    (MM/DD/YY)
#        admuro      1.0.0      02/05/25 - Script creation
#
#    TODO:
#
# chkconfig: 35 99 98
# pidfile: /var/run/sql-scheduler/pid
# description: SQL Scheduler Service

# The PID file containing directory
pid_file_home='/var/run/sql-scheduler'
if [ ! -d $pid_file_home ]; then
    mkdir -p $pid_file_home
fi
# The absolute path to the PID file
pid_file_path="$pid_file_home"'/pid'
log_file='/var/log/sql-schedule.logs'

function _running(){
    if [ -f $pid_file_path ]; then
        pid=$(cat $pid_file_path)
        echo $pid
    else
        echo 0
    fi
}

function _out_msg(){
    if [ "$1" == "ERROR" ]; then
        printf "\a%s%s\n" "$(date +%Y-%m-%dT%T.%3NZ) ERR: " "$2" >> ${log_file} &
        printf "\a%s%s\n" "$2" & 
    else
        printf "\a%s%s\n" "$(date +%Y-%m-%dT%T.%3NZ) INF: " "$2" >> ${log_file} &
        printf "\a%s%s\n" "$2" & 
    fi
}

function _start(){
    #Get the first user of the sql-scheduler group
    touch $log_file
    chown root:sql-scheduler $log_file
    chmod 660 $log_file
    main_user=$(getent  group  sql-scheduler | cut -d':' -f 4 | cut -d',' -f1)
    pid=$(_running)
    if [ $pid -gt 0 ]; then
        _out_msg "ERROR" "The SQLcl Scheduler Daemon is already running by ${main_user} user."
        exit 1
    fi
    _out_msg "INFO" "Starting the SQLcl Scheduler Daemon."
    for user in $(getent  group  sql-scheduler | cut -d':' -f 4 | tr "," "\n"); do
        _out_msg "INFO" "Starting the SQLcl Scheduler Daemon for user ${user}."
        sudo -iu ${user} <<-_SCRIPT
            sql -daemon start 2>&1 |tee -a ${log_file} 
_SCRIPT
    done
    exit_code="$?"
    if [ ${exit_code} -gt 0 ]; then
        _out_msg "ERROR" "The SQLcl Scheduler Daemon did not started correctly." 
    else
        _out_msg "INFO" "The SQLcl Scheduler Daemon has started correctly."
    fi
    touch $pid_file_path
    sleep 2
    echo $(ps -efa | grep java | grep '[d]aemon'|head -1|awk '{print $2}') > $pid_file_path
}

function _stop(){
    _out_msg "INFO" "Stopping the SQLcl Scheduler Daemon."
    for user in $(getent  group  sql-scheduler | cut -d':' -f 4 | tr "," "\n"); do
        _out_msg "INFO" "Stopping SQLcl Scheduler Daemon for user ${user}."
        sudo -iu ${user} <<-_SCRIPT
            sql -daemon stop 2>&1 | tee -a ${log_file} 
_SCRIPT
    done
    exit_code="$?"
    if [ ${exit_code} -gt 0 ]; then
        _out_msg "ERROR" "The SQLcl Scheduler Daemon did not stopped correctly." 
    else
        _out_msg "INFO" "The SQLcl Scheduler Daemon has stopped correctly."
    fi
    rm $pid_file_path
}
case "${1}" in
    start)
        _start
        ;;
    stop)
        _stop
        ;;
    restart)
        _stop
        _start 
        ;;
esac
