# -------------------------------------------------------------------------------
# Library for handling qserv daemons on Scientific Linux
# Author: Fabrice Jammes <fabrice.jammes@in2p3.fr> (09.17.2013)
# -------------------------------------------------------------------------------

# Make sure umask is sane
umask 022

#
# Use LSB init script functions for printing messages, if possible
#
lsb_functions="/lib/lsb/init-functions"
if test -f $lsb_functions ; then
  . $lsb_functions
else
  log_success_msg()
  {
    echo " SUCCESS! $@"
  }
  log_failure_msg()
  {
    echo " ERROR! $@"
  }
fi

# If we don't have LSB init functions, check alternate location
# (for getpidofproc)
etc_functions="/etc/init.d/functions"
if test ! -f $lsb_functions -a -f $etc_functions ; then
  . $etc_functions
fi

# Check if any of $pid (could be plural) are running
checkpid() {
    local i

    for i in $* ; do
        [ -d "/proc/$i" ] && return 0
    done
    return 1
}

# used for service launched in background (i.e. with "$cmd&" ) :
# - wait for the service to create its pid file during $service_startup_timeout
# - return success once pid file is created, failure otherwise
wait_for_pid () {
  verb="$1"
  manager_pid="$2"  # process ID of the program operating on the pid-file
  i=0
  avoid_race_condition="by checking again"
  while test $i -ne $service_startup_timeout ; do

    case "$verb" in
      'created')
        # wait for a PID-file to pop into existence.
        test -s $pidfile && i='' && break
        ;;
      'removed')
        # wait for this PID-file to disappear
        test ! -s $pidfile && i='' && break
        ;;
      *)
        echo "wait_for_pid () usage: wait_for_pid created|removed manager_pid"
        exit 1
        ;;
    esac

    # if manager isn't running, then pid-file will never be updated
    if test -n "$manager_pid"; then
      if kill -0 "$manager_pid" 2>/dev/null; then
        :  # the manager still runs
      else
        # The manager may have exited between the last pid-file check and now.
        if test -n "$avoid_race_condition"; then
          avoid_race_condition=""
          continue  # Check again.
        fi

        # there's nothing that will affect the file.
        log_failure_msg ": Manager of pid-file quit without updating file."
        return 1  # not waiting any more.
      fi
    fi

    echo -n "."
    i=`expr $i + 1`
    sleep 1
  done

  if test -z "$i" ; then
    # success
    return 0
  else
    # failure
    return 1
  fi
}


start() {
	local prog lockfile= pidfile= pid retval=2 cmd= timeout=0

	# Test syntax.
	if [ "$#" = 0 ] ; then
		echo $"Usage: start [-p pidfile] [-l lockfile] [-t timeout] {program} {cmd}"
		return 1
	fi
	if [ "$1" = "-p" ]; then
		pidfile=$2
		shift 2
	fi
	if [ "$1" = "-l" ]; then
		lockfile=$2
		shift 2
	fi
	if [ "$1" = "-t" ]; then
		timeout=$2
		shift 2
	fi
	prog=${1##*/}
	cmd=${2}

	if [ -z "${lockfile}" ]; then
		lockfile=/var/lock/subsys/${prog}
	fi

	if [ -z "${pidfile}" ]; then
		pidfile=/var/run/${prog}.pid
	fi

 	# check to see if it's already running
 	echo -n "Starting $prog"
	if [ -f ${pidfile} ]; then
                log_success_msg " : (already up)"
		retval=0
	elif [ "$status" = 2 ]; then
		log_failure_msg " : was dead but lockfile existed"
		retval=$status
	# TODO : use fgrep
	elif [ "$status" = 4 ]; then
		log_failure_msg " : could not access PID file"
		retval=$status
	else
            if [ ${timeout} = 0 ]; then
                start_mode='manage-pid'
            else
                start_mode='timeout'
            fi
            case "$start_mode" in
                'manage-pid')
                    # $prog manages its pidfile
                    # inspired by mysqld
                    eval ${cmd}
                    pid=$!
                    # Default value, in seconds, afterwhich the script should timeout waiting
                    # for server start.
                    service_startup_timeout=900
                    wait_for_pid created $pid
                    retval=$?
                    ;;
                'timeout')
                    # $prog doesn't manage its pidfile : weak
                    # used by qserv-master
                    eval ${cmd}
                    pid=$!
                    sleep ${timeout}
                    checkpid ${pid} && retval=0
                    # Create pid file
                    if [ ${retval} -eq 0 ]; then
                            echo ${pid} > ${pidfile}
                    fi
                    ;;
            esac

	    if [ ${retval} -eq 0 ]; then
	        touch ${lockfile}
                log_success_msg
	    else
                log_failure_msg
	    fi
	fi
	return ${retval}
}


