#!/usr/bin/perl -w
use CGI;
my $cgi = new CGI;
my @leaf;
my @key;

$ticket = $cgi->param("Ticket");
$satz=int($cgi->param("Leaf"));
if ($satz == 0) { $satz = 1;}
$line=$cgi->param("Line");
$db=$cgi->param("DB");


if ($db ne "") {
 #ganz normaler Aufruf
 adddebug("DB:$db");
 dbinfoladen ($db);
# $Datei = "/cgi-bin/RC.XTS";
$Datei="/cgi-bin".$dbpath.$dbfile;
adddebug ($Datei);
 satzladen();
 $tfile = "/cgi-bin/inputtemplate.html";
 templateladen($tfile);
}
else {
 # Aufruf ohne DB-Argument erzwingt die Auswahl einer Datei
 $tfile="/cgi-bin/dbselecttemplate.html";
 templateladen($tfile);
}
replaceintemplate();
sendresponse();

#*****************************************************************

sub satzladen {
 $offset = ($satz-1)*512;
 open (DATEI,"<$Datei");
 seek (DATEI,$offset,0);
 read (DATEI,$root,5);
 read (DATEI,$level,5);
 read (DATEI,$question,128);
 read (DATEI,$comment,256);
 read (DATEI,$leafraw,64);
 read (DATEI,$picfile,4);
 $picfile=unpack("c4",$picfile);
 read (DATEI,$commandfile,4);
 $commandfile=unpack("c4",$picfile);
 read (DATEI,$stdrtf,4);
 $stdrtf=unpack("c4",$stdrtf);
 read (DATEI,$spare,48);
 close (DATEI); 

 #print"***".$leafraw."***\n";
 for ($n=0;$n<8;$n++){
  $leaf[$n] = int(substr($leafraw,$n*8,5)); 
 
  $key[$n]="  ".chr(substr($leafraw,$n*8+5,3))."  " ;
  #print "key $key[$n] leaf $leaf[$n]\n";
 }
 #if KEYTEXT ist given in comment field...
 $mycomment=$comment;
 $pos1= index(uc($mycomment),"KEYTEXT");
 adddebug ($mycomment.$pos1);
 if ($pos1 >=0 ) {
  #$mycomment=trim(substr($mycomment,$pos1+7));
  $mycomment=substr($mycomment,$pos1+7);
  $pos2 = index($mycomment,chr(34),1);
  $keytextraw=substr($mycomment,1,$pos2-1);
  $mycomment=substr($mycomment,0,$pos1).substr($mycomment,$pos2+1);
  adddebug ($comment.$keytextraw);
  @keytext=split(/\\/,$keytextraw);
  for ($n=0;$n<@keytext;$n++) {
   $key[$n]=$keytext[$n];
  }
  adddebug ("$pos1 $pos2");
  $comment=substr($comment,0,$pos1).substr($comment,$pos1+$pos2+8);
 }
 

}

sub trim {
 my $arg=shift;
 $arg=~ s/^\s+//; # ^: start at beginning; \s: space +: multiple occurrence
 $arg=~ s/\s+$//; # as above, but $: at end of text
 return $arg;
}
sub templateladen {
 #load template
 $htmltemplatefile = shift; 

 open (TEMPLATE,"<$htmltemplatefile");

  $tempall="";
  while ($template=<TEMPLATE>){
   $tempall=$tempall.$template;
  }
 close (TEMPLATE);
}


