#!/usr/bin/perl

use CGI qw(:standard);

print header();

my $debug  = 0;
my $config = "galleryconfig.pl";
my $gallery;
$gallery   = param("gallery");
$gallery   = "." unless defined $gallery;
my $page;
$page      = param("page");
$page      = 1 unless defined $page;
my $url    = $ENV{'REDIRECT_URL'} . "?gallery=$gallery" ;
my $dir    = "$gallery/gallery";
print "$gallery, $page, $url, $dir<br>\n" if $debug;

# Gallery specific globals
print "$gallery/$config<br>\n" if $debug;
require "$gallery/$config";
if ($debug) {
    foreach my $key (sort(keys(%GALLERY))) {
	print "$key = $GALLERY{$key}<br>\n";
    }
}

print_html_header();
print_html_page();
print_html_footer();

sub print_html_page {

    my $itms = 0;
    my $rows = 0;
    my $cols = 12/$GALLERY{'LINE'};
    open(IMG, "cd $dir; ls -t */$GALLERY{'FILE'} | ");
    while (<IMG>) {
        s/\r?\n//;
        my $file = $_;
	if ($page eq "all" || int($itms / $GALLERY{'PAGE'}) + 1 == $page) {
	    if ($itms % $GALLERY{'LINE'} == 0) {
		print "</div>\n"    if ($rows > 0);
		print "<div class='row'>\n";
		$rows++;
	    }
	    ($name=$file) =~ s/\/.*$//;
	    my $dt = `date -r "$dir/$file" "+%d%b %H:%M"`;
	    print <<EOF;
<div class="col-md-$cols portfolio-item">
  <a href="$dir/$file">
    <img class="img-responsive" src="$dir/$file" alt="">
  </a>
  <h6><a href="$dir/$file">$name</a> <div class="pull-right"><small>$dt</small></div></h6>
</div>
EOF
	}
	$itms++;
    }
    print "</div>\n"    if ($rows > 0);
    close(IMG);
    if ($itms > $GALLERY{'PAGE'}) {
	my $max_pages = int($itms / $GALLERY{'PAGE'}) + 1;

        my ($previous_disabled, $previous_link, $previous_page);
        if ($page <= 1) {
            $previous_disabled = "disabled";
            $previous_link = "#";
        } else {
            $previous_page = $page - 1;
            $previous_disabled = "";
            $previous_link = "$url&page=$previous_page";
        }
	print <<EOF;
        <!-- Pagination -->
        <div class="row text-center">
            <div class="col-lg-12">
                <ul class="pagination pagination-lg">
                    <li class="$previous_disabled">
                        <a href="$previous_link">&laquo;</a>
                    </li>
EOF

        my $p = 1;
	while ($p <= $max_pages) {
	    if ($p == $page) {
		$active = "active";
	    } else {
		$active = "";
	    }
	    print <<EOF;
                    <li class="$active">
                        <a href="$url&page=$p">$p</a>
                    </li>
EOF
            $p = $p + 1;
        }

        my ($next_disabled, $next_link, $next_page);
        if ($page >= $max_pages) {
            $next_disabled = "disabled";
            $next_link = "#";
        } else {
            $next_page = $page + 1;
            $next_disabled = "";
            $next_link = "$url&page=$next_page";
        }
        print <<EOF;
                    <li class="$next_disabled">
                        <a href="$next_link">&raquo;</a>
                    </li>
                </ul>
            </div>
        </div>
        <!-- /.row -->
EOF
    }
}

sub print_html_header {

    print <<HEADER;
<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="utf-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1">
    <meta name="description" content="">
    <meta name="author" content="">

    <title>$GALLERY{'TITLE'}</title>

    <!-- Bootstrap core CSS -->
    <link href="http://www.cse.unsw.edu.au/~give/css/bootstrap.min.css" rel="stylesheet">
  </head>
  <body>
    <div class="container">
      <h1 class="page-header">$GALLERY{'TITLE'}  <small><a href="$dir" class="pull-right">Gallery Index</a></small></h1>
HEADER
}

sub print_html_footer {
    print <<FOOTER;
    </div>
  </body>
</html>
FOOTER
}

