#!/bin/sh
# sepostgresql	This is the init script for starting up SE-PostgreSQL
#
# chkconfig: - 62 38
# description: Starts and stops the SE-PostgreSQL backend daemon
# processname: postmaster
# pidfile: /var/run/postmaster.pid
#---------------------------------------------------------------------

PGVERSION="8.3.7"
PGMAJORVERSION=`echo "$PGVERSION" | sed 's/^\([0-9]*\.[0-9a-z]*\).*$/\1/'`
SEPGVERSION="2.1770"

# source function library
. /etc/rc.d/init.d/functions

# get config
. /etc/sysconfig/network

# find the name of the script
NAME=`basename $0`
if [ ${NAME:0:1} = "S" -o ${NAME:0:1} = "K" ]; then
    NAME=${NAME:3}
fi

# set defaults for configurable variables
SEPGSQL_BIN="/usr/bin"
SEPGSQL_DATA="/var/lib/sepgsql/data"
SEPGSQL_OPTS="-i -p 5432"
SEPGSQL_STARTUP_LOG="/var/lib/sepgsql/pgstartup.log"
SEPGSQL_LOG="/var/log/sepostgresql.log"
SEPGSQL_FALLBACK_CONTEXT="user_u:user_r:user_t"

# override defaults from /etc/sysconfig/sepostgresql
test -f /etc/sysconfig/${NAME} && . /etc/sysconfig/${NAME}

export SEPGSQL_FALLBACK_CONTEXT

# Check that networking is up.
test "${NETWORKING}" = "no" && exit 0
test -f "${SEPGSQL_BIN}/sepostgres" || exit 1

script_result=0

do_start() {
    PSQL_START=$"Starting ${NAME} service: "
    echo -n "$PSQL_START"

    # make sure startup-time log file is valid
    if [ ! -e "${SEPGSQL_STARTUP_LOG}" -a ! -h "${SEPGSQL_STARTUP_LOG}" ]; then
        touch "${SEPGSQL_STARTUP_LOG}" || exit 1
        chown sepgsql:sepgsql "${SEPGSQL_STARTUP_LOG}"
        chmod 600 "${SEPGSQL_STARTUP_LOG}"
        /sbin/restorecon "${SEPGSQL_STARTUP_LOG}"
    fi
    
    # check for the SEPGSQL_DATA structure
    if [ -f "${SEPGSQL_DATA}/PG_VERSION" ] && [ -d "${SEPGSQL_DATA}/base" ]; then
        if [ x`cat "${SEPGSQL_DATA}/PG_VERSION"` != x"${PGMAJORVERSION}" ]; then
            echo_failure
            echo
            echo "HINT: An old version of the database format was found."
            echo "HINT: You need to upgrade the data format before using SE-PostgreSQL."
            exit 1
        fi
    else
        echo_failure
        echo
        echo "HINT: ${SEPGSQL_DATA} is missing."
        echo "HINT: Use '/etc/init.d/${NAME} initdb'"
        echo "HINT:   to initialize the database cluster first."
        exit 1
    fi

    # make sure SEPGSQL_LOG
    touch ${SEPGSQL_LOG}
    chown sepgsql:sepgsql ${SEPGSQL_LOG}
    chmod 600 ${SEPGSQL_LOG}
    test -x /sbin/restorecon && /sbin/restorecon ${SEPGSQL_LOG}

    cd ${SEPGSQL_BIN}
    /sbin/runuser sepgsql -c "./sepg_ctl -l ${SEPGSQL_LOG} -D ${SEPGSQL_DATA} -o '${SEPGSQL_OPTS}' start" \
        >> ${SEPGSQL_STARTUP_LOG} 2>&1 < /dev/null
    sleep 1
    PID=`/sbin/runuser sepgsql -c "./sepg_ctl -D ${SEPGSQL_DATA} status 2>/dev/null \
	| sed 's/^.*PID: //g' | sed 's/[^0-9].*$//g'"`
    if [ ${PIPESTATUS[0]} -eq 0 ]; then
        echo "$PID" > "/var/run/${NAME}.pid"
        touch "/var/lock/subsys/${NAME}.lock"
        echo_success
    else
        script_result=1
        echo_failure
    fi
    echo
}

