Add $* for filename base
[conv.git] / conv.pl
diff --git a/conv.pl b/conv.pl
index 2f5a609..5904405 100755 (executable)
--- a/conv.pl
+++ b/conv.pl
@@ -10,7 +10,23 @@ my %opts = (
     j => 1
 );
 
-getopts('vnj:f:o:s:', \%opts);
+getopts('vnsj:f:o:i:', \%opts);
+
+my $dirout;
+if (defined $opts{o}) {
+    if (-d $opts{o}) {
+        # output is a directory
+        $dirout = 1;
+    } else {
+        # output is a single file, imply -i <ext> unless -i has also been specified
+        if ($opts{o} =~ /\.(\w+)$/) {
+            $opts{i} = $1 unless exists $opts{i};
+        } else {
+            print "Specified -o <file>, but '$opts{o}' has no extension.\n";
+            exit 1;
+        }
+    }
+}
 
 print "Using $opts{j} processors\n" if $opts{v};
 
@@ -22,12 +38,17 @@ sub convert {
     my $infile = "$base.$iext";
     $infile =~ s/"/\\"/g;
     $cmd =~ s!\$\<!"$infile"!g;
+    $cmd =~ s!\$\*!"$base"!g;
     my $outfile;
     if ($opts{o}) {
-        # Strip leading path from base so we can stick the file in
-        # another dir.
-        $base = basename($base);
-        $outfile = "$opts{o}/$base.$oext";
+        if ($dirout) {
+            # Strip leading path from base so we can stick the file in
+            # another dir.
+            $base = basename($base);
+            $outfile = "$opts{o}/$base.$oext";
+        } else {
+            $outfile = $opts{o};
+        }
     } else {
         $outfile = "$base.$oext";
     }
@@ -55,7 +76,7 @@ while (<CONF>) {
     s/\s+$//;
     next if /^$/;
 
-    if (/^(\w+)\s*=>\s*(\w+)\s*:\s*(.*)$/) {
+    if (/^([\w.-]+)\s*=>\s*([\w.-]+)\s*:\s*(.*)$/) {
         $rules{$2}{$1} = $3;
     } elsif (/^($var_re)\s*=\s*(.*)$/) {
         $vars{$1} = expand($2);
@@ -66,15 +87,32 @@ while (<CONF>) {
 close CONF;
 
 my @queue;
+my @files;
 
 # If we specify reading from stdin, do this.
 if (exists $opts{s}) {
     while (<STDIN>) {
         next if /^#/;
         chomp;
-        
+
+        push @files, $_;
+    }
+} else {
+    @files = @ARGV;
+}
+
+# Reversed semantics. Files are input to be converted to the specified extension
+if ($opts{i}) {
+    unless (exists $rules{$opts{i}}) {
+        print <<EOD;
+You have specified -i <ext>, but the Convfile has no rule to convert to
+extension '$opts{i}'
+EOD
+        exit 1;
+    }
+    foreach (@files) {
         unless (-e $_) {
-            print "File specified on stdin, $_, does not exist.\n";
+            print "$_ does not exist.\n";
             next;
         }
 
@@ -82,9 +120,9 @@ if (exists $opts{s}) {
             my $base = $1;
             my $iext = $2;
             print "Considering $base.$iext\n" if $opts{v};
-            if (exists $rules{$opts{s}}{$iext}) {
+            if (exists $rules{$opts{i}}{$iext}) {
                 print "Using $base.$iext\n" if $opts{v};
-                push @queue, [$base, $iext, $opts{s}];
+                push @queue, [$base, $iext, $opts{i}];
             }
         } else {
             print "I don't see an extension on $_, so I don't know what to do with it.\n";
@@ -92,31 +130,30 @@ if (exists $opts{s}) {
     }
 }
 
-# Not reading filenames from stdin.
+# Make semantics, files specified are desired output files
 else {
-    # Do something fun
-    my @tmpARGV;
-    foreach (@ARGV) {
+    # Do something fun (convert *.foo into a list of files that would match if they existed)
+    my @tmpfiles;
+    foreach (@files) {
         if (/^\*\.(\w+)$/) {
             my $extsub = $1;
-            opendir D, '.';
-            my @files = 
+            push @tmpfiles,
             map { s/\.(\w+)$/\.$extsub/; $_ } 
                 grep { /^[^.]/ }
-                readdir D;
-            closedir D;
-            push @tmpARGV, @files;
+                <*>;
         } else {
-            push @tmpARGV, $_;
+            push @tmpfiles, $_;
         }
     }
-    @ARGV = @tmpARGV;
+    @files = @tmpfiles;
     
     # I like to call this the TOWER OF POWER
-    for my $ofile (@ARGV) {
+    for my $ofile (@files) {
         my $ofound = 0;
         for my $oext (keys %rules) {
-            if ($ofile =~ /^(.*)\.$oext$/) {
+            my $oext_re = $oext;
+            $oext_re =~ s/\./\\./g;
+            if ($ofile =~ /^(.*)\.$oext_re$/) {
                 my $base = $1;
                 my $ifound = 0;
                 for my $iext (keys %{$rules{$oext}}) {
@@ -154,4 +191,4 @@ foreach my $chunk (@queue) {
 }
 wait while $procs--;
 
-# vim:set ts=4 sts=4 sw=4 expandtab:
+# vim:ts=4 sts=4 sw=4 expandtab