# Process info from infosat files
sub ReadExternalEPG_infosat
{	
	#infosat downloads a fixed set of 7 days
	if ($days2download >7) {
		$days2download=7;
	}
	$event_id=1000;	
	our @events="";
	
	#go through every day
	for ($days=0;$days < $days2download ;$days++) {
		doexit() if $please_exit;

		$now_string = DateCalc($heute,"+ $days tage",\$err,0);
		$now_string = UnixDate($now_string, "%m.%d");

		($month,$day) = split /\./,$now_string;

		#this is the day file from infosatepg, containing the EPG Data
		$infosatfile="$infosaturl/infosat_";
		$infosatfile="$infosaturl/infosat_$month\_$day";	

		open(FILE, "$infosatfile") || warn "cannot open $infosatfile: $!";
		print "infosat: reading $infosatfile ...\n";

		# Zuerst das ganze File in den Speicher, newlines entfernen. ###################################
		#
		while(<FILE>) {
			doexit() if $please_exit;
			chomp;
			s/\r$//;  # fricking' DOS
			$epg .= $_ unless /^...$/;
			}
		
		print "infosat: parsing $infosatfile ...\n";
		$epg =~ s/\212/\|/msg;
		

		# Jetzt die ganze Datei in einzelne Sender zerlegen ############################################
		#
		($removed,$epg) = ($1,$2) if $epg =~ m/^(.*?)(\@P:.+\@Q:\d\d:\d\d).*?$/ms;
		$epgchanneldata = "";
		($epgchanneldata,$epg) = ($1,$2) if $epg =~ m/^(\@P:.+?\@Q:\d\d:\d\d)(.*)$/ms;
		while ($epgchanneldata ne "") {
			doexit() if $please_exit;

			# Argh, scheiss Kruecke:  ##############################################################
			# Schauen, ob der Sender importiert werden soll.
			#
			$import = "no";
			$epgchannel,$epgdate,$epgendofday = "";
			while (1==1) {
				($epgchannel,$epgdate,$epgendofday) = ($1,$2,$3) 
				if $epgchanneldata =~ s/^\@P:(.*?)\(.*?\) (.*?) .*? (\d\d:\d\d)(\@E:.*)$/$4/ms;

				foreach $sender (keys(%chan)) {
					if ($chan{$sender}[5] eq $epgchannel && $chan_wanted{$sender} eq "4") {
						$import = $sender;
					}
				}

				# Nicht im Hash gefunden, weiter mit nächstes Sender. ##########################
				#
				last if $import eq "no";
			
				# Im Hash gefunden, also diesen Sender parsen. #################################
				#
				$epgchanneldata =~ s/\\//g;
				$eodpassed = "no";
								
								
				# alle Events dieses Senders suchen und abarbeiten #############################
				#
				$prev_ustoptime = "";
				$epgevent=$metainfo=$subtitle=$summary=$epgtitle=$genre=$country=$year=$duration = "";
				($epgevent,$epgchanneldata) = ($1,$2) 
				if $epgchanneldata =~ m/^(\@E:\d\d:\d\d.*?)(\@[EQ]:\d\d:\d\d.*)$/ms;
				while ($epgevent ne "") {
					doexit() if $please_exit;
					
					# here we are - das aktuelle event parsen und ins hash pushen ##########
					$summary=$subtitle = "";

					($epgevent,$summary) = ($1,$3)
					if $epgevent =~ m/^(\@E:\d\d:\d\d.*?)(\d+ \d+|)\@L:(.*)$/;
					($epgevent,$subtitle) = ($1,$3)
					if $epgevent =~ m/^(\@E:\d\d:\d\d.*?)(\d+ \d+|)\@S:(.*)$/;
					$epgevent =~ s/^\@E:(.*)$/$1/;


					# frickin' hack - Lokalsendung mit Hinweis auf Sat ?
					if ($summary=~ m/Sat\.:(.*)/) {
						$tmp = $1;
						$subtitle = "";
						$summary = "";
						$epgevent = "$1 $tmp"
						if $epgevent =~ m/^(\d\d:\d\d) (.*)$/;
						$tmp = "";
						($epgevent,$metainfo) = ($1,$2) if $epgevent =~ m/^(.*?)\|\((.*?)\)$/;
						($epgevent,$subtitle) = ($1,$2) if $epgevent =~ m/^(.*) - (.*?\(.*\))$/;
						($epgevent,$subtitle) = ($1,$2) if $epgevent =~ m/^(.*)(\(.*\))$/;
						}



					# kleinklein erledigen #################################################
					#
					($epgstarttime,$epgtitle) = ($1,$2)
					if $epgevent =~ m/^(\d\d:\d\d) (.*)$/;
					$epgustarttime = UnixDate("$epgdate $epgstarttime", "%s");
					if ($epgustarttime < UnixDate("$epgdate $epgendofday", "%s") && $eodpassed eq "yes") {
						$epgustarttime = UnixDate(DateCalc("$epgdate $epgstarttime","+ 1 tag",\$err,0), "%s");
						}			
					if (UnixDate("$epgdate $epgstarttime", "%s") > UnixDate("$epgdate $epgendofday", "%s")) {
						$eodpassed = "yes";
						} 
					if ($subtitle =~ m/^(.*)\((.*?, (\d+) Min.)\)$/ ) {
						$subtitle=$1;
						$metainfo=$2;
						$duration=$3;
						$subtitle =~ s/\|$//;
						
						$duration=~s/Min.//;
						$duration=~s/\s//g;
						$duration=$duration*60;
						} # if ($subtitle =~ m/^

						@help=split /,/,$metainfo;

						if ($#help == 1) {
							$genre = $help[0];
							$year = $help[1];
						}

						if ($#help == 2) {
							$genre = $help[0];
							$year = $help[1];
						}

						if ($#help == 3) {
							$genre = $help[0];
							$country = $help[1];
							$year = $help[2];
						}
						$genre =~ s/ \/ /\//g;
						$year = "" if $year =~ m|\d+ Min|;
						$type = "";
						($type,$genre) = ($1,$2) if $genre =~ m|^(.*)/(.*)$|;
						if ($type eq "") {
							$type = $genre;
							$genre = "";
						}
						$genre =~ s/-Serie//;

					if ($prev_ustoptime > $epgustarttime + 5*60) {
						print "dropping $epgtitle (". printHumantime($epgustarttime) ." starts before previous event ends ... )\n";
						$epgevent=$metainfo=$subtitle=$summary=$epgtitle=$genre=$country=$year=$duration = "";
						($epgevent,$epgchanneldata) = ($1,$2)
						if $epgchanneldata =~ m/^(\@E:\d\d:\d\d.*?)(\@[EQ]:\d\d:\d\d.*)$/ms;
						next;                                        
						}

					if ($duration eq "") {
						#print "guessing $epgtitle \n";
						$epgstoptime = "";
						$epgstoptime = $1 if $epgchanneldata =~ m/^\@E:(\d\d:\d\d)/;
						$epgustoptime = UnixDate("$epgdate $epgstoptime", "%s");
						if ($epgustoptime < UnixDate("$epgdate $epgendofday", "%s")) {
							$epgustoptime = UnixDate(DateCalc("$epgdate $epgstoptime","+ 1 tag",\$err,0), "%s");
							}			
						$duration = $epgustoptime - $epgustarttime;
					}
					$summary .= "|";
					$summary =~ s/\|\* /\|/g;

					$regie=$darsteller=$folge=$flags=$origtitle=$folge=$rate="";
					$regie = $1 if $summary =~ m/\|Regie: (.*?)\|/;
					$darsteller = $1 if $summary =~ m/\|Mit (.*?)\|/;
					$summary =~ s/Regie:.*?\|//;
					$summary =~ s/\|Mit .*?\|//;
					$folge = $1 if $subtitle =~ m/Folge (\d+)/;
					$folge = $1 if $subtitle =~ m/Kapitel (\d+)/;
					$flags .= "[Premiere]" if $subtitle =~ m/Deutschland-Premiere/;
					$flags .= "[Erstaufführung]" if $subtitle =~ m/Deutsche Erstaufführung/;
					$flags .= "[Live]" if $subtitle =~ m/live/i;
					$rate = "Tipp" if $subtitle =~ m/TIPP/;
					$origtitle =$1 if $subtitle =~ m/\((.*?)\)/;

					makeEPGevent($event_id,$epgustarttime,$duration,$epgtitle,$subtitle,$event_id,"original",
							$summary,$regie,$darsteller,$genre,$type,$country,
							$year,$audio,$format,$flags,$origtitle,$fsk,$folge,$rate,
							$import,"","");			

					$event_id++;

					$prev_ustoptime = $epgustarttime + $duration;
					$epgevent=$metainfo=$subtitle=$summary=$epgtitle=$genre=$country=$year=$duration = "";
					
					($epgevent,$epgchanneldata) = ($1,$2)
					if $epgchanneldata =~ m/^(\@E:\d\d:\d\d.*?)(\@[EQ]:\d\d:\d\d.*)$/ms;

					last if $epgchanneldata =~ m/^\@Q/;

					} # while ($epgevent ne "")
				$prev_ustoptime = "";
				last;
			} # while (1==1) ## sender parsen

			$epgchanneldata = "";
			($epgchanneldata,$epg) = ($1,$2) if $epg =~ m/^(\@P:.+?\@Q:\d\d:\d\d)(.*)$/ms;
		} # while ($epgchanneldata ne "")
	} # for ($days=0;$days


	foreach $sender (keys(%chan)) {
		if ($chan_wanted{$sender} eq "4") {
	print "sorting $sender ... \n";
			$last = 0;
			my(@temp);
			for(sort({ $a->{start} <=> $b->{start} } @events)) {
				doexit() if $please_exit;
				next if($last eq $_->{start});
				next if($_->{channel} ne $sender);
				push(@temp, $_); 
				$last = $_->{start};
			}
			$XML{$sender} = [ @temp ];
		}
	}
} # sub ProcessEpg

##################################################################################
		

sub calcUnixtime {
my $time = shift;
my $date = shift;
my $eod  = shift;
my $temputime = UnixDate("$date $time", "%s");
if ($temputime < UnixDate("$epgdate $eod", "%s")) {
	$temputime = UnixDate(DateCalc("$date $time","+ 1 tag",\$err,0), "%s");
	}			
return $temputime;
}

return 1;


