#! /usr/oracle/bin/oraperl


########  Read the parameters from the stdin ######

$inp_line=<>;
chop $inp_line;


### split the input line ###
@param = split(/\&/, $inp_line );



foreach (@param)
{
    ### convert plus to space ###
    $_ =~ s/\+/ /g;
    ### convert %XX from hex numbers to alphanumeric ###
    $_ =~ s/%(..)/pack("c", hex($1))/ge;

    $action   = $1 if /action=(.*)/;
    $clipid   = $1 if /clipid=(.*)/;
    $title    = $1 if /title=(.*)/;
    $server   = $1 if /server=(.*)/;
    $filename = $1 if /filename=(.*)/;
    $length   = $1 if /length=(.*)/;
    $encoding = $1 if /encoding=(.*)$/;
    $url      = $1 if /url=(.*)/;    
}


######### distinguish which action ########

### for comparing strings use regular expressions ###

if ($action =~ /Update/)   
{
    $query  = "update clip set title='$title' , ";
    $query .= " filename='$filename' , ";
    $query .= " server='$server' , ";
    $query .= " encoding='$encoding' , ";
    $query .= " length='$length' , ";
    $query .= " foff='$url'  ";
    
    $query .= " where clipid=$clipid ";
}
else
{
    $query = " delete from clip where clipid=$clipid ";

}



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

print "<HTML>\n";
print "<TITLE> Database Update / Delete </TITLE>\n";
print "<BODY>\n";
print "<HR><h2>NPAC VoD Database query results: </H2><HR> \n";
print "You submitted the following query to the database: <P>\n";

print "$query <BR><P>\n";
print ("action=$action <BR>\n");


##### set Oracle's env variables ######
$ENV{'ORACLE_HOME'}='/usr/oracle';
$ENV{'ORACLE_SID'}='nodb';

##### logon Oracle and perform the query #####

$lda = &ora_login('','michal','michal');

if (!($ret =&ora_do($lda, $query))) 
  { 
      print ("$ora_errstr<BR>\n");
      print "<B>Modification failed!!!</B><BR>\n";
  }
else 
  {
      #if ($ret =~ /OK/) {$ret = 0}
      print ("rows updated: $ret<BR> \n");
      print ("$ora_errstr <BR>\n");
      print "<B>Modification successful</B>. <BR>\n";
      &ora_commit($lda);
  }

##### additional command for delete #####
if ($action =~ /Delete/)
{
    &ora_do($lda, " delete from text where clipid=$clipid ");
}

##### logoff #####

&ora_logoff($lda);




print (" </BODY></HTML>");


