Index: trunk/DataStoreServer/scripts/dsreg
===================================================================
--- trunk/DataStoreServer/scripts/dsreg	(revision 16761)
+++ trunk/DataStoreServer/scripts/dsreg	(revision 16765)
@@ -16,4 +16,5 @@
 use PS::IPP::Metadata::Stats;
 use PS::IPP::Metadata::List qw( parse_md_list );
+use File::Copy;
 
 use PS::IPP::Config qw($PS_EXIT_SUCCESS
@@ -32,6 +33,5 @@
 my $del;
 
-#my $ds_dir = './dsroot';
-my $ds_dir = '/var/www/html/ds/dsroot';
+my $ds_dir;
 my $product;
 my $fileset;
@@ -77,10 +77,21 @@
     unless $add xor $del;
 
-$err .= "Datastore not found at '$ds_dir'.\n"
-    if stat("$ds_dir/index.txt") eq undef;
+my $ipprc =  PS::IPP::Config->new(); # IPP Configuration
+my $siteConfig = $ipprc->{_siteConfig};
+if (!$ds_dir) {
+    $ds_dir = metadataLookupStr($ipprc->{_siteConfig}, 'PSTAMP_DATA_STORE_ROOT');
+    if (!$ds_dir) {
+        die("data store root not defined");
+    }
+}
+
+
+if (!stat("$ds_dir/index.txt")) {
+    $err .= "Datastore not found at '$ds_dir'.\n"
+}
 
 # bail if any of the above args were improper
 show_usage($err)
-    if defined($err);
+    if ($err);
 
 $product = shift;
@@ -91,13 +102,8 @@
 $fileset = shift;
 
+
 #
 # run
 #
-
-# allow write for the mockup
-umask 0011;
-
-my $ipprc =  PS::IPP::Config->new(); # IPP Configuration
-my $siteConfig = $ipprc->{_siteConfig};
 
 my $dbserver = metadataLookupStr($siteConfig, 'DBSERVER');
@@ -111,110 +117,59 @@
 my $dbh = DBI->connect($dsn, $dbuser, $dbpass) or die "Cannot connect to server\n";
 
-
-#
-# delete fileset
-#
+my $index_script_name = "$ds_dir/$product/$fileset/index.txt";
+
 if ($del) {
-
-    die("delete not implemented yet");
-
-    if (stat("$ds_dir/$product/$fileset/index.txt") == undef) {
-        print "Fileset '$fileset' doesn't exist under $product.\n";
-        exit(3);
-    }
-
-    # When iterating through the product index below, we rememeber the
-    # fileset and time of the newest (assumes chrono order) fileset that is
-    # not the fileset we're deleting, just in case we have to update the
-    # root index.
-    my $last_fileset;
-    my $last_time;
-
-
-    # Read the product index into a buffer, minus $fileset's entry
-
-    open(IO, "$ds_dir/$product/index.lst")
-        or die("Failed to open '$ds_dir/$product/index.lst' for reading\n");
-
-    my $buf;
-
-    while (<IO>) {
-        my ($fs, $time, my $tmp) = split(/\|/, $_, 3);
-        $fs =~ s/^\s+|\s+$//;
-       
-        # skip the one we're removing
-        next if $fs eq $fileset;
-
-        # record the last one that wasn't the one we're removing
-        $last_fileset = $fs;
-        $last_time = $time;
-
-        $buf .= $_;
-    }
-
-    close(IO);
-
-
-    # Write the new index
-
-    open(IO, ">", "$ds_dir/$product/index.lst")
-        or die("Failed to open '$ds_dir/$product/index.lst' for writing\n");
-        
-    print IO $buf;
-    close(IO);
-    
-
-    # Remove the fileset index file
-
-    unlink("$ds_dir/$product/$fileset/index.txt")
-        or die("Failed to delete '$ds_dir/$product/$fileset/index.txt'\n");
-
-    # Check the root index; if the fileset we just removed was the
-    # 'most_recent' one, then replace it.
-
-    my $doreplace = 0;
-    
-    open(IO, "$ds_dir/index.txt")
-        or die("Failed to open '$ds_dir/index.txt' for reading\n");
-
-    $buf = '';
-
-    while (<IO>) {
-        my ($prod, $oldrecent, $oldtime, $extra) = split(/\|/, $_, 4);
-        $prod =~ s/^\s+|\s+$//;
-        
-        if ($prod ne $product) {
-            $buf .= $_;
-            next;
-        }
-
-        if ($oldrecent ne $fileset) {
-            $buf .= $_;
-            next;
-        }
-        
-        $buf .= "$prod|$last_fileset|$last_time|$extra";
-        $doreplace = 1;
-    }
-
-    close(IO);
-
-    # Write the new root index if needed
-    if ($doreplace) {
-        open(IO, ">", "$ds_dir/index.txt")
-            or die("Failed to open '$ds_dir/index.txt' for writing\n");
-        
-        print IO $buf;
-        close(IO);
-    }
- 
-    print "Removed $fileset from $product.\n";
-}
-
-
-#
-# addding a new fileset
-#
-else {
+    #
+    # delete fileset
+    #
+    my $stmt = $dbh->prepare("SELECT prod_id, last_fs FROM dsProduct WHERE prod_name = \'$product\'");
+    $stmt->execute();
+    my $prod_row = $stmt->fetchrow_hashref();
+    my $prod_id = $prod_row->{prod_id};
+    my $old_last_fs = $prod_row->{last_fs};
+
+    if (!$prod_id) {
+        die("product $product not found");
+    }
+    $stmt = $dbh->prepare("SELECT fileset_id,fileset_name FROM dsFileset WHERE fileset_name = '$fileset'" .
+                        " AND prod_id = $prod_id");
+
+    $stmt->execute();
+    my $fs_row = $stmt->fetchrow_hashref();
+
+    if (!$fs_row) {
+        print "Fileset '$fileset' not found under $product.\n";
+        exit 1;
+    }
+
+    # zap the index script
+    unlink("$index_script_name");
+
+    my $fileset_id = $fs_row->{fileset_id};
+
+    $dbh->do("DELETE from dsFile where fileset_id = $fileset_id");
+    $dbh->do("DELETE from dsFileset where fileset_id = $fileset_id");
+
+    if ($old_last_fs eq $fs_row->{fileset_name}) {
+        # print STDERR "deleting most recent fileset in $product\n";
+
+        $stmt = $dbh->prepare("SELECT fileset_name from dsFileset " .
+                    "WHERE prod_id = $prod_id ORDER BY reg_time DESC LIMIT 1");
+        $stmt->execute();
+        my $new_last_fs;
+        my $new_newest = $stmt->fetchrow_hashref();
+        if ($new_newest) {
+            $new_last_fs = $new_newest->{fileset_name};
+        } else {
+            $new_last_fs = "none";
+        }
+        $dbh->do("UPDATE dsProduct SET last_fs = \'$new_last_fs\' WHERE prod_id = $prod_id");
+    }
+    exit 0;
+
+} else {
+    #
+    # adding a new fileset
+    #
     my $stmt = $dbh->prepare("SELECT prod_id FROM dsProduct WHERE prod_name = \'$product\'");
     $stmt->execute();
@@ -283,4 +238,9 @@
     }
 
+    # create the cgi script index file by making a copy of its parent's
+    if (!copy("$ds_dir/$product/index.txt", $index_script_name)) {
+        die("failed to copy index script to fileset");
+    }
+
     eval {
         $dbh->{RaiseError} = 1; # raise exception if error occurs
@@ -332,8 +292,9 @@
         print STDERR "transactionfailed, rolling back error was:\n$@\n";
         eval {$dbh->rollback();};
+        unlink($index_script_name);
         exit 1;
     }
-	
-    print "Added $fileset to $product.\n";
+
+    # print "Added $fileset to $product.\n";
 
     exit 0;
@@ -356,11 +317,16 @@
     --add        Add a new fileset with the given metadata options.
                 File information will be read per-line from STDIN, in the form:
-                    fileID type chipname
+                    fileID type [chipname]
                 These entries are read until EOF is sent.
 Options:
     
-    --dsdir      Specify the path to the Datastore (defaults to CWD).
+    --dsdir      Specify the path to the Datastore
     
- (The following metadata is optional because this is just a mock-up.)
+$str",
+        -exitval => 2
+    );
+
+    if (0) {
+     my $saveString = "(The following metadata is not used yet)
 
     --date       ISO8601 date string. (File timestamp used if not specified.)
@@ -371,9 +337,6 @@
     --etime      Exposure time.
     --filter     Filter used.
-    --airmass    Air mass.
-    
-$str",
-        -exitval => 2
-    );
+    --airmass    Air mass. ";   
+    }
 }
 
