#! /usr/oracle/bin/oraperl

$/="\x0d";

$words;
@words;
@servers;
@encoding;



$inp_line=<>;
chop $inp_line;

@params=split(/\&/,$inp_line);

foreach (@params)
{
  push(@encoding," (ENCODING='$1') ") if /encoding=(.*)$/;
  push(@enc, " $1 ") if /encoding=(.*)$/;
  
  push(@servers, " (SERVER='$1') ") if /server=(.*)$/  && !/''/;
  push(@serv, "  $1 ,") if /server=(.*)$/ && !/''/;
  $words=$1 if /captions=(.*)$/;
}
$serv = join(' ', @serv);
$enc  = join(' ', @enc);

# translation into the upper case
$words =~ tr/a-z/A-Z/;




$encoding = 'all' if @encoding eq '';

if ($words eq '')
{
 $encoding=join(' OR ',@encoding);
 $servers=join(' OR ',@servers);

 $querystr .= " ($servers)  " if $servers ne '';
 $querystr .= " AND ($encoding) " if $encoding ne ''; 

 $wherestr  = " where $querystr " if $querystr ne '';

 $query  = "select distinct TITLE, SERVER, FILENAME, ENCODING, LENGTH, ";
 $query .= "START_FR, END_FR, START_OFF, END_OFF, FOFF ";
 $query .= "from CLIP ";
 $query .= " $wherestr  " if $wherestr ne '';

 $cap = 'the list of available clips';
}
else
{
 $cap .= $words;

 @words=split(/\+/,$words);

  foreach (@words)
  {
    $_="(text like '%$_%')";
  }

 $encoding=join(' OR ',@encoding);
 $servers=join(' OR ',@servers);
 $words=join(' AND ',@words);
 $querystr .= " AND ($words) " if $words ne '';
 $querystr .= " AND ($servers) " if $servers ne '';
 $querystr .= " AND ($encoding) " if $encoding ne '';
 $query  = "select distinct TITLE, SERVER, FILENAME, ENCODING, LENGTH, ";
 $query .= "START_FR, END_FR, START_OFF, END_OFF, FOFF,  FILE_OFFSET, FR_ST ";
 $query .= "from CLIP A,TEXT B where A.CLIPID=B.CLIPID $querystr";
}

print "Content-type: text/html\n\n ";

print "<HTML>\n";
print "<TITLE>NPAC VoD Database search results</TITLE>\n";
print "<BODY>\n";
print "<HR><h2>NPAC VoD Database query results: </H2><HR>\n ";
print "<b>You searched for: \n";
print $cap,"<BR>\n";

if ($serv eq '')
{
    $serv = 'all servers';
}
print "on the following servers:</b> $serv \n";
if ($enc eq '')
{
    $enc = 'all types';
}
print "<br> encoding type: $enc \n";

print "<ol> \n";

$ENV{'ORACLE_HOME'}='/usr/oracle';
$ENV{'ORACLE_SID'}='nodb';



$lda=&ora_login('','michal','michal');
$csr=&ora_open($lda,$query);
while(@tab=&ora_fetch($csr))
{
  ($title,$server,$fname,$enc,$len,$st_fr,$en_fr,$st_of,$en_of,$foff,$file_off,$fr_st)=
    @tab;
  $server=$1 if $server =~ /^(\S*)\s+/;
  print "<LI><P> \n";
  print "<TABLE BORDER=1 WIDTH=75%> \n";
  print "<TR><TD align=left>Title:</TD> \n";
  print "<TD align=center><A HREF=\"http://kayak.npac.syr.edu:1963/cgi-bin/vser/vclix.pl?\n";

  print "$server&$fname&$file_off&$enc&$len&$st_fr&$en_fr&$st_of&$en_of&$foff&$fr_st\n";


  print "\"<B>$title</B></A>";

  $len = int($len/30);		# length of clip in minutes
  $minute = int($len/60);	# minutes
  $second = ($len%60);		# seconds
 
  print "</TD></TR>";
  print "<TR><TD align=left><B>server</B></TD><TD align=center> $server</TD></TR>\n";
  print "<TR><TD align=left><b>file name:</b></TD><TD align=center> $fname</TD></TR>\n";
  print "<TR><TD align=left><b> encoding type:</b></TD><TD align=center> $enc,</TD></TR>\n ";
  print "<TR><TD align=left><B>length of clip:</B></TD><TD align=center> $minute min $second sec.\n </TD></TR>";
  print "</TABLE>\n";
}
print "</OL>\n";
print "<HR><ADDRESS>Please report any problem to NPAC VoD Group</ADDRESS>\n";
print "</BODY>\n";
print "</HTML>\n";
&ora_close($csr);
&ora_logoff($lda);


