From a60ee40720cb0bba027099a0f88859189644c1e7 Mon Sep 17 00:00:00 2001
From: Peter Seiderer <seiderer@localhost.(none)>
Date: Sun, 29 Jan 2012 12:14:52 +0100
Subject: [PATCH 04/11] Add cmdl option for gallery data file name to use.

---
 jigl.pl |  110 +++++++++++++++++++++++++++++++++-----------------------------
 1 files changed, 59 insertions(+), 51 deletions(-)

diff --git a/jigl.pl b/jigl.pl
index ba1dd05..38ca2ad 100755
--- a/jigl.pl
+++ b/jigl.pl
@@ -43,7 +43,6 @@ my $fileTypeStr = "$jpgTypeStr $gifTypeStr $pngTypeStr";
 my $jiglRCDir   = "$ENV{HOME}/.jigl";        # dir for global themes/configs
 my $jiglRCFile  = $jiglRCDir . "/jigl.opts"; # name of users global config file
 my $gblThemeDir = $jiglRCDir . "/themes";    # global name of dir for themes
-my $galDatFile  = "gallery.dat";       # name of gallery data file
 my $themeDir    = "theme";             # dir for optional theme files
 my $thumbDir    = "thumbs";            # thumbnail directory
 my $thumbPrefix = "$thumbDir/thumb_";  # thumbnail prefix
@@ -103,8 +102,11 @@ if ($optsCmd{r}) {
 }
 
 if ($optsCmd{cg}) {
-    print "Generating $galDatFile file\n";
-    genGalleryDat(\@dirs);
+    my %optsGalTmp = ();
+    my %opts = mergeOpts(\%optsSys,\%optsRC,\%optsGalTmp,\%optsCmd);
+    %opts = &checkOpts(\%opts, \%optsSys);
+    print "Generating $opts{gd} file\n";
+    genGalleryDat(\%opts, \@dirs);
     print "\n$progName has finished processing\n";
     exit 0;
 }
