#!/bin/sh
##############################################################################
# If not stated otherwise in this file or this component's LICENSE file the
# following copyright and licenses apply:
#
# Copyright 2020 RDK Management
#
# Licensed 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 /lib/rdk/init-functions
#
# Description : Run Level Control Functions
#
########################################################################

## Environmental setup
# Setup default values for environment
umask 022

if [ -f /etc/env_setup.sh ]; then
    . /etc/env_setup.sh
fi

export LOG4C_RCPATH=/etc

## Screen Dimensions
# Find current screen size
# if [ -z "${COLUMNS}" ]; then
#   COLUMNS=$(stty size)
#   COLUMNS=${COLUMNS##* }
# fi

# When using remote connections, such as a serial port, stty size returns 0
# if [ "${COLUMNS}" = "0" ] || [ -z "${COLUMNS}" ]; then
   COLUMNS=80
#fi

SUFFIX_LEN=8
WSUFFIX_LEN=10

## Measurements for positioning result messages
COL=$((${COLUMNS} - SUFFIX_LEN))
WCOL=$((${COLUMNS} - WSUFFIX_LEN))

## Set Cursor Position Commands, used via echo
SET_COL="\\033[${COL}G"      # at the $COL char
SET_WCOL="\\033[${WCOL}G"    # at the $WCOL char
CURS_UP="\\033[1A\\033[0G"   # Up one line, at the 0'th char
CURS_ZERO="\\033[0G"

## Set color commands, used via echo
# Please consult `man console_codes for more information
# under the "ECMA-48 Set Graphics Rendition" section
#
# Warning: when switching from a 8bit to a 9bit font,
# the linux console will reinterpret the bold (1;) to
# the top 256 glyphs of the 9bit font.  This does
# not affect framebuffer consoles

NORMAL="\\033[0;39m"         # Standard console grey
SUCCESS="\\033[1;32m"        # Success is green
WARNING="\\033[1;33m"        # Warnings are yellow
FAILURE="\\033[1;31m"        # Failures are red
ERRORC="\\033[1;31m"         # Errors are red
INFO="\\033[1;36m"           # Information is light cyan
BRACKET="\\033[1;34m"        # Brackets are blue

# Use a colored prefix
BMPREFIX="     "
SUCCESS_PREFIX="${SUCCESS}  *  ${NORMAL}"
FAILURE_PREFIX="${FAILURE}*****${NORMAL}"
WARNING_PREFIX="${WARNING} *** ${NORMAL}"
ERROR_PREFIX="${ERRORC} *** ERROR *** ${NORMAL}"

PREFIX_LEN=5
EPREFIX_LEN=15

SUCCESS_SUFFIX="${BRACKET}[${SUCCESS}  OK  ${BRACKET}]${NORMAL}"
FAILURE_SUFFIX="${BRACKET}[${FAILURE} FAIL ${BRACKET}]${NORMAL}"
WARNING_SUFFIX="${BRACKET}[${WARNING} WARN ${BRACKET}]${NORMAL}"

SUCCESS_SUFFIX_LOG="[  OK  ]"
FAILURE_SUFFIX_LOG="[ FAIL ]"
WARNING_SUFFIX_LOG="[ WARN ]"

BASE_LOG_DIR=/var/log
BOOTLOG=${BASE_LOG_DIR}/bootlog
KILLDELAY=3

# CONSTANTS FOR CONSOLE AND LOG LEVELS
SI_LVL_SILENTLY=4
SI_LVL_RESUME=3
SI_LVL_ERROR=2
SI_LVL_WARNING=1
SI_LVL_INFO=0
SI_BOOTLOG_STRLEN=80

# CURRENT CONSOLE AND LOG LEVEL SETTINGS
SI_STDOUT_LOG_LEVEL=$SI_LVL_RESUME
SI_LOGFILE_LOG_LEVEL=$SI_LVL_RESUME

# Set any user specified environment variables e.g. HEADLESS
[ -r /etc/sysconfig/rc.site ]  && . /etc/sysconfig/rc.site

