#!/bin/bash
#	       
# chkconfig: 2345 99 1
### BEGIN INIT INFO
# Provides: libstoragemgmtd
# Required-Start: $remote_fs $local_fs
# Required-Stop: $remote_fs $local_fs
# Default-Start: 2 3 4 5
# Default-Stop: 0 1 6
# Short-Description: libStorageMgmt plug-in daemon
# Description: libStorageMgmt plug-in daemon
### END INIT INFO

. /lib/lsb/init-functions

NAME=lsmd
PROG=/usr/bin/$NAME
PID=/var/run/lsm/lsmd.pid
LOCK=/var/lock/subsys/libstoragemgmtd
DAEMON=libstoragemgmtd
PYTHON=/usr/bin/python

RETVAL=0
STATUS=0

if [ "`id -u`" != 0 ] ; then
	echo "Not root"
	exit 4
fi

check_dirs() {
	test -d /var/run/lsm || mkdir -p /var/run/lsm/ipc
	test -d /var/run/lsm/ipc || mkdir -p /var/run/lsm/ipc
	
	# And fix ownership
	chown libstoragemgmt.libstoragemgmt -R /var/run/lsm
}


start_lsm() {
	log_daemon_msg "Starting $NAME daemon:"
	start-stop-daemon --start --quiet --pidfile $PID --exec $PROG

	RETVAL=$?
	log_end_msg $RETVAL
}

stop_lsm() {
	log_daemon_msg "Stopping $NAME daemon:"
	start-stop-daemon --stop --quiet --signal TERM --pidfile $PID --exec $PYTHON -- --operation stop
	log_end_msg 0
}

# See how we were called.
case "$1" in
  start)
	check_dirs
	start_lsm

	;;
  force-stop|stop)
	stop_lsm
	;;
  force-reload | reload)
	check_dirs
	log_daemon_msg "Reloading $NAME daemon:"
	
	pkill -F $PID --signal HUP $NAME
	log_end_msg 0
	;;
  restart)
	stop_lsm
	check_dirs
	start_lsm
	;;

  status)
	status_of_proc -p $PID $PROG "libstoragemgmtd daemon" && return 0 || return $?
	;;

  *)
	echo $"Usage: $0 {start|stop|restart|reload|status|force-stop}"
	;;
esac

exit $RETVAL

