# orabase-functions
#
# Usefull functions for shell-scripts
#
# Used following environment variables:
#   * ORABASE_EXE_NAME  - name of calling shell-script
#   * ORABASE_LOG_FILE  - name of log file to write output messages
#   * ORABASE_QUIET     - supress output to console
#
#

#
# Set environment to highlight messages
# 
if [ "$TERM" = "dumb" -o "$TERM" = "unknown" ] ; then
    ORABASE_BLUE=""
    ORABASE_CYAN=""
    ORABASE_GREEN=""
    ORABASE_RED=""
    ORABASE_OFF=""
else
    ORABASE_BLUE="$(tput setaf 4 2>/dev/null)"
    ORABASE_CYAN="$(tput setaf 6 2>/dev/null)"
    ORABASE_GREEN="$(tput setaf 2 2>/dev/null)"
    ORABASE_RED="$(tput setaf 1 2>/dev/null)"
    ORABASE_OFF="$(tput sgr0 2>/dev/null)"
fi

#
# Write error message and exit
#
# Params:
#       $1 - Message
#       $2 - Exit code
#
orabase_error_exit() {
    local exe_name=${ORABASE_EXE_NAME:-$0}
    local str="E: $exe_name: `date '+%F %T.%S'`: ${1:-Error}"

    [ -z "$ORABASE_QUIET" ] && echo "${ORABASE_RED}${str}${ORABASE_OFF}" >&2
    [ -n "$ORABASE_LOG_FILE" ] && echo $str >> $ORABASE_LOG_FILE
    exit ${2:-1}
}

#
# Write error message
#
# Params:
#       $1 - Message
#
orabase_error() {
    local exe_name=${ORABASE_EXE_NAME:-$0}
    local str="E: $exe_name: `date '+%F %T.%S'`: ${1:-Error}"

    [ -z "$ORABASE_QUIET" ] && echo "${ORABASE_RED}${str}${ORABASE_OFF}" >&2
    [ -n "$ORABASE_LOG_FILE" ] && echo $str >> $ORABASE_LOG_FILE
}

#
# Write warning message
#
# Params:
#       $1 - Message
#
orabase_warn() {
    local exe_name=${ORABASE_EXE_NAME:-$0}
    local str="W: $exe_name: `date '+%F %T.%S'`: ${1:-Warning}"

    [ -z "$ORABASE_QUIET" ] && echo "${ORABASE_CYAN}${str}${ORABASE_OFF}" >&2
    [ -n "$ORABASE_LOG_FILE" ] && echo $str >> $ORABASE_LOG_FILE
}

#
# Write info message
#
# Params:
#       $1 - Message
#
orabase_info() {
    local exe_name=${ORABASE_EXE_NAME:-$0}
    local str="I: $exe_name: `date '+%F %T.%S'`: ${1:-Info}"

    [ -z "$ORABASE_QUIET" ] && echo "${ORABASE_BLUE}${str}${ORABASE_OFF}" >&2
    [ -n "$ORABASE_LOG_FILE" ] && echo $str >> $ORABASE_LOG_FILE
}

#
# Write okay message
#
# Params:
#       $1 - Message
#
orabase_okay() {
    local exe_name=${ORABASE_EXE_NAME:-$0}
    local str="I: $exe_name: `date '+%F %T.%S'`: ${1:-Okay}"

    [ -z "$ORABASE_QUIET" ] && echo "${ORABASE_GREEN}${str}${ORABASE_OFF}" >&2
    [ -n "$ORABASE_LOG_FILE" ] && echo $str >> $ORABASE_LOG_FILE
}

#
# Write newline
#
orabase_nl() {
    [ -z "$ORABASE_QUIET" ] && echo >&2
    [ -n "$ORABASE_LOG_FILE" ] && echo >> $ORABASE_LOG_FILE
}

#
# Show elapsed time
#
# Params:
#       $1 - Start time in seconds
#       $1 - End time in seconds
#
orabase_show_elapsedtime() {
    local s=${1:-0}
    local e=${2:-1}

    local ws=$(($e-$s))

    local hour=$(($ws/3600))
    local min=$((($ws - $hour*3600)/60))
    local sec=$(($ws - $hour*3600 - $min*60))

    echo "${hour}h ${min}m ${sec}s"
}

#
# Generate header for autogenerated files
#
# Params:
#       $1 - input file
#       $2 - output file
#       $3 - comment symbol(s)
#
orabase_generate_header() {
    local in_file=$1
    local out_file=${2:-/dev/null}
    local comment=${3:-#}
    local exe_name=${ORABASE_EXE_NAME:-$0}

    echo "$comment" > $out_file
    echo "$comment  This file is autogenerated at `(LANG=C; date)`" >> $out_file
    if [ -n "$in_file" ]; then 
        echo "$comment  from $in_file" >> $out_file
    fi
    echo "$comment" >> $out_file
    echo "$comment  by $exe_name" >> $out_file
    echo "$comment" >> $out_file
}

#
# Check group existence
#
# Params:
#       $1 - group name
#
orabase_check_group() {
  grep -q $1 /etc/group 2>/dev/null
  return $?
}

#
# Generate name for pidfile
#
orabase_pidfile() {
    local exe_name=${ORABASE_EXE_NAME:-$(basename $0)}

    echo "/var/run/$exe_name.pid"
}

#
# Write pid to pidfile
#
# Params:
#       $1 - optional PID of process
#       $2 - optional name of pidfile
#       $3 - optional number of command line args for given process
#
orabase_pidfile_write() {
    local pid=${1:-$$}
    local pidfile=${2:-`orabase_pidfile`}
    local parnum=${3:-""}

    ps h -o pid,command $pid | sed -e 's/\s\+/ /' | cut -d ' ' -f 1-$parnum | head -1 > $pidfile
}

#
# Check existence of process in pidfile
#
# Params:
#       $1 - optional name of pidfile
#
orabase_pidfile_chkstale() {
    local pidfile=${1:-`orabase_pidfile`}
    local rc=1

    if [ -r "$pidfile" ] ; then
        ps ax -o pid,command | fgrep -q -s -f $pidfile 2>/dev/null
        rc=$?
    fi

    return $rc
}

#
# Remove pidfile
#
# Params:
#       $1 - optional name of pidfile
#
orabase_pidfile_remove() {
    local pidfile=${1:-`orabase_pidfile`}

    [ -r "$pidfile" ] && rm -f $pidfile
}

