#!/usr/bin/perl
use warnings;
use strict;
use feature 'say';
use Cwd;
use IPC::Cmd 'can_run';
use File::Spec::Functions qw(catfile catdir);
use Photography::Website;

=head1 NAME

Photog! - The Photography Website Generator

=head1 SYNOPSIS

B<photog> [I<-q>] [I<-v>] [I<destination>]

B<photog->[B<scale>, B<watermark>, B<thumbnail>, B<preview>]

=head1 DESCRIPTION

Photog! turns a directory tree of source images into a photography
website with nested albums of chronologically sorted photographs. To
get started, simply C<cd> to the source directory and call C<photog>:

    $ cd Pictures
    $ photog

Upon the first run, Photog! asks the user for a destination directory,
which it writes to the file C<photog.ini> inside the source
directory. The destination can also be specified on the command line
as the final argument, or inside a pre-existing C<photog.ini>
file. Subsequent runs of the C<photog> command do nothing unless the
contents of the source directory have changed. Photog! only
regenerates the parts of the website that should be updated.

=head1 OPTIONS

=over

=item B<-q>

Be less verbose

=item B<-v>

Be more verbose

=back

=head1 CONFIGURATION

Photog! reads configuration directives from a file named C<photog.ini>
inside the source directory. Subdirectories can contain additional
configuration files that override certain settings from the root
configuration file. A value set in the configuration file will become
the new default value for all child albums.

Config files consist of a number of C<variable = value>
directives. Empty lines and lines starting with a C<#> are
ignored. All the possible configuration variables are documented in
the manual page of L<Photography::Website::Configure>, the module that
implements Photog!'s configuration system. Here is an example
configuration file:

    # This is an example ~/Pictures/photog.ini
    title = Super Formosa Photography
    watermark = /home/jj/watermark.png
    template = /home/jj/frontpage-template.html
    destination = /var/www/superformosa.nl

This tells Photog! the title, watermark, template, and destination to
use when generating the album that corresponds to the directory
C<~/Pictures>. The albums that are created from the subdirectories
inside C<~/Pictures> will also have the same title, watermark,
template and destination, because these values are inherited by all
child albums (unless they are overridden by additional C<photog.ini>
files). Again, for a complete list of all possible configuration
variables consult the Photography::Website::Configure(3) manual page.

=head1 TEMPLATING

Photog! comes with a file named C<template.html> that uses
Template::Toolkit syntax to render each album's C<index.html>. The
default template uses Javascript to "pack" images into horizontal
rows. You can supply the path to your own template with the
B<template> configuration variable.

The easiest way to create your own template is to start with a copy of
the default template. The location of the default template varies by
platform, but it can always be found with the following Perl
oneliner:

    perl -MFile::ShareDir=dist_file -E \
    'say dist_file("Photography-Website", "template.html")'

All the configuration options automatically become template
variables. An additional template variable, B<items>, contains the
sorted list of the album's children. Here is an example template (see
L<Template::Manual::Intro> for an introduction to the template
syntax):

    <h1>Welcome to [% title %]!</h1>
    <p>These are my photo albums:</p>
    [% FOREACH item in items %]
      [% IF item.type == 'album' %]
        <img src="[% item.src %]" title="[% item.my_custom_title %]">
      [% ENDIF %]
    [% ENDFOR %]

As you can see, this example references the attribute
C<my_custom_title> of each child album. This is not an "official"
configuration option, but as long as you set it yourself in the
album's C<photog.ini> it will be available to all child albums just
like the regular configuration variables.

=head1 ADDITIONAL COMMANDS

Photog! calls the commands C<photog-scale>, C<photog-watermark>,
C<photog-thumbnail>, and C<photog-preview> to generate scaled,
watermarked, thumbnail and preview images. These commands are simple
shell scripts that have been installed to the same path as the
C<photog> command. They call ImageMagick to do the actual image
processing. Each command prints out a simple usage instruction when
called with no arguments. If you want to change Photog!'s behavior for
generating images and thumbnails, you can supply your own commands in
the configuration file (see L<Photography::Website::Configure>).

=head1 SEE ALSO

L<Photography::Website>, L<Photography::Website::Configure>

=head1 AUTHOR

Photog! was written by Jaap Joris Vens <jj@rtts.eu>, and is used to create
his personal photography website at L<http://www.superformosa.nl/>

=cut

######################################################################

my $source = getcwd();
my $destination;
my $silent = 0;
my $verbose = 0;

# Process arguments
for (@ARGV) {
    if ($_ eq '-v') {
        $verbose = 1;
        $Photography::Website::verbose = 1;
    }
    elsif ($_ eq '-q') {
        $silent = 1;
        $Photography::Website::silent = 1;
    }
    elsif (not defined $destination) {
        $destination = $_;
    }
    else {
        die "ERROR: Too many command-line arguments\n";
    }
}

say "Welcome to Photog!" unless $silent;

die <<EOM

Please install ImageMagick in order to use Photog!, preferably using
your operating system's package manager, or otherwise by downloading
and running the appropriate installer from http://imagemagick.org/

This error message will go away when the ImageMagick commands
`convert` and `composite` can be executed. If you have installed
ImageMagick but are still seeing this error message, you should make
sure that the directory that contains these commands is included in
your \$PATH

If you need help, please read the Installation Guide located at
http://photog.created.today/install/ or drop me a line at jj\@rtts.eu

EOM
unless can_run('convert') and can_run('composite');

my $ini = catfile($source, 'photog.ini');
if (not -f $ini) {
    until (defined $destination and -d $destination) {
        if (defined $destination) {
            say "Creating destination directory $destination";
            mkdir $destination or die "ERROR: Couldn't create destination directory ($!)\n";
        }
        else {
            my $default = catdir('..', 'My-Photography-Website');
            print "Please specify a destination directory, or press Enter for the default [$default]: ";
            $destination = <STDIN>;
            chomp $destination;
            $destination ||= $default;
            if (-d $destination) {
                print "This directory already exists. All of its contents will be deleted. Are you sure you want to continue? [y/N] ";
                my $answer = <STDIN>;
                chomp $answer;
                die "GAME OVER. Better luck next time!\n" unless lc($answer) =~ /^ye?s?$/;
            }
        }
    }
    open(my $fh, '>', $ini);
    print $fh "destination = $destination\n";
    close $fh;
}

say "Processing source directories..." unless $silent;
my $website = Photography::Website::create_album($source);

say "Generating website resources..." unless $silent;
Photography::Website::generate($website);

print "\n" unless $silent;

my @greetings = (
    "Keep on photographing!",
    "See you next time!",
    "Lovely pictures!",
    "Love your work!",
    "Signing off...",
    "One day, you'll be famous.",
    "Keep 'm coming!",
    "Request new features on Github.",
    "It's wonderful!",
    "Wasn't that fast?",
);

my $greeting = $greetings[int(rand(@greetings))];
say "Website generation complete. $greeting" unless $silent;