################################################################################
# start_daemon()                                                               #
# Usage: start_daemon [-f] [-n nicelevel] [-p pidfile] pathname [args...]      #
#                                                                              #
# Purpose: This runs the specified program as a daemon                         #
#                                                                              #
# Inputs: -f: (force) run the program even if it is already running.           #
#         -n nicelevel: specify a nice level. See 'man nice(1)'.               #
#         -p pidfile: use the specified file to determine PIDs.                #
#         pathname: the complete path to the specified program                 #
#         args: additional arguments passed to the program (pathname)          #
#                                                                              #
# Return values (as defined by LSB exit codes):                                #
#       0 - program is running or service is OK                                #
#       1 - generic or unspecified error                                       #
#       2 - invalid or excessive argument(s)                                   #
#       5 - program is not installed                                           #
################################################################################
start_daemon()
{
    local force=""
    local nice="0"
    local pidfile=""
    local pidlist=""
    local retval=""

    # Process arguments
    while true
    do
        case "${1}" in

            -f)
                force="1"
                shift 1
                ;;

            -n)
                nice="${2}"
                shift 2
                ;;

            -p)
                pidfile="${2}"
                shift 2
                ;;

            -*)
                return 2
                ;;

            *)
                program="${1}"
                break
                ;;
        esac
    done

    # Check for a valid program
    if [ ! -e "${program}" ]; then return 5; fi

    # Execute
    if [ -z "${force}" ]; then
        if [ -z "${pidfile}" ]; then
            # Determine the pid by discovery
            pidlist=`pidofproc "${1}"`
            retval="${?}"
        else
            # The PID file contains the needed PIDs
            # Note that by LSB requirement, the path must be given to pidofproc,
            # however, it is not used by the current implementation or standard.
            pidlist=`pidofproc -p "${pidfile}" "${1}"`
            retval="${?}"
        fi

        # Return a value ONLY 
        # It is the init script's (or distribution's functions) responsibilty
        # to log messages!
        case "${retval}" in

            0)
                # Program is already running correctly, this is a 
                # successful start.
                return 0
                ;;

            1)
                # Program is not running, but an invalid pid file exists
                # remove the pid file and continue
                rm -f "${pidfile}"
                ;;

            3)
                # Program is not running and no pidfile exists
                # do nothing here, let start_deamon continue.
                ;;

            *)
                # Others as returned by status values shall not be interpreted
                # and returned as an unspecified error.
                return 1
                ;;
        esac
    fi

    # Do the start!
    nice -n "${nice}" "${@}"
}

