#!/usr/bin/perl

use POSIX qw(ceil);

$ind            =       0;
$dump           =       `du -a | egrep -i 'png|bmp|jpg|tif'`;
@pics           =       split(/\n/,$dump);
$width          = 12;
$length         = 20;

if (@ARGV[$#ARGV] eq "")
{
        print "You must supply an output file name!\n";
        exit();
}

foreach (@pics)
{
        s/./"/;
        s/\d+\t//;
        $_ .= "\"";
        @pics[$index] = $_;
        $index++;
}

# fisher_yates_shuffle( \@array ) :
# generate a random permutation of @array in place
sub fisher_yates_shuffle {
        my $array = shift;
        my $i;
                for ($i = @$array; --$i; ) {
                my $j = int rand ($i+1);
                next if $i == $j;
                @$array[$i,$j] = @$array[$j,$i];
        }
}

if(@ARGV[0] eq "-shuffle")
{
        fisher_yates_shuffle( pics );
        $shuffle = 1;
}
else
{
        $shuffle = 0;
}

print "Processing $#pics pictures...\n";
print "File saving as \"";

$height = ceil($#pics / 12);
$dimensions = join('',$width,'x',$height);

if ($shuffle == 1)
{
        print "@ARGV[1]\"\n";
        system "montage -geometry 80x60 -tile $dimensions  -background grey @pics @ARGV[1]";
}
else
{
        print "@ARGV[0]\"\n";
        system "montage -geometry 80x60 -tile $dimensions -background grey @pics @ARGV[0]";
}