Index: /tags/ipp-20100823/DataStoreServer/scripts/dsreg
===================================================================
--- /tags/ipp-20100823/DataStoreServer/scripts/dsreg	(revision 29478)
+++ /tags/ipp-20100823/DataStoreServer/scripts/dsreg	(revision 29479)
@@ -42,4 +42,5 @@
 
 my $dbname;         # Database name
+my $dbserver;       # Database host
 my $dsroot;         # root directory of the data store
 
@@ -82,8 +83,14 @@
         'ps7=s'         =>      \$ps7,
         'dbname=s'      =>      \$dbname,
+        'dbserver=s'    =>      \$dbserver,
+        'ds_dbname=s'   =>      \$dbname,       # some scripts use these names
+        'ds_dbserver=s' =>      \$dbserver,
         'dsroot=s'      =>      \$dsroot,
         'verbose'       =>      \$verbose,
 ) or pod2usage(2);
 
+pod2usage( -msg => "Unknown option: @ARGV", -exitval => 2 ) if @ARGV;
+
+# collect all command line argument errors and only fail once below.
 my $err = "";
 
@@ -121,8 +128,7 @@
     $dsroot = metadataLookupStr($ipprc->{_siteConfig}, 'DATA_STORE_ROOT');
     if (!$dsroot) {
-        die("Data Store root directory not set");
-    }
-}
-
+        &my_die("Data Store root directory not set", $PS_EXIT_CONFIG_ERROR);
+    }
+}
 
 if (!stat("$dsroot/index.txt")) {
@@ -131,12 +137,11 @@
 
 # bail if any errors above
-show_usage($err)
-    if ($err);
-
-show_usage("Invalid product '$product'.")
+show_usage($err) if ($err);
+
+show_usage("Invalid data store product '$product'.")
     unless defined stat("$dsroot/$product/index.txt");
 
 $dbname      = metadataLookupStr($siteConfig, 'DS_DBNAME') unless defined $dbname;
-my $dbserver = metadataLookupStr($siteConfig, 'DS_DBSERVER');
+$dbserver    = metadataLookupStr($siteConfig, 'DS_DBSERVER') unless defined $dbserver;
 my $dbuser   = metadataLookupStr($siteConfig, 'DS_DBUSER');
 my $dbpass   = metadataLookupStr($siteConfig, 'DS_DBPASSWORD');
@@ -148,5 +153,5 @@
 
 my $dbh = DBI->connect_cached($dsn, $dbuser, $dbpass, \%conn_attrs)
-        or die "Cannot connect to DB server\n";
+        or &my_die("Cannot connect to DB server\n", $PS_EXIT_SYS_ERROR);
 
 print STDERR "dbh: $dbh\n" if $verbose;
@@ -161,5 +166,9 @@
 
     my $stmt = $dbh->prepare("SELECT prod_id, last_fs FROM dsProduct WHERE prod_name = ?");
-    $stmt->execute($product);
+    my $execute_status = $stmt->execute($product);
+    if (!$execute_status) {
+        print STDERR "failed to execute product lookup\n";
+        exit 2;
+    }
 
     my $prod_row = $stmt->fetchrow_hashref();
@@ -228,5 +237,5 @@
         my $rc;
         if (($rc = system "rm -r $fileset_dir")) {
-            die("failed to remove $fileset_dir error code: $rc");
+            &my_die("failed to remove $fileset_dir error code: $rc", $PS_EXIT_UNKNOWN_ERROR);
         }
     } else  {
@@ -252,5 +261,5 @@
 
     if (!$prod_id) {
-        die("product $product not found");
+        &my_die("product $product not found", $PS_EXIT_UNKNOWN_ERROR);
     }
     my $count = $dbh->do("SELECT fileset_id FROM dsFileset WHERE fileset_name = ?" .
@@ -268,8 +277,8 @@
     if (! -e $fileset_dir) {
         if (!mkdir $fileset_dir) {
-            die("failed trying to create fileset directory $fileset_dir");
+            &my_die("failed trying to create fileset directory $fileset_dir", $PS_EXIT_SYS_ERROR);
         }
 	if (! -d $fileset_dir ) {
-            die("cannot access just created fileset directory $fileset_dir");
+            &my_die("cannot access just created fileset directory $fileset_dir", $PS_EXIT_SYS_ERROR);
 	}
     }
@@ -281,5 +290,5 @@
             $in = *STDIN;
         } else {
-            open $in, "<$filelist" or die  "cannot open filelist file $filelist\n";
+            open $in, "<$filelist" or &my_die("cannot open filelist file $filelist\n", $PS_EXIT_UNKNOWN_ERROR);
         }
 
@@ -296,5 +305,5 @@
             my @fields = split /\|/;
             if (@fields < 4) {
-                die "malformed input line at $filelist line $lineno: $_\n";
+                &my_die("malformed input line at $filelist line $lineno: $_\n", $PS_EXIT_PROG_ERROR);
             }
             $filename = shift @fields;
@@ -320,5 +329,5 @@
         }
 
