#!/bin/sh
# chkconfig: 345 88 20
# after vstorage-fs (S56) and after shaman (S57)
#
### BEGIN INIT INFO
# Provides: ostor-cfgd
# Required-Start: $remote_fs
# Default-Start:  3 4 5
# Default-Stop: 0 1 2 6
# Short-Description: Object storage configuration service
### END INIT INFO

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

PID_FILE="/var/run/ostor/ffffffffffffffff.pid"
CFG_ROOT_FILE="/var/lib/ostor/root.cfg"
LOG_FILE="/var/log/ostor/ostorcfgd.log.gz"
SERVICE_BIN="/usr/bin/ostorcfg-service"
SUBSYS="ostor-cfgd"
HOST_ID_FILE="/etc/vstorage/host_id"
CONTROL_DIR="services/ffffffffffffffff/control"
USER="ostor"

if [ ! -d "/var/run/ostor" ]; then
        mkdir -p "/var/run/ostor"
        chgrp "$USER" "/var/run/ostor"
	chmod g+w "/var/run/ostor"
fi


get_lock_file() {
	echo "$1/$CONTROL_DIR/.lock" | sed "s#//#/#"
}

read_cfg_root() {
	if [ -f "$CFG_ROOT_FILE" ]; then
		cat "$CFG_ROOT_FILE" 2>/dev/null | head -n 1
	else
		echo ""
	fi
}

check_pid_name() {
        # check process own flock for 
        pid=$(cat "$PID_FILE" 2>/dev/null | head -n 1)
        ls -l /proc/$pid/fd 2>/dev/null | grep $LOCK_FILE >/dev/null
        return $?
}

check_monitor_pid() {
        __pids_var_run ignored "$PID_FILE"
        code="$?"
        case "$code" in
                0) # program is running (likely)
                        if check_pid_name ; then
                                # program is running
                                return 0
                        else
                                return 1
                        fi
                        ;;
                *)
                        return "$code"
                        ;;
        esac
}

test_cfg_root() {
	local root="$1"
	if [ -z "$root" ] ; then
		echo_failure
		echo
		echo "Path to configuration data not specified." 1>&2
		return 1
	fi

	if [ ! -d "$ROOT" ] ; then
		echo_failure
		echo
		echo "Directory '$ROOT' does not exist" 1>&2
		return 1
	fi

	return 0
}

service_is_local() {
	 [ ! -f "$ROOT/$CONTROL_DIR/host" ] && return 1
	target_host=$(cat "$ROOT/$CONTROL_DIR/host" 2>/dev/null | head -n 1)
	my_host=$(cat "$HOST_ID_FILE" 2>/dev/null | head -n 1)
	[ "$my_host" != "$target_host" ] && return 0

	return 1
}

start_service() {
	echo -n "Starting ostor-cfgd:"
	ROOT=$(read_cfg_root)
	test_cfg_root $ROOT
	if [ $? -ne 0 ]; then
		RETVAL=1
		return
	fi

	service_is_local
	if [ $? -eq 0 ] ; then
		echo_warn
		echo
		echo "Service registered on another host" 1>&2
		RETVAL=0
		return
	fi

	LOCK_FILE=$(get_lock_file $ROOT)
        check_monitor_pid
        case $? in
                1) # program is dead, pid file exists
                        rm -f $PID_FILE
                        ;;
                0) # program is running
                        echo_failure
                        echo
                        echo "Service is already running"
                        RETVAL=1
                        return
                        ;;
        esac

        $SERVICE_BIN -r "$ROOT" -l "$LOG_FILE" -D
        if [ $? -eq 0 ]; then
                [ -d "/var/lock/subsys" ] && touch "/var/lock/subsys/$SUBSYS"
                echo_success
        else
                echo_failure
                RETVAL=1
        fi
	echo
}

stop_service() {
	echo -n $"Shutting down ostor-cfgd: "
        if [ ! -f "$PID_FILE" ]; then
		[ -d "/var/lock/subsys" ] && rm -f "/var/lock/subsys/$SUBSYS"
		echo_success
		echo
                return
        fi

        if killproc -p "$PID_FILE" $SERVICE_BIN; then
                [ -d "/var/lock/subsys" ] && rm -f "/var/lock/subsys/$SUBSYS"
		echo_success
	else
		echo_failure
		RETVAL=1
        fi
	echo
}

condrestart_service() {
	[ ! -f "$PID_FILE" ] && return
	stop_service
	start_service
}

service_status() {
	ROOT=$(read_cfg_root)
	test_cfg_root $ROOT
	if [ $? -ne 0 ]; then
		RETVAL=1
		return
	fi

	service_is_local
	if [ $? -eq 0 ] ; then
		echo "Service registered on another host" 1>&2
		RETVAL=3
		return
	fi

	LOCK_FILE=$(get_lock_file $ROOT)
        check_monitor_pid
        RETVAL=$?
        case $RETVAL in
                1)
                        echo "Service is dead, pid file exists"
                        ;;
                0)
                        echo "Service is running"
                        ;;
                3)
                        echo "Service stopped"
                        ;;
        esac
}

# See how we were called.
RETVAL=0
case "$1" in
  start)
        start_service
        ;;
  stop)
        stop_service
        ;;
  status)
        service_status
        ;;
  restart)
	stop_service
	start_service
	;;
  condrestart)
	condrestart_service
	;;
  *)
        echo $"Usage: $0 {start|stop|status|restart|condrestart}"
        exit 2
esac
exit $RETVAL

