#!/usr/bin/perl

# Copyright (C) 2012 by Debian Edu project, http://wiki.debian.org/DebianEdu
#       Mike Gabriel <mike.gabriel@das-netzwerkteam.de>

# Internet Kiosk 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.
#
# Internet Kiosk 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 this program; if not, write to the
# Free Software Foundation, Inc.,
# 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA.

use strict;
use IPC::Open3;

sub catch_term
{
	system ("killall firefox-bin");
	system ("killall Xorg");
	system ("killall pulseaudio");
	unlink("/var/run/internet-kiosk.pid");
	exit;
}

sub check_x
{
	my $x=`ps ax | grep X`;
	if ( $x=~m/usr.bin.(Xorg|X)/ )
	{
		return 1;
	}
	return 0;
}

sub check_pulse
{
	my $pulse=`ps ax | grep pulse`;

	if ( $pulse=~m/pulseaudio/)
	{
		return 1;
	}
	return 0;
}

sub check_browser
{
	my $browser=`ps ax | grep firef`;

	if ( $browser=~m/firefox-bin/)
	{
		return 1;
	}
	return 0;
}

# make sure the .halt file is not present when we start this script
system("rm -f /home/internet-kiosk/.halt");

# start with a clean .mozilla config dir
system("rm -Rf /home/internet-kiosk/.mozilla");

# pre-load the thinclient initialization script
system("su internet-kiosk -c \". /etc/internet-kiosk/ik_init\"");

# fork to background, check if forked state is ok...
my $pid = fork();
if (not defined $pid) 
{
	print "resources not avilable.\n";
} 
elsif ($pid != 0) 
{
	open (F,">/var/run/internet-kiosk.pid");
	print F "$pid\n";
	close(F);
}
elsif ($pid == 0 )
{

	# silence this daemon script completely...
	close(STDOUT);
	close(STDERR);

	$SIG{TERM}=\&catch_term;

	while(sleep 1)
	{
		# test if shutdown is requested
		if ( -e "/home/internet-kiosk/.halt")
		{
			# when used with plymouth, make sure we stay on the correct terminal
			system("killall Xorg");
			system("shutdown -h now");
		}
		# test if XServer is running, if not launch it...
		if ( check_x() ==0 )
		{
			system("/usr/bin/Xorg -br &");
			system("su - internet-kiosk -c \"DISPLAY=:0 matchbox-window-manager &\"");
			system("su - internet-kiosk -c \"which unclutter 1>/dev/null && DISPLAY=:0 unclutter&\"");
		}

		# test if pulseaudio is running, if not launch it...
		if ( !check_pulse() )
		{
			system("su - internet-kiosk -c \"DISPLAY=:0 pulseaudio -D -n \\
			-L 'module-native-protocol-tcp port=4713' \\
			-L 'module-esound-protocol-tcp port=16001' \\
			-L 'module-udev-detect' --exit-idle-time=65535\"");
		}

		# test if web browser is running, if not launch it...
		my $ic_pid = 0;
		if ( !check_browser() )
		{
			local *IN, *OUT, *ERR;
			$ic_pid = eval {
				open3( \*IN, \*OUT, \*ERR, "su - internet-kiosk -c 'DISPLAY=:0 bash -c \"/etc/internet-kiosk/ik_start &\"'");
			};
			## give the web browser some time to start
			sleep(3);
			## wait for browser to finish
			if ( check_browser() )
			{
				waitpid( $ic_pid, 0 );
			}
		}
	}
}
