#!/bin/sh
#
### BEGIN INIT INFO
# Provides: openwsmand
# Required-Start: $remote_fs
# Required-Stop: $network
# Default-Start:
# Default-Stop:
# Short-Description: Openwsman Daemon
# Description: openwsmand
#       Start/Stop the Openwsman Daemon
### END INIT INFO
#
#
# chkconfig: - 36 64
# description: Openwsman Daemon
# processname: openwsmand

NAME=openwsmand
DAEMON=/usr/sbin/$NAME
OPTIONS=-S
PIDFILE=/var/run/$NAME.pid

if [ $EUID != 0 ]; then
 echo "This script must be run as root."
 exit 1;
fi

if [ "$DESCRIPTIVE" = "" ]; then
 DESCRIPTIVE="Openwsman Daemon"
fi

lockfile=${SVIlock:-/var/lock/subsys/$NAME}

[ -x $DAEMON ] || exit 0

if [ -f "/etc/sysconfig/openwsman" ]; then
    . /etc/sysconfig/openwsman
fi

if [ -f "/etc/SuSe-release" ]; then
     # See how we were called.
     . /etc/rc.status

     # Reset status of this service
     rc_reset
fi

case "$1" in
    start)
    if [ ! -f "/etc/openwsman/serverkey.pem" ]; then
        if [ -f "/etc/ssl/servercerts/servercert.pem" \
                -a -f "/etc/ssl/servercerts/serverkey.pem" ]; then
            echo "Using common server certificate /etc/ssl/servercerts/servercert.pem"
            ln -s /etc/ssl/servercerts/server{cert,key}.pem /etc/openwsman/
        else
            echo "Generating Openwsman server public certificate and private key"
            FQDN=`hostname --fqdn`
            if [ "x${FQDN}" = "x" ]; then
                FQDN=localhost.localdomain
            fi
cat << EOF | sh /etc/openwsman/owsmangencert.sh > /dev/null 2>&1
--
SomeState
SomeCity
SomeOrganization
SomeOrganizationalUnit
${FQDN}
root@${FQDN}
EOF
        fi
    fi

    # Start daemons.
    echo -n "Starting the $DESCRIPTIVE"
    if [ -f "/etc/SuSe-release" ]; then
    	startproc -p $PIDFILE $DAEMON > /dev/null 2>&1
    	rc_status -v
    elif [ -f "/etc/redhat-release" ]; then
    	$DAEMON -S && echo "          done." || echo "   failed."
    fi
    
    touch $lockfile
    ;;

    stop)
    # Stop daemons.
    echo -n "Shutting down $DESCRIPTIVE"
    if [ -f "/etc/SuSe-release" ]; then
       killproc -p $PIDFILE -TERM $DAEMON
       rc_status -v
    elif [ -f "/etc/redhat-release" ]; then
       kill -9 `pidof openwsmand` > /dev/null 2>&1
          if [ $? -eq 0 ]; then
             echo "         done"
          fi
    fi
    rm -f $lockfile
    ;;

    restart|force-reload)
    $0 stop
    $0 start

    ;;

    reload)
    echo -n "Reloading service $DESCRIPTIVE"
    if [ -f "/etc-SuSe-release" ]; then
    	killproc -p $PIDFILE  -HUP $DAEMON
    	rc_status -v
    elif [ -f "/etc/redhat-release" ]; then
    	killall -HUP openwsmad && echo "            done."|| echo "    failed."
    fi
    ;;

    status)
    echo -n "Checking for service $DESCRIPTIVE"
    if [ -f "/etc/SuSe-release" ]; then
        checkproc -p $PIDFILE $DAEMON
        rc_status -v
    elif [ -f "/etc/redhat-release" ]; then
        pidof opewsmand > /dev/null 2>&1
        if [ $? -eq 0 ]; then
           echo "         running"
        else
           echo "         stopped"
        fi 
    fi
        
    ;;
    condrestart)
       [ -f $lockfile ] && restart || :
    ;;

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

if [ -f "/etc/SuSe-release" ]; then
rc_exit
elif [ -f "/etc/redhat-release" ]; then
exit 0 
fi