-        die("empty filelist\n") if (@files == 0);
+        &my_die("empty filelist\n", $PS_EXIT_PROG_ERROR) if (@files == 0);
 
         # Prepare the fileset directory
@@ -350,5 +359,5 @@
                     } else {
                         if (!copy($src, $dest)) {
-                            die("copy($src, $dest) failed");
+                            &my_die("copy($src, $dest) failed", $PS_EXIT_UNKNOWN_ERROR);
                         }
                     }
@@ -359,5 +368,5 @@
 
                 if (!$no_cleanup && system "rm -r $fileset_dir") {
-                    die("failed to remove $fileset_dir");
+                    &my_die("failed to remove $fileset_dir", $PS_EXIT_UNKNOWN_ERROR);
                 }
                 exit $PS_EXIT_UNKNOWN_ERROR;
@@ -370,10 +379,10 @@
             my $path = "$fileset_dir/$filename";
             if (! -e $path ) {
-                die "file $path not found";
+                &my_die("file $path not found", , $PS_EXIT_UNKNOWN_ERROR);
             }
             # get the size of the file
             my @finfo = stat($path);
             unless (@finfo) {
-                die ("can't stat $path");
+                &my_die ("can't stat $path", $PS_EXIT_UNKNOWN_ERROR);
             }
             # if size was supplied make sure that it matches the actual
@@ -382,6 +391,6 @@
                 my $current_size = $finfo[7];
                 if ($file->{bytes} != $current_size) {
-                    die "size on disk: $current_size does not match supplied"
-                    . " size: $file->{bytes} for $path";
+                    &my_die("size on disk: $current_size does not match supplied"
+                    . " size: $file->{bytes} for $path", $PS_EXIT_PROG_ERROR);
                 }
             } else {
@@ -391,5 +400,5 @@
                 # Get MD5 sum
                 $file->{md5sum} = file_md5_hex($path);
-                die("failed to compute valid md5sum for $path") if !$file->{md5sum};
+                &my_die("failed to compute valid md5sum for $path", $PS_EXIT_UNKNOWN_ERROR) if !$file->{md5sum};
             }
         }
@@ -401,5 +410,5 @@
         $dbh->disconnect();
         $dbh = DBI->connect_cached($dsn, $dbuser, $dbpass, \%conn_attrs)
-            or die "Cannot connect to server\n";
+            or &my_die("Cannot connect to server\n", $PS_EXIT_SYS_ERROR);
         print STDERR "ping failed new dbh: $dbh\n" if $verbose;
     }
@@ -415,5 +424,5 @@
                             $ps0, $ps1, $ps2, $ps3, $ps4, $ps5, $ps6, $ps7));
 
-        die("failed to insert $fileset") if (!defined($count) or ($count == 0E0));
+        &my_die("failed to insert $fileset", $PS_EXIT_UNKNOWN_ERROR) if (!defined($count) or ($count == 0E0));
 
         # I don't use last_insert_id() because I've seen some problems with it and
@@ -423,7 +432,7 @@
         my $nfs = @fsids;
         if (!$nfs) {
-            die("fileset '$fileset' missing even though we just added it");
+            &my_die("fileset '$fileset' missing even though we just added it", $PS_EXIT_UNKNOWN_ERROR);
         } elsif ($nfs > 1) {
-            die("Fileset '$fileset' already exists under $product.");
+            &my_die("Fileset '$fileset' already exists under $product.", $PS_EXIT_PROG_ERROR);
         }
         my $fileset_id = $fsids[0];
@@ -443,5 +452,5 @@
 
             if ($count == 0E0) {
-                die("failed to insert $filename into $fileset");
+                &my_die("failed to insert $filename into $fileset", $PS_EXIT_UNKNOWN_ERROR);
             }
         }
@@ -450,5 +459,5 @@
                                 . " WHERE prod_id = $prod_id");
         if ($count == 0E0) {
-            die("failed to update dsProduct $prod_id");
+            &my_die("failed to update dsProduct $prod_id", $PS_EXIT_UNKNOWN_ERROR);
         }
 
@@ -457,5 +466,5 @@
         # create the cgi script index.txt by making a copy of its parent's
         if (!copy("$dsroot/$product/index.txt", $index_script_name)) {
-            die("failed to copy index script to file set");
+            &my_die("failed to copy index script to file set", $PS_EXIT_UNKNOWN_ERROR);
         }
     };
@@ -531,2 +540,9 @@
     }
 }
+
+sub my_die {
+    my $msg = shift;
+    my $fault = shift;
+    carp $msg;
+    exit $fault;
+}
