#!/usr/bin/perl 
# this script is a part of the zgal gallery generator
# http://zgal.sourceforge.net
# feeback to "znoopy2k at gmx dot net"

sub helpmsg() {
    print(
        "+-------------------------------------------------------+\n",
        "| Jpegresizer version 1.11111111111 ( a frontend for Imagemagick )|\n",
        "| ----------------------------------------------------- |\n",
        "| resizes ALL jpeg files in the current directory       |\n",
        "| usage: jpeg_resize [ res number ] [ quality ]         |\n",
        "| default quality is 85%                                |\n",
        "|                                                       |\n",
        "| EXPAMPLE:  jpeg_resize 2  75                          |\n",
        "|                                                       |\n",
        "|   [ 1 ]    160x120                                    |\n",
        "|   [ 2 ]    640x480                                    |\n",
	"|   [ 3 ]    800x600                                    |\n",
        "|   [ 4 ]   1024x768                                    |\n",
        "|   [ 5 ]   1280x960                                    |\n",
	"|   [ 6 ]  1600x1280                                    |\n",
        "+-------------------------------------------------------+\n"
    );
    exit(0);
}

if ( $ARGV[0] eq "" ) { helpmsg(); }

if ( $ARGV[0] < 1 || $ARGV[0] > 4 ) { helpmsg(); }
if ( $ARGV[0] == "1" ) { $res = "160x120" }
if ( $ARGV[0] == "2" ) { $res = "640x480" }
if ( $ARGV[0] == "3" ) { $res = "800x600" }
if ( $ARGV[0] == "4" ) { $res = "1024x768" }
if ( $ARGV[0] == "5" ) { $res = "1280x960" }
if ( $ARGV[0] == "6" ) { $res = "1600x1280" }

if ( $ARGV[1] <= 20 ) {
    print(
"you chose a quality below 20%. That's not a good idea !!!\nUSING DEFAULT QUALITY 75\n"
    );
    $quality_def = 85;
}
if ( $ARGV[1] <= 100 && $quality_def != 85 ) { $quality = $ARGV[1]; }
else { $quality = 75; }

@all_pics_jpg1  = glob("./*.JPG");
$number_of_pics = @all_pics;
@all_pics_jpg2  = glob("./*.jpg");
push ( @all_pics_jpg1, @all_pics_jpg2 );
$number_of_pics = @all_pics_jpg1;
print "\n\n$number_of_pics Jpeg files found \n";

##### thumbnail generation with Image-magick 
print("resizing jpegs\n");

@thumbnails = @all_pics;
system("mkdir resized_to_$res");
$counter = 1;
while (@all_pics_jpg1) {
    $name = shift @all_pics_jpg1;
    $name = substr( $name, 2 );
    system(
        "convert -geometry $res  -quality $quality $name  resized_to_$res/$name"
    );
    print("\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b$counter/$number_of_pics");
    $counter++;
}
print("\n");

