#!/usr/bin/perl

use strict;
use File::Basename;
use File::Spec;


my $tempdir = "C:\\temp";  # dir to store temporary files
my $pluckerdir = "C:\\Program Files\\plucker";  # dir where plucker is installed

my $python = "$pluckerdir\\python\\python.exe";  # to run plucker script
my $plucker = "$pluckerdir\\PyPlucker\\Spider.py";  # this script can run without operator
my $install = "H:\\agb\\palm\\Instapp.exe";  # Palm install tool

my $source = "tvgids";  # options: veronica, tvgids


if ($ARGV[0] eq "filter")
{
	# This script is now called to filter a page
	$source = $ARGV[1];  # the kind of source, see above
	my $fname = $ARGV[2];  # the (temporary) file with the page

	open FILE, "<$fname" || die "Cannot open $fname for reading: $!";
	my $contents = join "", <FILE>;
	my $modified = 0;
	close FILE;

	if ($source eq "tvgids")
	{
		if ($contents =~ /.*(<table.*?U heeft gezocht op:.*?lijst_tijd.*?<\/table>)/si)
		{
# This is a page with the program --> cut it up and make an own layout
			print "Filter page: time table\n";
			my @rows = split /<tr[^<]*?>/si, $1;

			$contents = "<html><head></head><body>\n";  # redefine contents

			my $idx = -1;  # just 1 before the first
			while (++$idx < @rows)
			{
				# retrieve the station
				if ($rows[$idx] =~ /U heeft gezocht op:.*?Zender:\s*([^<]*?)</si)
				{
					$contents .= "$1, ";  # this is the station
					last;
				}
			}
			while (++$idx < @rows)
			{
				# retrieve the date
				if ($rows[$idx] =~ /lijst_zender.*?<h3>(.*?)<\/h3>/si)
				{
					$contents .= "$1\n";  # this is the day
					last;
				}
			}
			$contents .= "<ul>\n";
			while (++$idx < @rows)
			{
				# process all entries
				my ($time) = $rows[$idx] =~ /lijst_tijd.*?>([^<]*?)</si;
				my ($program) = $rows[$idx] =~ /lijst_programma.*?>(.*?)<\/td>/si;
				next unless defined $time && defined $program;

				# beautify
				my ($start, $end) = $time =~ /(.*)-(.*)/;
				$start = "**:**" unless $start;
				$end = "**:**" unless $end;
				$time = "$start-$end";

				($program) = $program =~ /(<a href=\"detail.php.*?<\/a>)/si;  # only interesting part
				$program =~ s/[\r\n]+/ /g;
				# make some program light up
				if ($program =~ /\b(Babylon|Star Trek|Grand Prix|Kopspijkers|Crime Scene Investigation|CSI|The Simpsons|Noorderlicht|The comedy factory|The Muppet Show)\b|cabare/gi)
				{
					$program = "<b>$program</b>";
				}

				# put in contents
				$contents .= "<li>$time $program</li>\n";
			}
			$contents .= "</ul>\n";
			$contents .= "</body></html>";

			$modified = 1;
		}
		elsif ($contents =~ /.*(<table.*?form_beschrijving.*?<\/table>)/si)
		{
# This is a page with the description --> just take over the description
			print "Filter page: description\n";
			$contents = "<html><head></head><body>\n";  # redefine contents

			my @rows = split /<tr[^<]*?>/si, $1;

			my $icons;
			my $idx = -1;  # just 1 before the first
			while (++$idx < @rows)
			{
				# retrieve (form_beschrijving, content) pairs
				my ($key) = $rows[$idx] =~ /class="form_beschrijving".*?>(.*?)<\/td>/si;
				my ($value) = $rows[$idx] =~ /class="content".*?>(.*?)<\/td>/si;
				unless (defined $key && defined $value)
				{
					# It's not a (key, value) pair, maybe there are images inside
					while ($rows[$idx] =~ /(<img[^<]*?kijkwijzer[^<]*?>)/sig)
					{
						$icons .= $1;  # append the icon
					}
				}
				else
				{
					$contents .= "<u>$key</u> $value<br>\n";
				}
			}

			# Now, there may be some icons
			while (++$idx < @rows)
			{
			}

			$contents .= "<center>$icons</center>\n" if $icons;

			$contents .= "</body></html>";

			$modified = 1;
		}
		else
		{
			print "Filter page: not changed\n";
		}
	}
	else
	{
		exit;
	}

	if ($modified)
	{
		open FILE, ">$fname" || die "Cannot open $fname for writing: $!";
		print FILE $contents;
		close FILE;
	}
	
	exit;
}


sub geturl
{
	my ($station, $interval) = @_;
	# $station should be one of: see below.
	# $interval should be one of 0 (today), 1 (tomorrow), 2 (day after tomorrow)
	#   3 (day after day after tomorrow)

	my %stationnr = (
		ned1   =>   1,  #   1: Nederland 1
		ned2   =>   2,  #   2: Nederland 2
		ned3   =>   3,  #   3: Nederland 3
		rtl4   =>   4,  #   4: RTL 4
		rtl5   =>  31,  #  31: RTL 5
		net5   =>  37,  #  37: Net 5
		sbs6   =>  36,  #  36: SBS 6
		yorin  =>  46,  #  46: Yorin
		"v8"   =>  34,  #  34: V8 (for some reason, it doesn't work without the quotes)
		vrt1   =>   5,  #   5: VRT TV1
		ketnet =>   6,  #   6: KETNET/Canvas
		disc   =>  29,  #  29: Discovery
		cnn    =>  26,  #  26: CNN
		bbc1   =>   7,  #   7: BBC 1
		bbc2   =>   8,  #   8: BBC 2

		nl     => 101,  # 101: Nederlandstalig
		uk     => 103,  # 103: Engelstalig
		de     => 104,  # 104: Duitstalig
		fr     => 105,  # 105: Franstalig
		xx     => 106,  # 106: Overige zenders

		all    =>   0,  #   0: Alle zenders
	);

	my $st = $stationnr{$station};

	return "" unless defined $st;

	if ($source eq "veronica")
	{
		my $dat = (16+$interval) . "-06-02";  # !!! CORRECT THIS CONVERSION !!!
		return "http://media.veronica.nl/programmagegevens/detail.html?datum=$dat&zender=$st";
	}
	elsif ($source eq "tvgids")
	{
		return "http://www.tvgids.nl/zoekprogramma.php?station=$st&interval=$interval";
	}
	else
	{
		die "Wrong source.";
	}
}

my @localtime = localtime;  # current time
my ($dd, $mm, $yy, $dow) = @localtime[3..6];
my @dayname = ( "Zondag", "Maandag", "Dinsdag", "Woensdag", "Donderdag", "Vrijdag", "Zaterdag" );
$mm += 1;
$yy += 1900;
my $date = "$dd-$mm-$yy";

my $fname = "$tempdir\\tvgids.html";
(my $urlname = "file:$fname") =~ s/\\/\//g;
open FILE, ">$fname" || die "Cannot open $fname: $!";

print FILE <<"HTML";
<html>

<head>
<title>TV Gids</title>
</head>

<body>

<h3>TV Gids ($date)</h3>

HTML

foreach my $interval (0..3)
{
	print FILE "$dayname[($dow+$interval)%7]<br>\n";
	foreach my $station (qw/ned1 ned2 ned3 rtl4 rtl5 net5 sbs6 yorin v8 bbc1 bbc2 disc/)
	{
		my $url = geturl($station, $interval);
		print FILE "<a href=\"$url\">$station</a> &nbsp;&nbsp;\n";
	}
	print FILE "<br>\n";
}

print FILE <<"HTML";

</body>

</html>

HTML

close FILE;

$ENV{PLUCKERHOME} = $pluckerdir;

my $maxdepth;
if ($source eq "veronica")
{
	system $python, $plucker, "--pluckerdir=$tempdir", "--home-url=$urlname", "--doc-file=tvgids", "--doc-name=TV Gids", "--noimages", "--zlib-compression", "--maxdepth=2";
}
elsif ($source eq "tvgids")
{
	$ENV{PLUCKER_FILTER_PAGE} = "perl $0 filter $source";
	system $python, $plucker, "--pluckerdir=$tempdir", "--home-url=$urlname", "--doc-file=tvgids", "--doc-name=TV Gids", "--noimages", "--zlib-compression", "--maxdepth=3";
}
else
{
	die "Wrong source.";
}

system $install, "$tempdir\\tvgids.PDB";

print "Done\n";

