#!/usr/bin/perl 
# wfindapi.pl - Generate web server file list JSON API
#
# Copyright 2014 David Meyer <papa@sdf.org> +JMJ
# (License and documentation at bottom of file.)

# Modules ###########################################################
use strict;
use warnings;
use POSIX;               # strftime() for timestamp formatting
use JSON;                # Convert Perl data <-> JSON text

# Configuration #####################################################
our $ROOTURL = 'http://totallynuclear.club';
                         # Web host top page URL
our $ROOTPATH = '/var/www/html';
                         # File system path to web root directory
                         # - Set by DocumentRoot in Apache config.
our $USERPATH = '/home/*/public_html';
                         # FS path to user web page directories
                         # - Set from UserDir in Apache configuration
                         # - Asterisk is replaced by user names, 
                         #   supports globbing find search path
our $UPATHRE = '/home/(\w+)/public_html';
                         # Perl regular expression to match $USERPATH
our $FIND = '/bin/find'; # Full path of FIND program: eliminates
                         # dependency on $PATH

# Command line arguments ############################################
our $fspec;
if ($#ARGV == 0) {       # If one argument ...
    $fspec = "-type f -a -name '$ARGV[0]'";
                         #  FIND will search for regular files with
                         #  names matching arg
}
elsif (@ARGV) {          # Else if more than one argument ...
    my @fspecs = ();
    for (@ARGV) {
	push @fspecs, "-name '$_'";
                         #  Prepend arg w/ "-name" to make find opt.
    }
    $fspec = '-type f -a \( ' . (join ' -o ', @fspecs) . ' \)';
                         #  FIND will search for regular files with
                         #  names matching any arg
}                        
else {                   # Else (no arguments)
    $fspec = '-type f';  #  FIND will search for all regular files
}
	
# Main ##############################################################
our @filelist = ();      # Array to store list of file info

open WFILE, "$FIND $ROOTPATH $USERPATH $fspec 2>/dev/null |";
                         # FIND searches web server root directory
                         # file tree and user web file trees for
                         # regular files matching pattern argument(s).

while (<WFILE>) {        # Repeat for each file found by FIND.
#    $urlpath = $user = $basename = $url = "";
    my ($urlpath, $user, $basename, $url) = "";
                         #  Initialize work variables.
    chop $_;             #  Chop off end-of-line character.
    if ($_ =~ m:^$ROOTPATH(.*)/([^/]+)$:) {
                         #  If file name starts with site root path
	$urlpath = $1;   #   Extract URL path
	$basename = $2;  #   Extract file base name (w/out path)
	$user = '';      #   User is undefined for site root files
	$url = $ROOTURL . $urlpath . '/' . $basename;
                         #   Construct file URL from components
    }
    else {
	$_ =~ m:^$UPATHRE(.*)/([^/]+)$:;
                         #   Match file name with user web path RE
	$user = $1;      #   Extract user
	$urlpath = $2;   #   Extract URL path
	$basename = $3;  #   Extract file base name
	$url = $ROOTURL . "/~$user" . $urlpath . '/' . $basename;
                         #   Construct file URL from components
    }

    my %fileobj = (      # Hash stores data for current file.
	file => $_,
	user => $user,
	urlpath => $urlpath,
	basename => $basename,
	url => $url
    );
    push @filelist, \%fileobj;
                         # Add ref. to file info hash to file list array. 
}

our @fspec = @ARGV;
our %flobj = (
    runtime => strftime("%Y-%m-%dT%H:%M:%S%z", localtime),
    rooturl => $ROOTURL,
    rootpath => $ROOTPATH,
    userpath => $USERPATH,
    filespec => \@fspec,
    filelist => \@filelist
);

our $json = new JSON;
print $json->pretty([$JSON::enable])->encode(\%flobj) . "\n";;

exit 0

__END__

# Documentation #####################################################
=head1 NAME

wfindapi.pl - Generate web server file list JSON API

=head1 USAGE

B<perl wfindapi.pl> [I<pattern> ...]

=head1 DESCRIPTION

Uses B<find> to search web server root and user directories for
regular[1] files with base names matching I<pattern> (defaults to
listing all files).

Outputs the following JSON schema to standard output.

{
    C<"runtime" : ">I<datetime>C<",>
    C<"rooturl" : ">I<url>C<",>
    C<"rootpath" : ">I<path>C<",>
    C<"userpath" : ">I<path>C<",>
    C<"filespec" : [>I<pattern, ...>C<],>
    C<"filelist" : [>I<FILEINFO, ...>C<]>
}

I<FILEINFO>:
{
    C<"file" : ">I<path>C<",>
    C<"user" : ">I<user>C<",>
    C<"urlpath" : ">I<path>C<",>
    C<"basename" : ">I<basename>C<",>
    C<"url" : ">I<url>C<">
}
 
=head1 CONFIGURATION

=head1 ENVIRONMENT

=over 6

=item VARIABLE

Description of usage of environment variable C<VARIABLE>.

=back

=head1 FILES

=head1 BUGS, LIMITATIONS, AND CAVEATS

=head1 NOTES

[1] The Unix FIND command considers a file to be fo "regular" type
    when it is not a directory, a symbolic link, a pipe, a socket, or
    other special file type.

=head1 AUTHOR

David Meyer <papa@sdf.org>

=head1 HISTORY

2014-11-11 Originally programmed for totallynuclear.club

=head1 COPYRIGHT AND LICENSE

Copyright 2014, David Meyer

Redistribution and use in source and binary forms, with or without
modification, are permitted provided that the following conditions
are met:

1. Redistributions of source code must retain the above copyright
   notice, this list of conditions and the following disclaimer.

2. Redistributions in binary form must reproduce the above copyright
   notice, this list of conditions and the following disclaimer in
   the documentation and/or other materials provided with the
   distribution.

THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
HOLDERS OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY
WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
POSSIBILITY OF SUCH DAMAGE.


=cut

# Emacs control #####################################################
#Local variables:
#mode: perl
#End: