#!/oracle/app/oracle/product/7.3.3/bin/oraperl

##### set Oracle's variables #####
$ENV{'ORACLE_HOME'}='/oracle/app/oracle/product/7.3.3';
$ENV{'ORACLE_SID'}='rabbit';
$orauser="vod";
$orapass="vod";

#### set web server address ####
$WSA = "http://clarissa.npac.syr.edu/vod";
$PROJDIR="/project/K136/VOD";





($clipid,$server,$fname,$enc,$len,$frame) =  split(/\&/,$ENV{'QUERY_STRING'});


#$version = "old";
$version = "new";


$input = <>;
#chop $input;

@param = split(/\&/, $input );

foreach (@param)
{
    ##### for URL decoding ######
    ### convert plus to space ###
    $_ =~ s/\+/ /g;
    ### convert %XX from hex numbers to alphanumeric ###
    $_ =~ s/%(..)/pack("c", hex($1))/ge;
    $clipid = $1 if /clipid=(.*)/;
    $serverid = $1 if /serverid=(.*)/;
    $len = $1 if /len=(.*)/;
    $filsiz = $1 if /filsiz=(.*)/;
    $enc = $1 if /enc=(.*)/;
    $frame = $1 if /frame=(.*)/;
}

$url  = "$WSA";
$port = "6578";
$path = "NO-MEANING.frs";

$frspath .= $path;

#
# Getting a FRS variable of the clip
#

$lda = &ora_login('', $orauser, $orapass);
$ora_long=32760;
$csr = &ora_open($lda," select FRSFILE, ACCESSLEVEL, BITRATE, FRAMERATE, DOWNLD
                        from CLIPTABLE where CLIPID=$clipid");
($frsvariable, $access, $bitrate, $framerate, $downld) = &ora_fetch($csr);
$frsvariable =~ s/NPAC\s+/NVS/g;

&ora_close($csr);
$csr = &ora_open($lda," select IPADDRESS from SERVERTABLE 
                 where SERVERID=$serverid");
($server) = &ora_fetch($csr);
&ora_close($csr);
$csr = &ora_open($lda," select FILENAME from CLIPSERVER 
                 where SERVERID=$serverid and CLIPID=$clipid");
($fname) = &ora_fetch($csr);
&ora_close($csr);
&ora_logoff($lda);

#
# Converting the frame number to file offset
#

&MakeFRSTable($frsvariable);
$file_off = &FrameToOffset($frame);
 
#
# Set up metafile variables
#
$fz = $filsiz;
$ln = $len;
$framm = $frame;
if( $fz eq "" ) {$fz = "2048000"}
if( $ln eq "" ) {$ln = "5000"}
if( $bitrate eq "" ) {$bitrate = "1500"}
if( $framerate eq "" ) {$framerate = "30"}
if( $access eq "" ) {$access = "ALL"}
if( $downld eq "" ) {$downld = "NO"}

# Metafile output now deferred until after CC script printed!

print "Content-type: multipart/x-mixed-replace;boundary=---EndOfHTMLdocument---\n\n";
print "---EndOfHTMLdocument---\n";

print "Content-type: TEXT/HTML\n\n";

$lda = &ora_login('', $orauser, $orapass);

# Getting title of the clip
$csr = &ora_open($lda,"select TITLE from CLIPTABLE where CLIPID=$clipid ");
($title) = &ora_fetch($csr);
&ora_close($csr);

print "<h2>Caption script associated with clip \"$title\"</h2><P>\n";
print "<h3> During playback, click on the \>\>\> symbol to jump to 
            a corresponding part of the video clip.
            Initial playback offset: $frame <BR></h3>";
print "<HR>";
print "<TABLE BORDER=0 WIDTH=99%>";

$csr= &ora_open
($lda,"select FRAME, TEXT from CAPTIONS where CLIPID=$clipid order by FRAME");
while ( ($frame, $text) = &ora_fetch( $csr )) {
  $text =~ s/&2/%/g;
  $text =~ s/&1/\'/g;
  $text =~ s/&&/&/g;
  $text =~ tr/A-Z/a-z/;
  $text =~ tr/\>//d;

  print "<TR><TD>$frame</TD><TD><A HREF=\"$WSA/cgi-bin/slider.ofs?";
  $file_offset = &FrameToOffset($frame);
  if ( $version eq "old" ) { print "$file_offset&type.ofs\">"; }
  else {print "$frame&$ln&type.ofs\">"; }
  print "\>\>\></A>";
  print "<TD> $text</TD></TR>\n";
}
print "</TABLE><BR>";
&ora_close($csr);
&ora_logoff($lda);

print <<"EndOfHTML";
<HR>
<A HREF="$WSA/cgi-bin/vnsquery">Play Another Clip </A> / 
<A HREF="$WSA/cgi-bin/vnsqueryform">Specify Another Search</A>
<HR>
<ADDRESS>Please report problems to NPAC VoD Group</ADDRESS>
<ADDRESS> Marek Podgorny <A Href="mailto:marek@npac.syr.edu">
          &lt;marek@npac.syr.edu&gt;</A><BR>
          Last Modified on Fri Aug 16 12:42:56 EDT 1996
</address>
</B></BODY></HTML>
EndOfHTML
print "---EndOfHTMLdocument---\n";
print "Content-type: vod/nvs\n\n";

if ( $version eq "old") {
print "$server\n";              # server name
print "$fname\n";               # filename
print "$file_off\n";            # offset
print "$url $port $path\n";	# url with the file offsets
print "$len\n";			# length of the clip
print "$frsvariable\n";
} else {
print "NVSID\n";		# file id string
print "ServerIP=$server\n";             # server IP
print "Filename=$fname\n";              # filename
print "Offset=$framm\n";		# playback offset in frames
print "Filesize=$fz\n";             	# file size in bytes
print "Length=$ln\n";			# length of the clip in frames
print "Codec=$enc\n";			# encoding
print "Bitrate=$bitrate\n";		# bitrate
print "Framerate=$framerate\n";		# framerate
print "Access=$access\n";		# access level
print "Download=$downld\n";		# download
print "$frsvariable\n";		# FRS table
}
print "---EndOfHTMLdocument---\n";
#
#  Subroutines for FRS table
#

sub MakeFRSTable {
  @timestamp=();
  @framenum=();
  @FRS=split('\n',$_[0]);
  $table_count=0;
  foreach(@FRS) {
    $a_line = $_;
    unless($a_line =~ /.*NPAC.*/) {
      $a_line =~ /(\d+)\s+(\d+)/;
      $timestamp[$table_count] = $1;
      $framenum[$table_count] = $2;
      $table_count++;
    }
  }
}

sub FrameToOffset {
  local($t,$i,$ub,$lb,$done);
  $t = $_[0];
  $done = 0;
  $ub = $table_count-1;
  $lb = 0;
  if($t >= $timestamp[$ub]) { $i = $ub; $done = 1; }
  if($t < 0) { $i = 0; $done = 1; }
  while($done!=1) {
    $i= int(($ub + $lb) / 2);
    if($ub==$lb) { $done = 1; }
    elsif (($timestamp[$i] <= $t)  && ($t < $timestamp[$i+1])) { $done = 1; }
    elsif ($t < $timestamp[$i]) { $ub = $i; }
    else { $lb = $i; }
  }
  $framenum[$i];
}
