#!/usr/bin/perl

use strict;
use warnings;
use File::Find;
use File::stat;
use POSIX;
use Data::Dumper;
use JSON;

my $directory = '/var/www/comix/Comics';
our %images;

sub is_image{
    my $file = shift @_;
    return 1 if ( $file =~ m/\.jpg$/ || $file =~ m/\.gif$/ || $file =~ m/.png$/ );
    return 0;
}
sub get_images {
    my $full = $File::Find::name;
    return unless is_image($full);
    #print "Got an image: $full\n";
    my $st = stat($full);
    my $mod = strftime("%d/%m/%Y", localtime $st->mtime);
    #print "Mod time = $mod\n";
    my @arr;
    if($images{$mod}){
        @arr=@{$images{$mod}};
    }
    #print "pushing $full into arr\n";
    push(@arr, $full);
    @{$images{$mod}} = @arr;
}



find(\&get_images, $directory);
$Data::Dumper::Indent = 3;
#print Dumper(%images);
print to_json(\%images, {pretty => 1});