sub replaceintemplate(){
$formstart="<form action='/cgi-bin/max_ts.cgi' method='post'>".chr(13).chr(10);
$formend="</form>".chr(13).chr(10);

#key selections
for ($n=0; $n<8;$n++){
 if ($leaf[$n] != 0){
  $sel=$formstart."<input type ='hidden' name='Leaf' value='$leaf[$n]' >";
  $sel=$sel."<input type ='hidden' name='DB' value='$db'>";
  #$sel=$sel."<input type ='submit' value='$key[$n]' accesskey='$key[$n]' style='width:80px;'>";
  $sel=$sel."<input type ='submit' value='$key[$n]' accesskey='$key[$n]' >";
  #$sel=$sel."$leaf[$n]"; 
  $sel=$sel."<br>".$formend;
 $selections=$selections.$sel;
 }
}
#backbutton
$backbutt="<input type ='hidden' name='Leaf' value='$root' >";
$backbutt=$backbutt."<input type ='hidden' name='DB' value='$db'>";
$backbutt=$formstart.$backbutt."<input type = 'submit' value='Back'>".$formend; 

#restartbutton
$restartbutt="<input type ='hidden' name='Leaf' value='0' >";
$restartbutt=$restartbutt."<input type ='hidden' name='DB' value='$db'>";
$restartbutt=$formstart.$restartbutt."<input type = 'submit' value='Restart' style='width:60px;'>".$formend; 

#DBSelection
$dbselection=$formstart."<select name='DB' size='4' style='width:15em;'>".chr(13).chr(10);

open (DBASES,"<databases.txt");
while ($dbentry=<DBASES>){
 if (substr($dbentry,0,1) ne chr(39)) {
  @dbfield=split(/,/,$dbentry);
  $dbsname=$dbfield[0];
  $dbselection=$dbselection."<option>$dbsname</option>".chr(13).chr(10);
 }
}

close (DBASES);
$dbselection=$dbselection."</select><input type='submit' value='OK'></form>";

#image
adddebug (substr(uc($comment),0,7));
if (($picfile eq "1") or (substr(uc($comment),0,7) eq "PICTURE"))  {
 if ($picfile eq "1")
  { $img=$dbpicpath.$satz.".jpg"; }
 else {
  $pos2=index($comment,chr(34),8); 
  $picname=substr($comment,8,$pos2-8);
  $img = $dbpicpath.$picname ;
  $comment=substr($comment,$pos2+1);
 } 
 $image ="<img src='$img' alt='$img' border='1' width='160' height='120'>";
}
else {
 $img=$dbpicpath."noimage.jpg";
 $image = "<img src='$img' alt='no image' border='1'>";
}

#replace
$title= "MAX_TS";
#$remoteip="123.456.789.001";
$ticket="12345";
$xtsname="$db";
$rootcell = $root;
$actualcell = $satz;

$question=~s/\x0a/<br>/g;
$comment=~s/\x0a/<br>/g;
$tempall=~s/\[Title\]/"$dbdescription ($satz)"/g;
$tempall=~s/\[image\]/$image/g;
$tempall=~s/\[RemoteIP\]/$ENV{'REMOTE_ADDR'}/g;
$tempall=~s/\[ticket\]/$ticket/g;
$tempall=~s/\[xtsname\]/$xtsname/g;
$tempall=~s/\[dbfile\]/$dbfile/g;
$tempall=~s/\[rootcell\]/$rootcell/g;
$tempall=~s/\[actualcell\]/$actualcell/g;
$tempall=~s/\[Backbutton\]/$backbutt/g;
$tempall=~s/\[Restartbutton\]/$restartbutt/g;
$tempall=~s/\[Selections\]/$selections/g;
$tempall=~s/\[Description\]/$dbdescription/g;
$tempall=~s/\[DBSelection:?\]/$dbselection/g;
$tempall=~s/\[Question\]/$question/g;
$tempall=~s/\[Comment\]/$comment/g;
$tempall=~s/\[DBinfos\]/@dbifield/g;
$tempall=~s/\[Debug\]/$debug/g;

}

sub sendresponse() {
print "Content-type: text/html\n\n";
print $tempall;
}

sub dbinfoladen(){
$dbgesucht = shift;
adddebug("DBgesucht:$dbgesucht");
open (DBASES,"<databases.txt");
while ($dbentry=<DBASES>){
 if (substr($dbentry,0,1) ne chr(39)) {
  @dbtempfield=split(/,/,$dbentry);

  if ($dbtempfield[0] eq $dbgesucht) {
   @dbifield=@dbtempfield;
   $dbname=$dbifield[0];
   $dbdescription=$dbifield[1];
   $dbfile=trim($dbifield[2]);
   $dbpath=trim($dbifield[3]);
   $dbpicpath=trim($dbifield[4]);
   $dbususaltemplate =trim($dbifield[5]);
   $dbanswertemplate =trim( $dbifield[6]);
   $dblearntemplate = $dbifield[7];
   $dbinputtemplate=$dbifield[8];
   $dbchoosefromtemplate=$dbifield[9];
  
  }
 }
}
close (DBASE);
}
sub adddebug(){
 $debug = $debug.shift."*<br>";
}