Index: trunk/tools/bills/concatp2batch
===================================================================
--- trunk/tools/bills/concatp2batch	(revision 38965)
+++ trunk/tools/bills/concatp2batch	(revision 38968)
@@ -21,5 +21,5 @@
 
 GetOptions(
-    "vebose|v"          =>  \$verbose,
+    "verbose|v"          =>  \$verbose,
     "save-temps"        =>  \$save_temps,
     "test"              =>  \$test,
@@ -31,6 +31,9 @@
 my $output = shift;
 
-# TODO: make this an option
-my $stiltsJar = '/home/panstarrs/bills/jars/stilts.jar';
+my $jardir = $ENV{JARDIR};
+if (!$jardir) {
+    $jardir = '/home/panstarrs/bills/jars';
+}
+my $stiltsJar = "$jardir/stilts.jar";
 
 my $stilts="java -jar $stiltsJar";
@@ -38,20 +41,9 @@
 my $ofmt = 'fits';
 
-my $extensionList = `ftlist $input h | grep Detection | awk '{print \$2}'`;
+my $listArg = getExtensionArg($input);
 
-my ($listFile, $listName) = tempfile ("/tmp/foo.XXXX", UNLINK => !$save_temps);
+my $cmd = "$stilts tcat in='$listArg' out=$output ofmt=$ofmt";
+print STDERR "running $cmd\n";
 
-# make a list of the detection extension names
-print "$listName\n" if $save_temps;
-foreach my $n (split "\n", $extensionList) {
-    print "$n\n" if $verbose;
-    # ftlist HDU number starts with 1, stilts expects zero based index
-    $n -= 1;
-    print $listFile "$input#$n\n";
-}
-close $listFile;
-
-# my $cmd = "$stilts tcat in=\@$listName out=$output ofmt=$ofmt ocmd='tablename $extname;'";
-my $cmd = "$stilts tcat in=\@$listName out=$output ofmt=$ofmt";
 my $rc = system $cmd;
 if ($rc) {
@@ -62,2 +54,28 @@
 exit 0;
 
+sub getExtensionArg {
+    # find the zero offset extension number of each of the detection extensions and build
+    # an argument list for them
+    my $input = shift;
+
+    my $ftableOutput = `ftable -list $input`;
+    die "ftable didn't return any output for $input\n" unless $ftableOutput;
+
+    my @lines = split "\n", $ftableOutput;
+
+    my $listArg = "";
+    my $n = -1;
+    foreach my $line (@lines) {
+        # skip first line which is a list header
+        if ($n == -1) {
+            $n = 0;
+            next;
+        }
+        if ($line =~ /^Detection/) {
+            $listArg .= "$input#$n ";
+        }
+        $n++;
+    }
+
+    return $listArg;
+}
