#!/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";





########  Read the parameters from the stdin ######
$input=<>;
#chop $input;

### split the input line ###
@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=(.*)/;
    $capfile  = $1 if /capfile=(.*)/;
    $ann_start = $1 if /ann_start=(.*)/;
    $datatype = $1 if /datatype=(.*)/;
    $annotations = $1 if /annotations=(.*)/;
    $fnailfile = $1 if /fnailfile=(.*)/;
    $fnailurl = $1 if /fnailurl=(.*)/;
}


###### start writing the ouput HTMLdoc
print ("Content-type: TEXT/HTML\n\n");
print <<"EndOfHTML"; 
<HTML>
<head>
<script>
document.bgColor=top.menu.document.bgColor
</script>
<TITLE>NPAC Video New DataBase: Clip annotated</TITLE>
</head>
<BODY > <B>
<H1> Results of the annotate clip  operation:</H1>
<HR>
<FONT Size="4"> Input Data <BR><BR>
<FONT Size="3"> 
Caption file : $capfile <BR>
Fingernail file : $fnailfile <BR>
Fingernail URL : $fnailurl <BR>
Annotation Start : $ann_start<BR>
Annotaion data type : $datatype<BR>
Annotation text : $annotations<BR>
</B> <HR> <B>
<FONT Size="4"> Results: <BR><BR>
<FONT Size="3">
EndOfHTML

#
# File existence tests
#


$nocaption = 0;
unless(-r $capfile) {
  print "Cannot read caption file - assuming default. <BR>";
  $nocaption = 1;
}
$nofnail = 0;
unless(-r $fnailfile ) {
  print "Cannot find fingernail file - assuming default. <BR>";
  $nofnail = 1;
}


#
# Oracle Login
#

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


#
# Load caption table
#
if($nocaption!=1) {
  print "CAPTIONS : <BR>";
  $cap_count=0;
  $succ_count=0;
  open (CAP, $capfile);
  $begin = 0;
  while (<CAP>) {
    $c_line = $_;
    if ($begin != 1 && $c_line =~ /Caption Script follows(.*)/i) { $begin=1; }
    elsif ($begin==1) {
      $c_line =~ /\s*(\d+)\s*(.*)$/;
      $tstamp      = $1;
      $captiontext = $2;
      if($tstamp ne '') {
        $frame = $tstamp;
#       print "$captiontext<BR>";
        $captiontext =~ s/&/&&/g;
        $captiontext =~ s/\'/&1/g;
        $captiontext =~ s/%/&2/g;
        $ret=&ora_do
($lda, " insert into CAPTIONS values ($clipid, $frame, '$captiontext') ");
#       if($ret =~ /1/) { $succ_count++; print "<BR>";}
        if($ret =~ /1/) { $succ_count++; }
        else { print "[ ERROR : $ora_errstr]<BR>"; }
        $cap_count++;
      }
    }
  }
  close(<CAP>);
  print "Total caption items: $cap_count, successfuly inserted: $succ_count<BR>";
}

#
# load annotation table
# take direct annotations and load them into annotations
#
unless($datatype =~ /NO ANNOTATION/) {
  if($datatype =~ /TEXT/) {
    $annotext = &HexString($annotations);
#   print "<p> $clipid, $ann_start, $datatype, $annotext, $annotations <br>\n";
#   $ret = &ora_do(
#          $lda,"insert into ANNOTATIONS values 
#                ($clipid, $ann_start,0,'$datatype','$annotext')");
    $ret = &ora_do(
           $lda,"insert into ANNOTATIONS values 
                 ($clipid, $ann_start,0,'$datatype','$annotations')");
    print "ANNOTATIONS : "; 
    if($ret>=1){
      print "$ret row inserted<BR>";
    } else {
      print "No rows are inserted.<BR>$ora_errstr<BR><BR>";
    }
  } else {
    print("This version only supports TEXT annotations.<BR>");
  }
}

#
# Logout from Oracle
#

&ora_commit($lda);
&ora_logoff($lda);

print <<"EndOfHTML";
<HR>
<A HREF="$WSA/cgi-bin/annclipform">ANNOTATE ANOTHER </A>
</B></BODY></HTML>
EndOfHTML

sub TimeToFrame {
  local($t,$i,$ub,$lb,$done);
  $t = $_[0];
  $done = 0;
  $ub = $table_count-1;
  $lb = 0;
  if($t >= $timestamp[$ub]) { $i = $ub; $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; }
  }
  $frame[$i];
}

sub HexString {
  local(@testing,$srcstr,$retstr);
  $srcstr = $_[0];
  @testing = ();
  $retstr="";
  @testing = unpack("c*",$srcstr);
  foreach(@testing) {
    $retstr.= &Hex2($_);
  }
  $retstr;
}

sub Hex2 {
  local($n,$c1,$c2,@hstr);
  $n=$_[0];
  @hstr=("0","1","2","3","4","5","6","7","8","9","A","B","C","D","E","F");
  $c1 = int($n/16);
  $c2 = $n % 16;
  $hstr[$c1].$hstr[$c2];
}
