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




# Log-on to Oracle

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