#! /bin/sh

# Author: pypons <pypons@fastwebnet.it>
# Avvia html2pop3 come demone
# Script modificato per funzionare su openSuSE da Stefano Bianchini
# Visitate openlinux: http://www.openlinux.eu
#
# /etc/init.d/html2pop3
#
# Shell functions sourced from /etc/rc.status:
#      rc_check         check and set local and overall rc status
#      rc_status        check and set local and overall rc status
#      rc_status -v     ditto but be verbose in local rc status
#      rc_status -v -r  ditto and clear the local rc status
#      rc_failed        set local and overall rc status to failed
#      rc_reset         clear local rc status (overall remains)
#      rc_exit          exit appropriate to overall rc status

### BEGIN INIT INFO
# Provides:       html2pop3
# Default-Start:  3 5
# Default-Stop:   0 6
# Short-Description: Run html2pop3 engine
# Description:    Run html2pop3 engine in service mode for allow retriewe email from providers with pop access is not allowed.
### END INIT INFO

# Modificare con il percorso assoluto dell'interprete java
JAVA="/etc/alternatives/jre/bin/java"
# Modificare con il percorso assoluto di html2pop3
HTML2POP3="/opt/html2pop3/"
# paramentri passati all'interprete java
COMANDO=" -cp html2pop3.jar:httpclient.jar:httpmail.jar:xercesImpl.jar:xmlParserAPIs.jar html2pop3"

. /etc/rc.status

# First reset status of this service
rc_reset

case "$1" in

  start)
            check=`pgrep -f -- "$COMANDO"`
	    if [ "$check" = "" ]; then
		cd $HTML2POP3
		echo -n "Avvio di html2pop3..."
		# html2pop3 in background...
		screen -d -m $JAVA $COMANDO
	    else
		echo -n "html2pop3 e' gia' in esecuzione"
	    fi
	    rc_status -v
	    ;;
  stop)
	        echo -n "Chiusura di html2pop3"
	        # Termina html2pop3 con killall attendendo l'avvenuta chiusura
	        pid=`pgrep -f -- "$COMANDO"`
	        kill -9 $pid
	    rc_status -v
	    ;;    
  status)
                echo -n "Controllo lo stato di html2pop3: "
                # Controlla lo stato html2pop3
                ps -p 1 `pgrep -f -- "$COMANDO"` |
		awk '{ORS="" ; if ($3 ~ /^[TWXZ]/) {print "1"} else print "0"}' |
		awk '{if ($1 > 0) {print "Il processo ha qualche problema oppure e` inattivo"} else {print "Il processo e` attivo"} }'
	    rc_status -v
	    ;;
  restart)
		echo -n "Chiusura di html2pop3"
		# Termina html2pop3 con killall attendendo l'avvenuta chiusura
		pid=`pgrep -f -- "$COMANDO"`
		kill -9 $pid
		cd $HTML2POP3
		echo -n "Riavvio di html2pop3.."
		# html2pop3 in background....
		$JAVA $COMANDO&
	    rc_status -v
	    ;;    
  *)
		echo "Sintassi: $0 {start|stop|status|restart}"
	    exit 1
            ;;

esac

rc_exit
