use Image::Size;

my $fname = "JPGfilelist.txt";

open (IN, $fname) || die "can't open $fname\n";

my $count = 0;
my $prevpic = undef;

my $settitle;

while (<IN>) {
    chomp;
    next if /^#/;
    my ($picname, $pictext) = split (/:/,$_);

    $settitle = $pictext and next
	if ($picname eq "title");

    my $picroot = $picname;
    $picroot =~ s/\.JPG//i;

    # make list of pictures, save their captions and real names
    push @pics, $picroot;
    $captions{$picroot} = $pictext;
    $longname{$picroot} = $picname;

    # if it doesn't have a thumbnail we have to make one
    make_resize($picname);

}

#### making thumbnails thingies wheeee
sub make_resize {

    my $picname = shift;
    my $picroot = $picname;
    $picroot =~ s/\.JPG//i;

    my ($width, $height) = imgsize($picname);
    return unless $width;
    print "making thumbnail for $width x $height $picname\n";
    my $thumbheight, $thumbwidth;
    if ($width > $height) {
	$thumbwidth = 500;
	$thumbheight = (500 * $height) / $width;
    }
    else {
	$thumbheight = 375;
	$thumbwidth = int ((375 * $width) / $height);
    }
    my $thumbname = "thumb_$picroot.jpg";

    system "cp $picname $thumbname";
    system "mogrify -resize ${thumbwidth}x$thumbheight $thumbname";
}