do_stop() {
    echo -n $"Stopping ${NAME} service: "
    cd ${SEPGSQL_BIN}
    /sbin/runuser sepgsql -c "./sepg_ctl -D ${SEPGSQL_DATA} stop" \
        >> ${SEPGSQL_STARTUP_LOG} 2>&1 < /dev/null
    ret=$?
    if [ $ret -eq 0 ]; then
        echo_success
    else
        echo_failure
        script_result=1
    fi
    echo
    rm -f "/var/run/${NAME}.pid"
    rm -f "/var/lock/subsys/${NAME}.lock"
}

do_status() {
    cd ${SEPGSQL_BIN}
    /sbin/runuser sepgsql -- -c "./sepg_ctl -D ${SEPGSQL_DATA} status" 2>/dev/null \
        | head -1 | sed "s/^sepg_ctl:/${NAME}:/g"

    if [ ${PIPESTATUS[0]} -ne 0 ]; then
        script_result=3
        test -e "/var/run/${NAME}.pid"          && script_result=1
        test -e "/var/lock/subsys/${NAME}.lock" && script_result=2
    fi
}

do_condrestart() {
    cd ${SEPGSQL_BIN}
    /sbin/runuser sepgsql -- -c "./sepg_ctl -D ${SEPGSQL_DATA} status" &>/dev/null && do_stop && do_start
}

do_condstop() {
    cd ${SEPGSQL_BIN}
    /sbin/runuser sepgsql -- -c "./sepg_ctl -D ${SEPGSQL_DATA} status" &>/dev/null && do_stop
}

do_reload() {
    echo -n $"Reloading ${NAME} service: "
    cd ${SEPGSQL_BIN}
    /sbin/runuser sepgsql -- -c "./sepg_ctl -D ${SEPGSQL_DATA} reload" &>/dev/null < /dev/null
    if [ $? -eq 0 ]; then
        echo_success
    else
        echo_failure
        script_result=1
    fi
    echo
}

do_initdb() {
    echo -n $"Initializing database: "

    if [ -f "${SEPGSQL_DATA}/PG_VERSION" ]; then
	echo_failure
	echo
	echo "HINT: Data directory is not empty"
	script_result=1
    else
	if [ ! -e "${SEPGSQL_DATA}" -a ! -h "${SEPGSQL_DATA}" ]; then
	    mkdir -p "${SEPGSQL_DATA}" || exit 1
	    chown sepgsql:sepgsql "${SEPGSQL_DATA}"
	    chmod 600 "${SEPGSQL_DATA}"
	fi
	# cleanup SELinux labeling for "${SEPGSQL_DATA}"
	test -x /sbin/restorecon && /sbin/restorecon -R "${SEPGSQL_DATA}"
	# Initialize the database
        cd ${SEPGSQL_BIN}
	/sbin/runuser -- sepgsql -c "./initdb.sepgsql --pgace-feature=selinux -A 'ident sameuser' ${SEPGSQL_DATA}" \
	    >> "${SEPGSQL_STARTUP_LOG}" 2>&1 < /dev/null
	if [ -f "${SEPGSQL_DATA}/PG_VERSION" ]; then
	    echo_success
	else
	    echo_failure
	    script_result=1
	fi
	echo
    fi
}

# see how we were called.
case "$1" in
    start)
        do_start
	;;
    stop)
        do_stop
	;;
    status)
	do_status
	;;
    restart)
	do_stop
	do_start
	;;
    condrestart)
        do_condrestart
	;;
    condstop)
        do_condstop
	;;
    reload|force-reload)
        do_reload
	;;
    initdb)
        do_initdb
	;;
    *)
        echo $"Usage: $0 {start|stop|status|restart|condrestart|condstop|reload|force-reload|initdb}"
        exit 1
    ;;
esac

exit $script_result