################################################################################
# killproc()                                                                   #
# Usage: killproc [-p pidfile] pathname [signal]                               #
#                                                                              #
# Purpose: Send control signals to running processes                           #
#                                                                              #
# Inputs: -p pidfile, uses the specified pidfile                               #
#         pathname, pathname to the specified program                          #
#         signal, send this signal to pathname                                 #
#                                                                              #
# Return values (as defined by LSB exit codes):                                #
#       0 - program (pathname) has stopped/is already stopped or a             #
#           running program has been sent specified signal and stopped         #
#           successfully                                                       #
#       1 - generic or unspecified error                                       #
#       2 - invalid or excessive argument(s)                                   #
#       5 - program is not installed                                           #
#       7 - program is not running and a signal was supplied                   #
################################################################################
killproc()
{
    local pidfile
    local program
    local prefix
    local progname
    local signal="-TERM"
    local fallback="-KILL"
    local nosig
    local pidlist
    local retval
    local pid
    local delay="30"
    local piddead
    local dtime

    # Process arguments
    while true; do
        case "${1}" in
            -p)
                pidfile="${2}"
                shift 2
                ;;
 
             *)
                 program="${1}"
                 if [ -n "${2}" ]; then
                     signal="${2}"
                     fallback=""
                 else
                     nosig=1
                 fi

                 # Error on additional arguments
                 if [ -n "${3}" ]; then
                     return 2
                 else 
                     break
                 fi                 
                 ;;
        esac
    done

    # Check for a valid program
    if [ ! -e "${program}" ]; then return 5; fi

    # Check for a valid signal
    check_signal "${signal}"
    if [ "${?}" -ne "0" ]; then return 2; fi

    # Get a list of pids
    if [ -z "${pidfile}" ]; then
        # determine the pid by discovery
        pidlist=`pidofproc "${1}"`
        retval="${?}"
    else
        # The PID file contains the needed PIDs
        # Note that by LSB requirement, the path must be given to pidofproc,
        # however, it is not used by the current implementation or standard.
        pidlist=`pidofproc -p "${pidfile}" "${1}"`
        retval="${?}"
    fi

    # Return a value ONLY
    # It is the init script's (or distribution's functions) responsibilty
    # to log messages!
    case "${retval}" in

        0)
            # Program is running correctly
            # Do nothing here, let killproc continue.
            ;;

        1)
            # Program is not running, but an invalid pid file exists
            # Remove the pid file.
            rm -f "${pidfile}"

            # This is only a success if no signal was passed.
            if [ -n "${nosig}" ]; then
                return 0
            else
                return 7
            fi
            ;;

        3)
            # Program is not running and no pidfile exists
            # This is only a success if no signal was passed.
            if [ -n "${nosig}" ]; then
                return 0
            else
                return 7
            fi
            ;;

        *)
            # Others as returned by status values shall not be interpreted
            # and returned as an unspecified error.
            return 1
            ;;
    esac

    # Perform different actions for exit signals and control signals
    check_sig_type "${signal}"

    if [ "${?}" -eq "0" ]; then # Signal is used to terminate the program

        # Account for empty pidlist (pid file still exists and no 
        # signal was given)
        if [ "${pidlist}" != "" ]; then

            # Kill the list of pids
            for pid in ${pidlist}; do

                kill -0 "${pid}" 2> /dev/null

                if [ "${?}" -ne "0" ]; then
                    # Process is dead, continue to next and assume all is well
                    continue
                else
                    kill "${signal}" "${pid}" 2> /dev/null

                    # Wait up to ${delay}/10 seconds to for "${pid}" to 
                    # terminate in 10ths of a second

                    while [ "${delay}" -ne "0" ]; do
                        kill -0 "${pid}" 2> /dev/null || piddead="1"
                        if [ "${piddead}" = "1" ]; then break; fi
                        sleep 1
                        delay="$(( ${delay} - 1 ))"
                    done

                    # If a fallback is set, and program is still running, then
                    # use the fallback
                    if [ -n "${fallback}" -a "${piddead}" != "1" ]; then
                        kill "${fallback}" "${pid}" 2> /dev/null
                        sleep 1
                        # Check again, and fail if still running
                        kill -0 "${pid}" 2> /dev/null && return 1
                    else
                        # just check one last time and if still alive, fail
                        sleep 1
                        kill -0 "${pid}" 2> /dev/null && return 1
                    fi
                fi
            done
        fi

        # Check for and remove stale PID files.
        if [ -z "${pidfile}" ]; then
            # Find the basename of $program
            prefix=`echo "${program}" | sed 's/[^/]*$//'`
            if [ -z "${prefix}" ]; then 
                progname="${program}"
            else
                progname=`echo "${program}" | sed "s@${prefix}@@"`
            fi

            if [ -e "/var/run/${progname}.pid" ]; then
                rm -f "/var/run/${progname}.pid" 2> /dev/null
            fi
        else
            if [ -e "${pidfile}" ]; then rm -f "${pidfile}" 2> /dev/null; fi
        fi

    # For signals that do not expect a program to exit, simply
    # let kill do it's job, and evaluate kills return for value

    else # check_sig_type - signal is not used to terminate program
        for pid in ${pidlist}; do
            kill "${signal}" "${pid}"
            if [ "${?}" -ne "0" ]; then return 1; fi
        done
    fi
}

################################################################################
# pidofproc()                                                                  #
# Usage: pidofproc [-p pidfile] pathname                                       #
#                                                                              #
# Purpose: This function returns one or more pid(s) for a particular daemon    #
#                                                                              #
# Inputs: -p pidfile, use the specified pidfile instead of pidof               #
#         pathname, path to the specified program                              #
#                                                                              #
# Return values (as defined by LSB status codes):                              #
#       0 - Success (PIDs to stdout)                                           #
#       1 - Program is dead, PID file still exists (remaining PIDs output)     #
#       3 - Program is not running (no output)                                 #
################################################################################
pidofproc()
{
    local pidfile
    local program
    local prefix
    local progname
    local pidlist
    local lpids
    local exitstatus="0"

    # Process arguments
    while true; do
        case "${1}" in

            -p)
                pidfile="${2}"
                shift 2
                ;;

            *)
                program="${1}"
                if [ -n "${2}" ]; then
                    # Too many arguments
                    # Since this is status, return unknown
                    return 4
                else
                    break
                fi
                ;;
        esac
    done

    # If a PID file is not specified, try and find one.
    if [ -z "${pidfile}" ]; then
        # Get the program's basename
        prefix=`echo "${program}" | sed 's/[^/]*$//'`
        if [ -z "${prefix}" ]; then 
           progname="${program}"
        else
           progname=`echo "${program}" | sed "s@${prefix}@@"`
        fi

        # If a PID file exists with that name, assume that is it.
        if [ -e "/var/run/${progname}.pid" ]; then
            pidfile="/var/run/${progname}.pid"
        fi
    fi

    # If a PID file is set and exists, use it.
    if [ -n "${pidfile}" -a -e "${pidfile}" ]; then

        # Use the value in the first line of the pidfile
        pidlist=`/bin/head -n1 "${pidfile}"`
        # This can optionally be written as 'sed 1q' to repalce 'head -n1'
        # should LFS move /bin/head to /usr/bin/head
    else
        # Use pidof
        pidlist=`pidof "${program}"`
    fi

    # Figure out if all listed PIDs are running.
    for pid in ${pidlist}; do
        kill -0 ${pid} 2> /dev/null

        if [ "${?}" -eq "0" ]; then
            lpids="${lpids}${pid} "
        else
            exitstatus="1"
        fi
    done

    if [ -z "${lpids}" -a ! -f "${pidfile}" ]; then
        return 3
    else
        echo "${lpids}"
        return "${exitstatus}"
    fi
}