@@ -123,7 +125,7 @@ foreach $dir (@dirs) {
     for (my $i=0;$i<30;$i++) {print "-"};
     print "\nProcessing directory \'$dir\'\n\n";
 
-    # get any options from this dirs gallery.dat file
+    # get any options from this dirs gallery data file
     my %optsGal = &getGalOpts;
     # check the validity of the options (i.e. bounds check)
     %optsGal = &checkOpts(\%optsGal, \%optsSys);
@@ -445,7 +447,8 @@ sub recursiveDirList {
 # the key values MUST be the same as the short option name!!
 #
 sub setSystemOpts {
-    return ("cg"=>"0",   # create-gallerydat
+    return ("gd"=>"gallery.dat", # gallery-data-file
+            "cg"=>"0",   # create-gallerydat
             "uec"=>"0",  # use-exif-comment
             "uet"=>"0",  # use-exif-thumb
             "rs"=>"a",   # replace-spaces (default ask)
@@ -532,13 +535,13 @@ sub getRCOpts {
 sub getGalOpts {
 
     # check to see if the gallery.dat file exists.
-    if (-e $galDatFile) {
-        print "Found \'$galDatFile\' - Parsing options.\n";
+    if (-e $opts{gd}) {
+        print "Found \'$opts{gd}\' - Parsing options.\n";
     } else {
-        print "No \'$galDatFile\' resource file found.\n";
+        print "No \'$opts{gd}\' resource file found.\n";
         return ();
     }
-    open DATFILE, "<$galDatFile" or die "Cannot open \'$galDatFile\'\n";
+    open DATFILE, "<$opts{gd}" or die "Cannot open \'$opts{gd}\'\n";
 
     # store the argv array temporarily
     my @tmpARGV = @ARGV;
@@ -583,7 +586,8 @@ sub getCmdOpts {
     my (%opts) = %{$_[0]};
 
     # get the options and store them in the optsCmd hash.
-    die unless GetOptions(\%opts, 'cg|create-gallerydat',
+    die unless GetOptions(\%opts, 'gd|gallery-data-file=s',
+               'cg|create-gallerydat',
                'uec|use-exif-comment!',
                'uet|use-exif-thumb!',
                'rs|replace-spaces=s',
@@ -794,13 +798,14 @@ sub mergeOpts {
 }
 
 sub genGalleryDat {
-    my (@dirs) = @{$_[0]};
+    my (%opts) = %{$_[0]};
+    my (@dirs) = @{$_[1]};
     my $aInfo = &newAlbumInfo; # album info hash
     my $picCnt = 0;          # count of valid pictures
     my $skipCnt = 0;         # count of skipped pictures
     my $filesWithSpaces = 0; # number of files with spaces
 
-    print "Creating a new $galDatFile.\n";
+    print "Creating a new $opts{gd}.\n";
 
     foreach $dir (@dirs) {
         # skip any directory that is not valid
@@ -845,7 +850,7 @@ sub genGalleryDat {
         print "$picCnt of " . ($picCnt + $skipCnt) ." image files found in the directory were valid.\n";
     }
 
-    # create a new gallery.dat file from the files hash and return
+    # create a new gallery data file from the files hash and return
     &createNewGalDat(\%opts, $aInfo);
 }
 
@@ -859,19 +864,19 @@ sub genGalleryDat {
 #
 # -cg option not set
 # ------------------
-# gallery.dat file exists:
+# gallery data file exists:
 # It will parse it and return a list of hashes containing the image file
-# names and descriptions. The gallery.dat file will NOT be written to.
+# names and descriptions. The gallery data file will NOT be written to.
 #
-# gallery.dat file does not exists:
+# gallery data file does not exists:
 # It will simply return the list of hashes with the file names filled in
-# and the descriptions empty. NO gallery.dat file will be created!
+# and the descriptions empty. NO gallery data file will be created!
 #
 # -cg option set
 # --------------
-# gallery.dat file exists OR gallery.dat file does not exist:
-# It will create a NEW gallery.dat file and return an empty array.
-# That means if the gallery.dat file exists, any options, file descriptions
+# gallery data file exists OR gallery data file does not exist:
+# It will create a NEW gallery data file and return an empty array.
+# That means if the gallery data file exists, any options, file descriptions
 # or file ordering will be LOST!
 #
 # --------------------------
@@ -895,13 +900,13 @@ sub genAlbumInfo {
 
     print "Retreiving file info from directory \'$dir\'\n" if $opts{d};
 
-    # if we have a gallery.dat file and we're not creating a new
-    # gallery.dat file.
-    if (-e $galDatFile && !$opts{cg}) {
-        # read the gallery.dat file and update the albumInfo hash
-        open GALFILE, "<$galDatFile" or die "Cannot open \'$galDatFile\'\n";
+    # if we have a gallery data file and we're not creating a new
+    # gallery data file.
+    if (-e $opts{gd} && !$opts{cg}) {
+        # read the gallery data file and update the albumInfo hash
+        open GALFILE, "<$opts{gd}" or die "Cannot open \'$opts{gd}\'\n";
 
-        print "Found \'$galDatFile\' - Retreiving file list.\n";
+        print "Found \'$opts{gd}\' - Retreiving file list.\n";
 
         while (<GALFILE>) {
             if ($_ =~ /^#/) {
@@ -927,7 +932,7 @@ sub genAlbumInfo {
                 } elsif ($tag =~ /index-footer/i) {
                     $aInfo->{footer} = $val;
                 } else {
-                    print "Warning: Invalid option found in $galDatFile. <$tag>\n";
+                    print "Warning: Invalid option found in $opts{gd}. <$tag>\n";
                 }
             } else {
                 # we have a filename and description line
@@ -969,8 +974,8 @@ sub genAlbumInfo {
                 }
             }
         }
-        close GALFILE; # close the gallery.dat file
-        print "$picCnt of " . ($picCnt + $skipCnt) . " image files found in $galDatFile were valid.\n";
+        close GALFILE; # close the gallery data file
+        print "$picCnt of " . ($picCnt + $skipCnt) . " image files found in $opts{gd} were valid.\n";
     }
 
     # figure out how many index pages need to be generated (zero based)
@@ -1058,18 +1063,18 @@ sub convertFileNames {
             }
         }
 
-        # change the filenames in the gallery.dat file too.
-        if (-e $galDatFile) {
-            print "Updating $galDatFile\n";
+        # change the filenames in the gallery data file too.
+        if (-e $opts{gd}) {
+            print "Updating $opts{gd}\n";
 
-            # move the gallery.dat file to a temp file
-            my $tmpGalDatFile = "tmp-$galDatFile";
-            my $dieMsg = "Cannot move $galDatFile to $tmpGalDatFile!\n";
-            move $galDatFile,$tmpGalDatFile or die "$dieMsg$!\n";
+            # move the gallery data file to a temp file
+            my $tmpGalDatFile = "tmp-$opts{gd}";
+            my $dieMsg = "Cannot move $opts{gd} to $tmpGalDatFile!\n";
+            move $opts{gd},$tmpGalDatFile or die "$dieMsg$!\n";
 
             # open for temp file for reading, original file for writing
             open RFILE, "<$tmpGalDatFile" or die "Cannot open \'$tmpGalDatFile\'\n";
-            open WFILE, ">$galDatFile" or die "Cannot open \'$galDatFile\'\n";
+            open WFILE, ">$opts{gd}" or die "Cannot open \'$opts{gd}\'\n";
 
             # read in each line and change if need be
             while (<RFILE>) {
@@ -1144,25 +1149,25 @@ sub getImgInfo {
 }
 
 ###########################################################
-# createNewGalDat - create a new gallery.dat file in the
+# createNewGalDat - create a new gallery data file in the
 # current directory from the list of valid filenames in the albumInfo
 #
 sub createNewGalDat {
     my (%opts) = %{$_[0]};
     my ($albumInfo) = $_[1];
-    my $yn = "y"; # create galDatFile
+    my $yn = "y"; # create gallery data file
 
-    if (-e $galDatFile) {
+    if (-e $opts{gd}) {
         $yn = ""; # clear this option out so we can ask the user
 
         # while we don't have a yes or no answer from the user
         while (!($yn =~ /^[yn]$/i)) {
             print "\n----------------------------------------------------\n";
             print "You have called $progName with the --create-gallerydat option\n";
-            print "$progName has detected that a $galDatFile already exists.\n";
+            print "$progName has detected that a $opts{gd} already exists.\n";
             print "If you overwrite this file all current descriptions and\n";
             print "customizations will be LOST!!\n";
-            print "\nWould you like to overwrite the current $galDatFile?\n";
+            print "\nWould you like to overwrite the current $opts{gd}?\n";
             print "y or (n) > ";
             $yn = <STDIN>; # get the input from the user
             chop $yn;
@@ -1173,9 +1178,9 @@ sub createNewGalDat {
 
     # if we wanted to
     if ($yn eq "y") {
-        print "Creating $galDatFile\n";
+        print "Creating $opts{gd}\n";
 
-        open(GALFILE,"> $galDatFile") || die "Cannot open \'$galDatFile\' $!\n";
+        open(GALFILE,"> $opts{gd}") || die "Cannot open \'$opts{gd}\' $!\n";
         print GALFILE "# Options can be placed after GAL-OPTIONS tag.\n";
         print GALFILE "# They should be on one line and entered like they would\n";
         print GALFILE "# on the command line. e.g. GAL-OPTIONS -ft -fs -rs y\n";
@@ -1201,7 +1206,7 @@ sub createNewGalDat {
         }
         close GALFILE;
     } else {
-        print "Skipping creation of $galDatFile\n";
+        print "Skipping creation of $opts{gd}\n";
     }
 }
 
@@ -1557,14 +1562,17 @@ sub printUsage {
 print <<endOfPrint;
 Usage: $progName [options] [directories]
 [options]
+-gd  --gallery-data-file <string>
+                    : Gallery data file name to use.
+		      Default is 'gallery.dat'.
 -cg  --create-gallerydat
-                    : Create a gallery.dat file and exit
+                    : Create a gallery data file and exit
 -uec --use-exif-comment
                     : Use the comment field in the exif header (if it exists)
                       as the slide comment. This will overwrite the description
-                      that is written in a gallery.dat file. If used in 
+                      that is written in a gallery data file. If used in 
                       conjunction with the -cg option, the comment field will
-                      be saved in the gallery.dat file.
+                      be saved in the gallery data file.
                       Default is DISABLED. (can be negated).
 -uec --use-exif-thumb
                     : Use the exif integral thumbnail image (if it exists)
@@ -1578,7 +1586,7 @@ Usage: $progName [options] [directories]
 -it --index-title <string>
                     : Sets the title and title-bar of the index.html page
                       to string. This is meant to be used when you are not
-                      using a gallery.dat file.
+                      using a gallery data file.
 -iw --index-width <1-255>
                     : How many thumbnails per line you want on the index page.
                       Default is 5
@@ -1652,7 +1660,7 @@ Usage: $progName [options] [directories]
 -gb --go-back       : Prepend a "Go Back" link to the header in the index.html
                       file. If used in conjunction with -cg, the Go Back link
                       will be added to the INDEX-HEADER tag in the newly
-                      created gallery.dat file.
+                      created gallery data file.
                       Default is DISABLED. (can be negated).
 -gbs --go-back-string <string>
                     : String to use for the go-back-url link. You could even
@@ -1697,7 +1705,7 @@ Default directory is "."
 
 Options can be used in any of three places: (listed ascending precedence)
 - A single line in the \$HOME/.jigl/jigl.opts file in the users home directory.
-- After the GAL-OPTIONS tag as a single line in the gallery.dat file in
+- After the GAL-OPTIONS tag as a single line in the gallery data file in
   each of the album directories.
 - On the command line.
 Regardless of where they are located, they should all be listed in the same
-- 
1.7.7

