#!/usr/bin/perl -w
use strict;

my(@files,@t,$i,$image,$thumbnail,$dir,$dir1,$dir2);

if(!$ARGV[0]) { 
printf("No image dir specified..\n");
printf("example:\n");
printf("./mkthumbnail.pl <image dir>\n");
exit 0;
}

$dir=$ARGV[0];
#$dir=~ s{/}{}g;

if(!-d $dir) { 
printf("Error: $dir dir not present here...\n"); exit 0; } 

$dir1=join('',$dir,"/images"); # image files
if(!-d $dir1) { 
printf("Error: \"images\" dir is not present inside \"$dir\" dir...\n"); exit 0; } 

$dir2=join('',$dir,"/thumbs"); # thumb files
if(!-d $dir2) { 
printf("Creating \"thumbs\" dir inside \"$dir\" dir...\n");
system("mkdir $dir2");
} 



# allowed image formats "png","jpg","jpeg","gif","PNG","JPG","JPEG","GIF"
chomp(@files=`ls -1 $dir1/*.* | grep -v ".mp4" | grep -v ".MP4" | grep -v ".YOUTUBE"`);


$i=0;
foreach $image (@files){ 
$i++;

@t=split(/\//,$image);
@t=reverse(@t);

$thumbnail=join('/',$dir2,$t[0]);

printf("$i Generating thumbnail for $image\n"); 
system("/usr/bin/convert -thumbnail 200 \"$image\" \"$thumbnail\""); 
} 