################################################################################
# statusproc()                                                                 #
# Usage: statusproc [-p pidfile] pathname                                      #
#                                                                              #
# Purpose: This function prints the status of a particular daemon to stdout    #
#                                                                              #
# Inputs: -p pidfile, use the specified pidfile instead of pidof               #
#         pathname, path to the specified program                              #
#                                                                              #
# Return values:                                                               #
#       0 - Status printed                                                     #
#       1 - Input error. The daemon to check was not specified.                #
################################################################################
statusproc()
{
   local pidfile
   local pidlist

   if [ "${#}" = "0" ]; then
      echo "Usage: statusproc [-p pidfle] {program}"
      exit 1
   fi

   # Process arguments
   while true; do
       case "${1}" in

           -p)
               pidfile="${2}"
               shift 2
               ;;

           *)
               if [ -n "${2}" ]; then
                   echo "Too many arguments"
                   return 1
               else
                   break
               fi
               ;;
       esac
   done

   if [ -n "${pidfile}" ]; then
      pidlist=`pidofproc -p "${pidfile}" $@`
   else
      pidlist=`pidofproc $@`
   fi

   # Trim trailing blanks
   pidlist=`echo "${pidlist}" | sed -r 's/ +$//'`

   base="${1##*/}"

   if [ -n "${pidlist}" ]; then
      echo -e "${INFO}${base} is running with Process" \
         "ID(s) ${pidlist}.${NORMAL}"
   else
      if [ -n "${base}" -a -e "/var/run/${base}.pid" ]; then
         echo -e "${WARNING}${1} is not running but" \
            "/var/run/${base}.pid exists.${NORMAL}"
      else
         if [ -n "${pidfile}" -a -e "${pidfile}" ]; then
            echo -e "${WARNING}${1} is not running" \
               "but ${pidfile} exists.${NORMAL}"
         else
            echo -e "${INFO}${1} is not running.${NORMAL}"
         fi
      fi
   fi
}

################################################################################
# timespec()                                                                   #
#                                                                              #
# Purpose: An internal utility function to format a timestamp                  #
#          a boot log file.  Sets the STAMP variable.                          #
#                                                                              #
# Return value: Not used                                                       #
################################################################################
timespec()
{
   STAMP="$(echo `date +"%b %d %T"` `hostname`) "
   return 0
}

################################################################################
# log_success_msg()                                                            #
# Usage: log_success_msg ["message"]                                           #
#                                                                              #
# Purpose: Print a successful status message to the screen and                 #
#          a boot log file.                                                    #
#                                                                              #
# Inputs: $@ - Message                                                         #
#                                                                              #
# Return values: Not used                                                      #
################################################################################
log_success_msg()
{
    # Strip non-printable characters from log file
    local logmessage=`echo "${@}" | sed 's/\\\033[^a-zA-Z]*.//g'`
    local len_of_logmessage=`expr length "${logmessage}"`
    local len_of_success_suffix_log=`expr length "${SUCCESS_SUFFIX_LOG}"`
    if [ $SI_STDOUT_LOG_LEVEL -le $SI_LVL_RESUME ]; then
        echo -n -e "${SUCCESS_PREFIX}${logmessage}"
        local num_of_blanks=`expr $COLUMNS - $SUFFIX_LEN - $PREFIX_LEN - ${len_of_logmessage}`
        echo -e ${SUCCESS_SUFFIX} | sed -e :a -e "s/^.\{1,$num_of_blanks\}$/ &/;ta"
    fi

    if [ $SI_LOGFILE_LOG_LEVEL -le $SI_LVL_RESUME ]; then
        timespec
        local num_of_blanks=`expr $SI_BOOTLOG_STRLEN - ${len_of_success_suffix_log} - ${len_of_logmessage}`
        echo -n -e "${STAMP} ${logmessage}" >> ${BOOTLOG}
        echo ${SUCCESS_SUFFIX_LOG} | sed -e :a -e "s/^.\{1,$num_of_blanks\}$/ &/;ta" >> ${BOOTLOG}
    fi
    
    return 0
}

