From 22f26c550c5f3ccf59de2d65408c1f30649b60e6 Mon Sep 17 00:00:00 2001
From: Peter Seiderer <seiderer@localhost.(none)>
Date: Sun, 29 Jan 2012 10:54:18 +0100
Subject: [PATCH 03/11] Move gallery data file creation to extra subroutine.

Work on given directory list (instead of processing every directory
alone).
---
 jigl.pl |  111 ++++++++++++++++++++++++++++++++++++--------------------------
 1 files changed, 64 insertions(+), 47 deletions(-)

diff --git a/jigl.pl b/jigl.pl
index bc8650e..ba1dd05 100755
--- a/jigl.pl
+++ b/jigl.pl
@@ -102,6 +102,13 @@ if ($optsCmd{r}) {
     @dirs = @rDirs;
 }
 
+if ($optsCmd{cg}) {
+    print "Generating $galDatFile file\n";
+    genGalleryDat(\@dirs);
+    print "\n$progName has finished processing\n";
+    exit 0;
+}
+
 # run on each directory given on the cmdLine.
 foreach $dir (@dirs) {
 
@@ -786,7 +793,63 @@ sub mergeOpts {
     return %opts;
 }
 
-###########################################################
+sub genGalleryDat {
+    my (@dirs) = @{$_[0]};
+    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";
+
+    foreach $dir (@dirs) {
+        # skip any directory that is not valid
+	if (! (-d $dir)) {
+            for (my $i=0;$i<30;$i++) {print "-"};
+	    print "\n\'$dir\' is not a valid directory. Skipping!\n";
+	    next;
+	}
+
+        print "Checking for files in directory.\n";
+
+        # get a list of files to work on
+        my $fileTypeStr = "$dir/$jpgTypeStr $dir/$gifTypeStr $dir/$pngTypeStr";
+        my @files = glob($fileTypeStr);
+
+        # stuff each file into the hash and push it on the list
+        foreach $file (@files) {
+            # create a new imgInfo structure and fill in it's name
+            $href = &newImgInfo;
+            $href->{file} = trimWS($file);
+
+            # rotate the original before we get it's size,X,and Y info.
+            &rotateOrig($href->{file}) if $opts{aro};
+
+            # get the files size and X,Y info
+            @tmpArr = &getImgInfo($href->{file});
+            $href->{size} = $tmpArr[0];
+            $href->{width} = $tmpArr[1];
+            $href->{height} = $tmpArr[2];
+
+            # push the new image into the array if the image is valid
+            if ($href->{size} ne "") {
+                push @{$aInfo->{images}},$href;
+                $filesWithSpaces++ if ($href->{file} =~ / /);
+                $picCnt++;
+            } else {
+                print "Skipping invalid image file: $file\n";
+                $skipCnt++;
+            }
+         }
+
+        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
+    &createNewGalDat(\%opts, $aInfo);
+}
+
+##########################################################
 # genAlbumInfo - generate the master array containing info about the album.
 #
 # options that concern this function:
@@ -908,46 +971,6 @@ sub genAlbumInfo {
         }
         close GALFILE; # close the gallery.dat file
         print "$picCnt of " . ($picCnt + $skipCnt) . " image files found in $galDatFile were valid.\n";
-
-    } else {
-        # no gallery.dat file found - just using image files found in dir.
-        # get a list of files to work on
-        my @files = glob($fileTypeStr);
-
-        if ($opts{cg}) {
-            print "Creating a new $galDatFile. Checking for files in directory.\n";
-        } else {
-            print "No $galDatFile file found. Checking for files in directory.\n";
-        }
-
-        # stuff each file into the hash and push it on the list
-        foreach $file (@files) {
-            # create a new imgInfo structure and fill in it's name
-            $href = &newImgInfo;
-            $href->{file} = trimWS($file);
-
-            # rotate the original before we get it's size,X,and Y info.
-            &rotateOrig($href->{file}) if $opts{aro};
-
-            # get the files size and X,Y info
-            @tmpArr = &getImgInfo($href->{file});
-            $href->{size} = $tmpArr[0];
-            $href->{width} = $tmpArr[1];
-            $href->{height} = $tmpArr[2];
-
-            # push the new image into the array if the image is valid
-            if ($href->{size} ne "") {
-                push @{$aInfo->{images}},$href;
-                $filesWithSpaces++ if ($href->{file} =~ / /);
-                $picCnt++;
-            } else {
-                print "Skipping invalid image file: $file\n";
-                $skipCnt++;
-            }
-        }
-
-        print "$picCnt of " . ($picCnt + $skipCnt) ." image files found in the directory were valid.\n";
-
     }
 
     # figure out how many index pages need to be generated (zero based)
@@ -971,12 +994,6 @@ sub genAlbumInfo {
         $aInfo->{header} = $gbStr;
     }
 
-    # create a new gallery.dat file from the files hash and return
-    if ($opts{cg}) {
-        &createNewGalDat(\%opts, $aInfo);
-        return ();
-    }
-
     return $aInfo;
 }
 
-- 
1.7.7

