#!/bin/bash
# kiosek startup script
# (C) bodik 2004
# (C) steve 2005
# (C) fous 2007, 2015

### BEGIN INIT INFO
# Provides:          kiosek
# Required-Start:    $local_fs $remote_fs $syslog $network $time
# Required-Stop:     $remote_fs
# Default-Start:     2 3 4 5
# Default-Stop:      0 1 6
# Short-Description: TV kiosek
# Description:       TV kiosek 
### END INIT INFO

PATH="/bin:/sbin:/usr/bin:/usr/sbin:/usr/X11R6/bin:/usr/local/bin:."
export PATH

umask 022

### nacteme globalni nastaveni kiosku
source /etc/kiosek.conf

# funkce pro import certifikatu
certimport()
{
SRCCERTDIR=$1
DSTCERTDIR=$2

for CERTFILE in $SRCCERTDIR/*
do
  if [ ! -f $CERTFILE ]; then
    continue
  fi
  CERTNAME=${CERTFILE##*/}
  CERTNAME=${CERTNAME%.*}
  certutil -A -n $CERTNAME -t CT,C,C -i $CERTFILE -d $DSTCERTDIR
done

chmod 644 $DSTCERTDIR/*.db
}

SU=`which su.local || which su`

case "$1" in
  start)
        echo "Executing $0 $1:"

	#import certificates
	certimport $ZCUCERTS $CERTDBPATH >/dev/null

	# start kiosk
	$SU -l $USER -c "$HOMEDIR/kiosek-start.sh >/dev/null" & echo $! > $KIOSEKPID

	# start daemons
	/usr/local/bin/connectivity.sh & echo $! > $CONNPID
	
        ;;
  stop)
        # Stop daemons.
	pkill -u $USER && rm -f $KIOSEKPID && echo "Kiosk shutdown."
	kill -TERM `cat $UNCLPID` 1>/dev/null && rm -f $UNCLPID && echo "Unclutter shutdown."
	kill -TERM `cat $DEVILPID` 1>/dev/null && rm -f $DEVILPID && echo "Devilspie shutdown."
	kill `cat $XPID` && rm -f $XPID && echo "X shutdown."
	kill -TERM `cat $CONNPID` 1>/dev/null && rm -f $CONNPID && echo "Connectivity checker shut down."
        ;;
  restart) 
        $0 stop
	sleep 2s
	$0 start
	;;
  vidplay)
  	# start xserver
	if [ -z $2 ]; then 
		echo "too few parameters"
		exit 1;
	fi
	#DISPLAY=:3 vlc -f $2 & echo $! > $VIDEOPID
	DISPLAY=:3 vlc --loop --rt-priority -f $2 & echo $! > $VIDEOPID
	;;
  vidstop)
  	kill -TERM `cat $VIDEOPID` 1>/dev/null && rm -f $VIDEOPID && echo "vlc closed."
	;;
  pptplay)
  	if [ -z $2 ]; then
		echo "too few parameters"
		exit 1;
	fi
	/usr/bin/ooimpress -norestore -show $2 & echo $! > $VIDEOPID
	;;
  pptstop)
         kill -TERM `cat $VIDEOPID` 1>/dev/null && rm -f $VIDEOPID
         ;;
  # tohle je hook pro resuscitaci televizniho kiosku z rozpojeni site, nicmene kdyz promitame
  # neco specialniho tak nechci aby mi to prekrejvalo promitani takze ho nebudu resuscitovat a pockam az
  # promitani skonci ;)
  killfire)
  	if [ ! -f $VIDEOPID ]; then
		# na lennym funguje jinak nez na etch, to by se jeden posral
		#/usr/bin/killall -q firefox-bin
		/bin/kill `pidof iceweasel`
	fi
	;;
  *)
        echo "Usage: $0 {start|stop|restart|vidplay|vidstop|pptplay|pptstop|killfire}"
        exit 1
esac

exit 0
