#! /bin/sh
#
# Example init.d script with LSB support.
#
# Please read this init.d carefully and modify the sections to
# adjust it to the program you want to run.
#
# Copyright (c) 2007 Javier Fernandez-Sanguino <jfs@debian.org>
#
# This is free software; you may 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,
# or (at your option) any later version.
#
# This 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 with
# the Debian operating system, in /usr/share/common-licenses/GPL;  if
# not, write to the Free Software Foundation, Inc., 59 Temple Place,
# Suite 330, Boston, MA 02111-1307 USA
#
### BEGIN INIT INFO
# Provides:          kwartz-deploy
# Required-Start:    $network $local_fs
# Required-Stop:
# Should-Start:      $named
# Should-Stop:
# Default-Start:     2 3 4 5
# Default-Stop:      0 1 6
# Short-Description: PULSE service (kwartz-deploy)
# Description:       <Enter a long description of the software>
#                    <...>
#                    <...>
### END INIT INFO

# Variables
DESC="Pulse 2 Imaging Server"
DAEMON="/usr/sbin/pulse2-imaging-server"
DAEMON_NT="/usr/rembo5/joindom"
DAEMON_ATFTP="/usr/sbin/atftpd"
DAEMON_TFTPHPA="/usr/sbin/tftpd-hpa"
RETVAL=0

# Function to start daemon
start () {
	[ ! -f /var/kwartz/PULSE.ok -a ! -f /var/kwartz/REMBO.ok ] && return 0
        echo -n "Starting ${DESC} : "

    # On efface les image.iuuid
    find /home/kwartz/deploy -name image.iuuid -exec rm -vf {} \;

    if [ -f /var/run/pulse2-imaging-server.pid ]; then
        echo "already running"
        return 0
    fi

    # Verif que rpcbind n'occupe pas le port 1001
    netstat -apn | grep -q 1001 && {
			[ -f /etc/init.d/rpcbind ] && invoke-rc.d rpcbind restart
			[ -f /etc/init.d/statd ] && invoke-rc.d statd restart
	}	

    ${DAEMON} >/dev/null 2>&1
    RETVAL=$?
    # If return code is 0, everything went fine
    if [ $RETVAL -eq 0 ]; then

		# ## Nettoyage des images incompletes : ##
		#	cas arret du poste pendant creation
		find /home/kwartz/deploy/masters -name "conf.tmp" -exec dirname {} \; | xargs rm -rf

		#	cas echec de creation
		#   On nettoie les repertoires qui ne contiennent que log.txt, plus vieux que 15 jours
        find /home/kwartz/deploy/masters -maxdepth 1 -type d -exec bash -c '
	    for dirpath do
	        ok=true
	        seen_files=false
	        set -- "$dirpath"/*
	        for name do
	            [ -d "$name" ] && continue  # skip dirs
	            seen_files=true
	            case "${name##*/}" in
	                log.txt) 
	                    if ! test `find "$name" -mtime +15`; then
	                        ok=false; break
	                    fi
	                    # if older, do nothing
	                    ;;
	                *) ok=false; break
	            esac
	        done
	
	        "$seen_files" && "$ok" && printf "%s\n" "$dirpath"
	    done' _ {} \; | xargs rm -rf
		# ##

		if [ ! -f /var/kwartz/REMBO.ok ]; then
			[ -f $DAEMON_TFTPHPA ] || DAEMON_TFTPHPA="/usr/sbin/in.tftpd"
			start-stop-daemon --start -b -m --pidfile /var/run/remboatftp.pid --exec $DAEMON_TFTPHPA -- --foreground --address 0.0.0.0:69 --secure --blocksize 512 -vvv --retransmit 100000  /home/kwartz/deploy
		fi
		if [ ! -f /var/kwartz/REMBO.ok ]; then
			pgrep joindom
			[ $? -eq 0 ] || start-stop-daemon --quiet --start --exec $DAEMON_NT -- -D -s -p 9998 -v 1 </dev/null >/dev/null 2>&1
		fi
		echo "done."
    else
		echo "$RETVAL failed."
	fi
	return $RETVAL
}

# Function to stop daemon
stop () {
    echo -n "Stopping ${DESC} : "
	if [ -f /var/run/remboatftp.pid ]; then
		start-stop-daemon --oknodo --stop --retry 5 --quiet --pidfile /var/run/remboatftp.pid
		rm -f /var/run/remboatftp.pid
	fi
	if [ ! -f /var/kwartz/REMBO.ok ]; then
		start-stop-daemon --stop --exec $DAEMON_NT
	fi
    if [ ! -f /var/run/pulse2-imaging-server.pid ]; then
        echo "no pid"
        return 0
    fi
    kill -9 `cat /var/run/pulse2-imaging-server.pid`
    rm -f /var/run/pulse2-imaging-server.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
