#!/bin/sh 
#
# $FreeBSD: ports/databases/cego/files/cego.in,v 1.2 2011/05/15 02:49:04 dougb Exp $
#

# PROVIDE: cego
# REQUIRE: LOGIN cleanvar
# KEYWORD: shutdown

#
# Add the following lines to /etc/rc.conf to enable cego:
# cego_enable (bool):      Set to "NO" by default.
#                          Set it to "YES" to enable cego
# cego_profiles (str):     Set to "" by default.
#                          Define your profiles here.
# cego_tablespc (str):     Set it to the tablespace
# cego_flags (str):        Set to "" by default.
#                          Extra flags passed to start command.

. /etc/rc.subr

name="cego"
rcvar=`set_rcvar`

command="/usr/local/bin/cego"

: ${cego_enable="NO"}
: ${cego_conf="/usr/local/cego"}
: ${cego_root="/usr/local/cego"}
: ${cego_user="cego"}
: ${cego_numdbthread="30"}
: ${cego_poolsize="3x1000"}

flags="--mode=daemon"

start_cmd="cego_start"
stop_cmd="cego_stop"  
status_cmd="cego_status"  

cegoprog=/usr/local/bin/cego

cego_start()
{
    echo "Starting cego database ${profile} ..."
    cd /usr/local/cego
    echo $cegoprog $command_args
    /usr/local/bin/sudo -u $cego_user $cegoprog $command_args
}

cego_stop()      
{
    echo "Stopping cego database ${profile} ..."
    cgpid=`cat ${cego_root}/${profile}.pid`
    /bin/echo -n " * Signaling PID:${myPID} ... "
    kill -INT ${cgpid} || { echo "Failed to signal PID:${cgpid}."; exit 1; }
    echo "Done."
    echo " * Waiting for lockfile to clear ..."
    echo '   This takes a few seconds. Please be patient!'
    myTimeout=1;
    while test -e ${cego_root}/${profile}.lck
    do
	test ${myTimeout} -ge 60 && { echo "Timeout reached. Exiting."; echo "This is NOT good."; exit 1; }
	sleep 1
	myTimeout=`expr ${myTimeout} + 1`;
    done
    echo "   Lockfile cleared in ${myTimeout} seconds."
    echo "Database shutdown complete." 
    echo ""
}

cego_status()
{
    if test -e ${cego_root}/${profile}.lck
    then
	echo "cego is up and running"
    else
	echo "cego is not running"
    fi
}

load_rc_config $name

if [ -n "$2" ]; then
        profile="$2"
        if [ "x${cego_profiles}" != "x" ]; then

		eval cego_tablespc="\${cego_${profile}_tablespc:-}"
                if [ "x${cego_tablespc}" = "x" ]; then
                        err 1 "You must define a tableset for db instance cego_${profile}_tablespc"
                fi

                eval cego_conf="\${cego_${profile}_conf:-${cego_conf}}"
                eval cego_root="\${cego_${profile}_root:-${cego_root}}"
		eval cego_user="\${cego_${profile}_user:-${cego_user}}"
		eval cego_numdbthread="\${cego_${profile}_numdbthread:-${cego_numdbthread}}"
		eval cego_poolsize="\${cego_${profile}_poolsize:-${cego_poolsize}}"
                
                required_files="${cego_conf}/${profile}.xml"
                eval cego_enable="\${cego_${profile}_enable:-${cego_enable}}"
                command_args="--mode=daemon --dbxml=${required_files} --tableset=${cego_tablespc} --numdbthread=${cego_numdbthread} \
--poolsize=${cego_poolsize} --lockfile=${cego_root}/${profile}.lck --logfile=${cego_root}/${profile}.log --pidfile=${cego_root}/${profile}.pid --protocol=serial"
                # echo "Setting command_args $command_args" 
        else
                warn "$0: extra argument ignored"
        fi
else
        if [ "x${cego_profiles}" != "x" -a "x$1" != "x" ]; then
                for profile in ${cego_profiles}; do
                        eval _enable="\${cego_${profile}_enable}"
                        case "x${_enable:-${cego_enable}}" in
                        x|x[Nn][Oo]|x[Nn][Oo][Nn][Ee])
                                continue
                                ;;
                        x[Yy][Ee][Ss])
                                ;;
                        *)
                                if test -z "$_enable"; then
                                        _var=cego_enable
                                else
                                        _var=cego_"${profile}"_enable
                                fi
                                warn "Bad value" \
                                    "'${_enable:-${cego_enable}}'" \
                                    "for ${_var}. " \
                                    "Profile ${profile} skipped."
                                continue
                                ;;
                        esac
                        echo "===> cego profile: ${profile}"
                        /usr/local/etc/rc.d/cego $1 ${profile}
                        retcode="$?"
                        if [ "0${retcode}" -ne 0 ]; then
                                failed="${profile} (${retcode}) ${failed:-}"
                        else
                                success="${profile} ${success:-}"
                        fi
                done
                exit 0
        fi
fi

cego_requirepidfile()
{
	if [ ! "0`check_pidfile ${cego_root}/${profile}.pid ${command}`" -gt 1 ]; then
		err 1 "${name} not running? (check $pidfile)."
	fi
}

run_rc_command "$1"
