#!/bin/bash
#
# Copyright 2017 IBM Corp.
#
#    Licensed under the Apache License, Version 2.0 (the "License"); you may
#    not use this file except in compliance with the License. You may obtain
#    a copy of the License at
#
#         http://www.apache.org/licenses/LICENSE-2.0
#
#    Unless required by applicable law or agreed to in writing, software
#    distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
#    WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
#    License for the specific language governing permissions and limitations
#    under the License.
#
# Sophie Song created on Jun. 7th, 2016
#
### BEGIN INIT INFO
# Provides:          iucvserver
# Required-Start:    
# Should-Start:      
# Required-Stop:
# Should-Stop:
# Default-Start:     2 3 4 5
# Default-Stop:      0 1 6
# Description:       This shell script takes care of starting and stopping IUCV agent
### END INIT INFO

# Source function library
if [ -f "/etc/init.d/functions" ]; then
    . /etc/init.d/functions
fi

#the service name  for example: iucvserv
SNAME=iucvserv

#the full path and name of the daemon program
#Warning: The name of executable file must be identical with service name
PROG=/usr/bin/$SNAME


# start function
start() {
    #check the daemon status first
    if [ -f /var/lock/subsys/$SNAME ]
    then
        echo "$SNAME is already started!"
        exit 0;
    else
        echo "Starting $SNAME ..."
        $PROG start-as-daemon
        [ $? -eq 0 ] && touch /var/lock/subsys/$SNAME
        exit 0;
    fi
}

#stop function
stop() {
    echo "Stopping $SNAME ..."
    killproc $SNAME
    rm -rf /var/lock/subsys/$SNAME
}

case "$1" in
start)
  start
  ;;
stop)
  stop
  ;;
reload|restart)
  stop
  start
  ;;
status)
  status $SNAME
  ;;
*)
  echo $"Usage: $0 {start|stop|restart|status}"
  exit 1
esac