log_success_msg2()
{
    if [ $SI_STDOUT_LOG_LEVEL -le $SI_LVL_RESUME ]; then
        echo -n -e "${BMPREFIX}${@}"
        echo -e "${CURS_ZERO}${SUCCESS_PREFIX}${SET_COL}${SUCCESS_SUFFIX}"
    fi

    if [ $SI_LOGFILE_LOG_LEVEL -le $SI_LVL_RESUME ]; then
        echo " [ OK ]" >> ${BOOTLOG}
    fi

    return 0
}

################################################################################
# log_failure_msg()                                                            #
# Usage: log_failure_msg ["message"]                                           #
#                                                                              #
# Purpose: Print a failure status message to the screen and                    #
#          a boot log file.                                                    #
#                                                                              #
# Inputs: $@ - Message                                                         #
#                                                                              #
# Return values: Not used                                                      #
################################################################################
log_failure_msg()
{
    # Strip non-printable characters from log file
    local logmessage=`echo "${@}" | sed 's/\\\033[^a-zA-Z]*.//g'`
    if [ $SI_STDOUT_LOG_LEVEL -le $SI_LVL_RESUME ]; then
        echo -n -e "${FAILURE_PREFIX}${logmessage}"
        local num_of_blanks=$(( $COLUMNS - $SUFFIX_LEN - $PREFIX_LEN - ${#logmessage} ))
        echo -e ${FAILURE_SUFFIX} | sed -e :a -e "s/^.\{1,$num_of_blanks\}$/ &/;ta"

    fi

    if [ $SI_LOGFILE_LOG_LEVEL -le $SI_LVL_RESUME ]; then
        timespec
        local num_of_blanks=$(( $SI_BOOTLOG_STRLEN - ${#SUCCESS_SUFFIX_LOG} - ${#logmessage} ))
        echo -n -e "${STAMP} ${logmessage}" >> ${BOOTLOG}
        echo ${FAILURE_SUFFIX_LOG} | sed -e :a -e "s/^.\{1,$num_of_blanks\}$/ &/;ta" >> ${BOOTLOG}
    fi
    
    return 0
}

log_failure_msg2()
{
    if [ $SI_STDOUT_LOG_LEVEL -le $SI_LVL_RESUME ]; then
        echo -n -e "${BMPREFIX}${@}"
        echo -e "${CURS_ZERO}${FAILURE_PREFIX}${SET_COL}${FAILURE_SUFFIX}"
    fi

    if [ $SI_LOGFILE_LOG_LEVEL -le $SI_LVL_RESUME ]; then
        echo "[FAIL]" >> ${BOOTLOG}
    fi

    return 0
}

################################################################################
# log_error_msg()                                                             #
################################################################################
log_error_msg()
{
    if [ $SI_STDOUT_LOG_LEVEL -le $SI_LVL_RESUME ]; then
        echo -n -e "${BMPREFIX}${ERROR_PREFIX}${@}"
        echo -e "${CURS_ZERO}"
    fi

    if [ $SI_LOGFILE_LOG_LEVEL -le $SI_LVL_RESUME ]; then
        local logmessage=`echo "${@}" | sed 's/\\\033[^a-zA-Z]*.//g'`
        timespec
        echo -e "${STAMP} ### ERROR ###${logmessage}" >> ${BOOTLOG}
    fi

    return 0
}

################################################################################
# log_resume_msg()                                                              #
################################################################################
log_resume_msg()
{
    if [ $SI_STDOUT_LOG_LEVEL -le $SI_LVL_ERROR ]; then
        echo -n -e "${BMPREFIX}${@}"
        echo -e "${CURS_ZERO}"
    fi

    if [ $SI_LOGFILE_LOG_LEVEL -le $SI_LVL_ERROR ]; then
        local logmessage=`echo "${@}" | sed 's/\\\033[^a-zA-Z]*.//g'`
        timespec
        echo -e "${STAMP} ${logmessage}" >> ${BOOTLOG}
    fi

    return 0
}

################################################################################
# log_warning_msg()                                                            #
# Usage: log_warning_msg ["message"]                                           #
#                                                                              #
# Purpose: Print a warning status message to the screen and                    #
#          a boot log file.                                                    #
#                                                                              #
# Return values: Not used                                                      #
################################################################################
log_warning_msg()
{
    if [ $SI_STDOUT_LOG_LEVEL -le $SI_LVL_WARNING ]; then
        echo -n -e "${BMPREFIX}${@}"
        echo -e "${CURS_ZERO}${WARNING_PREFIX}${SET_COL}${WARNING_SUFFIX}"
    fi

    if [ $SI_LOGFILE_LOG_LEVEL -le $SI_LVL_WARNING ]; then
        # Strip non-printable characters from log file
        local logmessage=`echo "${@}" | sed 's/\\\033[^a-zA-Z]*.//g'`
        timespec
        echo -e "${STAMP} ${logmessage} [WARN]" >> ${BOOTLOG}
    fi

    return 0
}

################################################################################
# log_info_msg()                                                               #
# Usage: log_info_msg message                                                  #
#                                                                              #
# Purpose: Print an information message to the screen and                      #
#          a boot log file.  Does not print a trailing newline character.      #
#                                                                              #
# Return values: Not used                                                      #
################################################################################
log_info_msg()
{
    if [ $SI_STDOUT_LOG_LEVEL -le $SI_LVL_INFO ]; then
        echo -n -e "${BMPREFIX}${@}"
    fi

    if [ $SI_LOGFILE_LOG_LEVEL -le $SI_LVL_INFO ]; then
        # Strip non-printable characters from log file
        local logmessage=`echo "${@}" | sed 's/\\\033[^a-zA-Z]*.//g'`
        timespec
        echo -n -e "${STAMP} ${logmessage}" >> ${BOOTLOG}
    fi

    return 0
}

log_info_msg2()
{
    if [ $SI_STDOUT_LOG_LEVEL -le $SI_LVL_INFO ]; then
        echo -n -e "${@}"
    fi

    if [ $SI_LOGFILE_LOG_LEVEL -le $SI_LVL_INFO ]; then
        # Strip non-printable characters from log file
        local logmessage=`echo "${@}" | sed 's/\\\033[^a-zA-Z]*.//g'`
        echo -n -e "${logmessage}" >> ${BOOTLOG}
    fi

    return 0
}

################################################################################
# evaluate_retval()                                                            #
# Usage: Evaluate a return value and print success or failyure as appropriate  #
#                                                                              #
# Purpose: Convenience function to terminate an info message                   #
#                                                                              #
# Return values: Not used                                                      #
################################################################################
evaluate_retval()
{
   local error_value="${?}"

   if [ ${error_value} = 0 ]; then
      log_success_msg2
   else
      log_failure_msg2
   fi
}

################################################################################
# check_signal()                                                               #
# Usage: check_signal [ -{signal} | {signal} ]                                 #
#                                                                              #
# Purpose: Check for a valid signal.  This is not defined by any LSB draft,    #
#          however, it is required to check the signals to determine if the    #
#          signals chosen are invalid arguments to the other functions.        #
#                                                                              #
# Inputs: Accepts a single string value in the form or -{signal} or {signal}   #
#                                                                              #
# Return values:                                                               #
#       0 - Success (signal is valid                                           #
#       1 - Signal is not valid                                                #
################################################################################
check_signal()
{
    local valsig

    # Add error handling for invalid signals
    valsig="-ALRM -HUP -INT -KILL -PIPE -POLL -PROF -TERM -USR1 -USR2"
    valsig="${valsig} -VTALRM -STKFLT -PWR -WINCH -CHLD -URG -TSTP -TTIN"
    valsig="${valsig} -TTOU -STOP -CONT -ABRT -FPE -ILL -QUIT -SEGV -TRAP"
    valsig="${valsig} -SYS -EMT -BUS -XCPU -XFSZ -0 -1 -2 -3 -4 -5 -6 -8 -9"
    valsig="${valsig} -11 -13 -14 -15"

    echo "${valsig}" | grep -- " ${1} " > /dev/null

    if [ "${?}" -eq "0" ]; then
        return 0
    else
        return 1
    fi
}

################################################################################
# check_sig_type()                                                             #
# Usage: check_signal [ -{signal} | {signal} ]                                 #
#                                                                              #
# Purpose: Check if signal is a program termination signal or a control signal #
#          This is not defined by any LSB draft, however, it is required to    #
#          check the signals to determine if they are intended to end a        #
#          program or simply to control it.                                    #
#                                                                              #
# Inputs: Accepts a single string value in the form or -{signal} or {signal}   #
#                                                                              #
# Return values:                                                               #
#       0 - Signal is used for program termination                             #
#       1 - Signal is used for program control                                 #
################################################################################
check_sig_type()
{
    local valsig

    # The list of termination signals (limited to generally used items)
    valsig="-ALRM -INT -KILL -TERM -PWR -STOP -ABRT -QUIT -2 -3 -6 -9 -14 -15"

    echo "${valsig}" | grep -- " ${1} " > /dev/null

    if [ "${?}" -eq "0" ]; then
        return 0
    else
        return 1
    fi
}

################################################################################
# wait_for_user()                                                              #
#                                                                              #
# Purpose: Wait for the user to respond if not a headless system               #
#                                                                              #
################################################################################
wait_for_user()
{
   # Wait for the user by default
   [ "${HEADLESS=0}" = "0" ] && read ENTER
   return 0
}

################################################################################
# is_true()                                                                    #
#                                                                              #
# Purpose: Utility to test if a variable is true | yes | 1                     #
#                                                                              #
################################################################################
is_true()
{
   [ "$1" = "1" ] || [ "$1" = "yes" ] || [ "$1" = "true" ] ||  [ "$1" = "y" ] ||
   [ "$1" = "t" ]
}

# End /lib/lsb/init-functions

################################################################################
# redirect_all()                                                               #
# Usage: redirect_all file                                                     #
#                                                                              #
# Purpose: Redirect 'std' and 'error' output to given file                     #
#                                                                              #
#                                                                              #
# Return values: Not used                                                      #
################################################################################
redirect_all()
{
    exec 4>&1 
    exec 5>&2
    
    if [ "$1" ]; then
        if [ ! $2 ] || [ "$2" != ">>" ]; then
            echo -n > "$1"
        fi
        exec 1>> "$1"
        exec 2>> "$1"
    fi
}

################################################################################
# undirect_all()                                                               #
# Usage: undirect_all file                                                     #
#                                                                              #
# Purpose: Undo redirection of 'std' and 'error' output. The function expects  #
#          that redirect_all() has been called before                          #
#                                                                              #
# Return values: Not used                                                      #
################################################################################
undirect_all()
{
    exec 1>&4 4>&-
    exec 2>&5 5>&-    
}


pre_start()
{
    if [ $1 ]; then
        if [ $2 ]; then
            filename="$1"
            mode="$2"
        else
            if [ $1 == ">" ] || [ $1 == ">>" ]; then
                mode=$1
            else
                filename="$1"
                mode=">"
            fi
        fi
    fi

    if [ ! $filename ]; then
        filename=$0
        if [ -h $filename ]; then
            filename=`readlink $filename`
        fi
        filename=`basename $filename`
    fi

    if [ ! $mode ]; then
        mode=">"
    fi

    if [ `basename ${filename}` == $filename ]; then
        logname="${BASE_LOG_DIR}/${filename}"
    else
        logname=${filename}    
    fi
    
    log_info_msg "$filename enter start_function"
    redirect_all "${logname}" "$mode"
}

post_start()
{
    filename=$0
    if [ -h $filename ]; then
        filename=`readlink $filename`
    fi
    filename=`basename $filename`

    undirect_all
    msg="Starting service $filename"
    if [ ! $1 ] || [ $1 -eq 0 ]; then
        log_success_msg "$msg"
    else
        log_failure_msg "$msg"
    fi
}

pre_stop()
{
    filename=$0
    if [ -h $filename ]; then
        filename=`readlink $filename`
    fi
    filename=`basename $filename`
    log_info_msg "$filename enter stop_function"
}

post_stop()
{
    filename=$0
    if [ -h $filename ]; then
        filename=`readlink $filename`
    fi
    filename=`basename $filename`
    msg="Stopping service $filename"
    if [ ! $1 ] || [ $1 -eq 0 ]; then
        log_success_msg "$msg"
    else
        log_failure_msg "$msg"
    fi
}

# debug_print
# Prints when the debugging variable above is set to 1
debug_print() {
    if [ $DEBUG -eq 1 ]; then
        echo "DEBUG ($SCRIPTNAME): $*"
    fi
}

# try_command()
# Tries to run the command supplied as arguments. If it fails, print a message
# and exit with error code 1
try_command() {
    debug_print "try_command running '$*'"
    $*
    if [ $? -ne 0 ]; then
        echo "ERROR ($SCRIPTNAME): Command '$*' failed"
        exit 1
    fi
}

# make_dev()
# Creates a character node based on the driver name and device name passed in.
# If only two arguments: major number is determined from /proc/devices; 0 is
#   used as the minor number. (if you need to specify the minor see
#   make_dev_minor())
# Arg 1: Driver name is it appears in /proc/devices
# Arg 2: Name of the node to create
# Arg 3: The nod major number,
# Arg 4: The nod minor number, default 0.
make_dev()
{
    local DRIVER=$1
    local DEVICE=$2

    rm -f /dev/$DEVICE
    local major_number=${3:-$(egrep "[0-9]+ ${DRIVER}$" /proc/devices | cut -b1,2,3)}
    local minor_number=${4:-"0"}
    if [ ! -z $major_number ]; then
        try_command /bin/mknod /dev/$DEVICE c $major_number $minor_number
    else
        echo "ERROR ($SCRIPTNAME): Failed creating device node $DEVICE"
        exit 1
    fi
}

# make_dev_minor()
# Creates a character node for a driver; The major number is determined by
# checking /proc/devices; Minor number is specified or defaults to 0
# Arg 1: Driver name as it appears in /proc/devices
# Arg 2: Name of the node to create
# Arg 3: Minor number to use (defaults to 0)
make_dev_minor()
{
    local DRIVER=$1
    local DEVICE=$2
    local MINOR=${3:-"0"}

    rm -f /dev/$DEVICE
    local major_number=$(egrep "[0-9]+ ${DRIVER}$" /proc/devices | cut -b1,2,3)
    if [ ! -z $major_number ]; then
        try_command /bin/mknod /dev/$DEVICE c $major_number $MINOR
    else
        echo "ERROR ($SCRIPTNAME): Failed creating device node $DEVICE"
        exit 1
    fi
}


# make_auto_dev()
# Creates a block or character device node; uses /proc/devices to determine
# the major number, and whether to create a block or character node
# Arg 1: Driver name is it appears in /proc/devices
# Arg 2: Name of the node to create
# Arg 3: The nod minor number, if not set this defaults to 0.
make_auto_dev()
{
    local DRIVER=$1
    local DEVICE=$2
    local MINOR=${3:-"0"}

    rm -f /dev/$DEVICE
    # Get the major number from /proc/devices
    local major_number=$(egrep "[0-9]+ ${DRIVER}$" /proc/devices | cut -b1,2,3)
    if [ -z $major_number ]; then
        echo "ERROR: Could not find ${DRIVER} in /proc/devices;"
        echo "       perhaps it hasn't been loaded yet?"
        exit 1
    fi

    # Find out which lines contain the starts of character and block devices;
    # and which line the requested device is on
    local char_dev_line=$(egrep -n "^Character devices" /proc/devices | egrep -o '^[0-9]+')
    local block_dev_line=$(egrep -n "^Block devices" /proc/devices | egrep -o '^[0-9]+')
    local requested_dev_line=$(egrep -n "[0-9]+ ${DRIVER}" /proc/devices | egrep -o '^[0-9]+')

    # Verify that we have valid entries
    if [ -z $char_dev_line -o -z $block_dev_line ]; then
        echo "ERROR: /proc/devices appears to be malformed;"
        echo "       could not find block section, or character section"
        exit 1
    elif [ -z $requested_dev_line ]; then
        echo "ERROR: Could not find ${DRIVER} in /proc/devices;"
        echo "       perhaps it hasn't been loaded yet?"
        exit 1
    fi
    
    # Figure out if the requested line is in the block or character section
    local device_type=""
    if [ $block_dev_line -gt $char_dev_line ]; then
        if [ $requested_dev_line -gt $block_dev_line ]; then
            device_type="b"
        else
            device_type="c"
        fi
    else
        if [ $requested_dev_line -gt $char_dev_line ]; then
            device_type="c"
        else
            device_type="b"
        fi
    fi

    # Create the device
    try_command /bin/mknod /dev/$DEVICE $device_type $major_number $MINOR
}

run_if_exists()
{
    if which ${1} > /dev/null; then
        "$@"
    fi
}

###############################
# Other utility ideas
# -- Utility which kills with SIGTERM; checks if still running after a few
#      seconds, if yes sends SIGKILL
###############################
ulimit -c unlimited

if [ -f /etc/profile ]; then
   source /etc/profile
fi
