#!/bin/bash
# -*- coding: utf-8; -*-
#
# (c) 2016 Siveo http://www.siveo.net
#
# $Id: pulse2-register-pxe 30 2008-02-08 16:40:54Z nrueff $
#
# This file is part of Pulse 2, http://www.siveo.net
#
# Pulse 2 is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2 of the License, or
# (at your option) any later version.
#
# Pulse 2 is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with Pulse 2; if not, write to the Free Software
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
# MA 02110-1301, USA.
#
### BEGIN INIT INFO
# Provides:          pulse2-register-pxe.py
# Required-Start:    $network
# Required-Stop:     $network
# Default-Start:     2 3 4 5
# Default-Stop:      0 1 6
# Short-Description: Start and stop pulse2 Register PXE
# Description:       Start and stop pulse2 Register PXE
### END INIT INFO

# Redhat based distributions specific
# chkconfig: 345 99 60
# description: Pulse2 Register PXE

# Variables
DESC="Pulse2 Register PXE"
DAEMON="/usr/sbin/pulse2-register-pxe.py"

# Function to start daemon
start () {
        echo -n "Starting ${DESC} : "
    if [ -f /var/run/pulse2-register-pxe.pid ]; then
        echo "already running"
        return 0
    fi
        ${DAEMON} >/dev/null 2>&1
        RETVAL=$?
        # If return code is 0, everything went fine
        if [ ${RETVAL} -eq 0 ]
          then
            echo "done."
          else
            echo "failed."
        fi
        return ${RETVAL}
}

# Function to stop daemon
stop () {
        echo -n $"Stopping ${DESC} : "
    if [ ! -f /var/run/pulse2-register-pxe.pid ]; then
        echo "no pid"
        return 0
    fi
    kill -9 `cat /var/run/pulse2-register-pxe.pid`
    rm -f /var/run/pulse2-register-pxe.pid
    # If return code is 0, everything went fine
        RETVAL=$?
    if [ ${RETVAL} -eq 0 ]
          then
            echo "done."
      else
            echo "failed."
    fi
        return ${RETVAL}
}

# Function to restart (run stop, then start)
restart() {
        stop
        sleep 1
        start
}

case $1 in
        start)
                start
        ;;
        stop)
                stop
        ;;
        restart | force-reload)
                restart
        ;;
        *)

        echo "Usage: ${0} {start|stop|restart|force-reload}"
        exit 1
esac

exit ${RETVAL}
