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

    $aname    = $1 if /aname=(.*)/;
    $adesc = $1 if /adesc=(.*)/;

}

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

print <<"EndOfHTML";
<body bgcolor="coral">
<H1> Results of the add archive operation </H1>
<HR>
<FONT Size="4"> Input data <BR><BR>
<FONT Size="3">
Archive name : $aname <BR>
Archive description : $adesc<BR>
<HR>
<FONT Size="4"> Results: <BR><BR>
<FONT Size="3">
EndOfHTML

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

# Getting an ARCHIVEID

$success = 0;
$try = 1;                # Trial number of getting a key
$maxtry = 10;            # Maximum trial number
while($success != 1) {
  # Try to get a keynum
  $keynum = undef;
  $cursor = &ora_open($lda, "select KEYNUM from ARCHIVEKEY where AKEY=1");
  ($keynum) = &ora_fetch($cursor);
  &ora_close($cursor);
  #if got a key

  unless($keynum == undef) {
    print "Got ID: $keynum <BR>";
    # Delete the key
    $ret = &ora_do($lda, "delete from ARCHIVEKEY where AKEY=1");
    # If the key is deleted successfully

    if($ret =~ /1/) {
      $keynum++;   # New server ID number
      $ret = &ora_do($lda, "insert into ARCHIVEKEY values (1,$keynum)");
      # If the key is inserted successfully...

      if($ret =~ /1/) {
        &ora_commit($lda);  # Done
        $success = 1;
      } else {
        &ora_logoff($lda);
        die "DATABASE ERROR : COULD NOT ASSIGN ARCHIVEID<BR>";
      }

    }
  }

  if($try > $maxtry) {
     &ora_logoff($lda);
     print "FATAL ERROR : COULD NOT ASSIGN ARCHIVEID<BR>";
     die ;
  }

  $try++;
}

$archiveid = $keynum;
print "New archive ID: ", $archiveid, "<BR>";

$ret = &ora_do ($lda, " insert into archivetable (archiveid, aname, adesc) values ($archiveid , '$aname' , '$adesc' )" );
if ($ret==1) {
  print "$ret row is inserted.<BR>";
} else {
  print "ARCHIVETABLE insert: Error occured.<BR>$ora_errstr<BR>";
}
&ora_commit($lda);
&ora_logoff ($lda);

print <<"EndOfHTML";
<HR>
<A HREF="$WSA/cgi-bin/addcollform">ADD ANOTHER</A>
</BODY>
</HTML>
EndOfHTML