stop() {
       local prog lockfile= pidfile= timeout=0

	# Test syntax.
	if [ "$#" = 0 ] ; then
		echo $"Usage: stop [-p pidfile] [-l lockfile] {program}"
		return 1
	fi
	if [ "$1" = "-p" ]; then
		pidfile=$2
		shift 2
	fi
	if [ "$1" = "-l" ]; then
		lockfile=$2
		shift 2
	fi
	prog=${1##*/}

	if [ -z "${lockfile}" ]; then
		lockfile=/var/lock/subsys/${prog}
	fi

	if [ -z "${pidfile}" ]; then
		pidfile=/var/run/${prog}.pid
	fi

        echo -n "Stopping $prog"
        if [ ! -f "$pidfile" ]; then
	    # not running; per LSB standards this is "ok"
            log_failure_msg " : PID file could not be found"
	    retval=999
        else
	    log_success_msg
	    killproc -p ${pidfile} $prog
	    retval=$?
            [ ${retval} = 0 ] && rm -f ${lockfile} ${pidfile}
        fi
        return ${retval}
}

# Return LSB status
# If the status action is requested, the init script will return the following exit status codes.
#
# 0	program is running or service is OK
# 1	program is dead and /var/run pid file exists
# 2	program is dead and /var/lock lock file exists
# 3	program is not running
# 4	program or service status is unknown
status_of_proc () {
    local pidfile daemon name status OPTIND

    pidfile=
    OPTIND=1
    while getopts p: opt ; do
        case "$opt" in
            p)  pidfile="$OPTARG";;
        esac
    done
    shift $(($OPTIND - 1))

    if [ -n "$pidfile" ]; then
        pidfileopt="-p $pidfile"
    fi
    daemon="$1"
    name="$2"

    status="0"
    pidofproc $pidfileopt $daemon >/dev/null || status="$?"
    if [ "$status" = 0 ]; then
	read pid < $pidfile	
        log_success_msg "$name is running (${pid})"
        return 0
    elif [ "$status" = 1 ]; then
        log_failure_msg "$name is dead but PID file exists"
        echo "  see $pidfile"
        return $status
    elif [ "$status" = 2 ]; then
        log_failure_msg "$name is dead but lockfile exists"
        return $status
    elif [ "$status" = 4 ]; then
        log_failure_msg "could not access PID file for $name"
        return $status
    else
        log_failure_msg "$name is not running"
        return $status
    fi
}

source_sysconfig() {
    if [ "$#" -ne 1 ] ; then
            echo "Usage: $0 {initd_dir}"
            return 1
    fi
    local_etc_dir=`dirname $1`
    local_sysconfig_file=${local_etc_dir}/sysconfig/qserv
    if [ -r ${local_sysconfig_file} ]; then
	. ${local_sysconfig_file}
    else
	log_failure_msg "Unable to load Qserv configuration: \
${local_sysconfig_file}"
	exit 1
    fi
}

check_permissions() {
    if [ "$#" -ne 3 ] ; then
            echo "Usage: $0 perms(r|w) service varname"
            return 1
    fi
    local_access_type="$1"
    local_service_name="$2"
    local_varname="$3"
    # value of variable named varname will be stored in local_dir_location
    eval "local_dir_location=\$$local_varname"
    local_err_msg="Failure while starting ${NAME}:"
    case "$local_access_type" in "w")
        if [ ! -w "${local_dir_location}" -o ! -d "${local_dir_location}" ]; then
            log_failure_msg "${local_err_msg} \$${local_varname} isn't writable"
            exit 1
        fi
    ;;
    "r")
        if [ ! -r "${local_dir_location}" -o ! -d "${local_dir_location}" ]; then
            log_failure_msg "${local_err_msg} \$${local_varname} isn't readable"
            exit 1
        fi
    ;;
    *)
        echo "$0 first parameter must be 'r' or 'w'"
        exit 1
    ;;
    esac
}

check_readable() {
    if [ "$#" -ne 2 ] ; then
            echo "Usage: $0 service varname"
            exit 1
    fi
    check_permissions "r" "$1" "$2"
}

check_writable() {
    if [ "$#" -ne 2 ] ; then
            echo "Usage: $0 service varname"
            exit 1
    fi
    check_permissions "w" "$1" "$2"
}
