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

    $sname    = $1 if /sname=(.*)/;
    $ipaddress= $1 if /ipaddress=(.*)/;
    $platform = $1 if /platform=(.*)/;

}
if ( $sname eq "" || $ipaddress eq "" ){
print ("Content-type: TEXT/HTML\n\n");
print <<"EndOfHTML";
<HTML>
<TITLE>Video News Database - Add Server </TITLE>
<BODY bgcolor="tan">
<H3>Error: you have not entered valid data! </H3>
<A HREF="$WSA/cgi-bin/addserverform">
Another chance...</A>
</BODY>
</HTML>
EndOfHTML
die;
}

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

print <<"EndOfHTML";
<HTML>
<TITLE>Video News Database - Add Server </TITLE>
<BODY bgcolor="tan">
<H1> Add a Server </H1>
<HR>
<FONT Size="4"> Input data <BR><BR>
<FONT Size="3">
Server name : $sname <BR>
IP address : $ipaddress<BR>
Platform : $platform <BR><BR>
<HR>
<FONT Size="4"> Results of the Add Server operation: <BR><BR>
<FONT Size="3">
EndOfHTML


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

# Getting a SERVERID

$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 SERVERKEY where SKEY=1");
  ($keynum) = &ora_fetch($cursor);
  &ora_close($cursor);
  #if got a key
  unless($keynum == undef) {
    # Delete the key
    $ret = &ora_do($lda, "delete from SERVERKEY where SKEY=1");
    # If the key is deleted successfully
    if($ret =~ /1/) {
      $keynum++;   # New server ID number
      $ret = &ora_do($lda, "insert into SERVERKEY 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 THE SERVERID<BR>";
      }
    }
  }
  if($try > $maxtry) {
     &ora_logoff($lda);
     die "TRIAL ERROR : COULD NOT ASSIGN THE SERVERID<BR>";
  }
  $try++;
}
$serverid = $keynum;

$ret = &ora_do ($lda, " insert into servertable (serverid, sname, ipaddress, platform) values ($serverid , '$sname' , '$ipaddress' , '$platform' )" );
if ($ret==1) {
  print "$ret row is inserted.<BR>";
} else {
  print "Error occured.<BR>$ora_errstr<BR>";
}
&ora_commit($lda);
&ora_logoff ($lda);

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


