#!/bin/perl

#  Init Lighttpd config files
#
use strict qw(vars);
use File::Spec;
use FindBin qw($RealBin);
use Cwd qw(realpath);
use lib $RealBin;
use perl5lib File::Spec->catdir($RealBin, File::Spec->updir());
use WebDyne::Base;
use Getopt::Long;
use Pod::Usage;


#  Get command line options, add them to environment so picked up
#  by WebDyne::Install::Apache::Constant module when loaded
#
my %option;
GetOptions(\%option, (
    'help|?',
    'lighttpd_uname|uname=s',
    'lighttpd_gname|gname=s',
    'file_lighttpd_conf|lighttpd_conf=s',
    'webdyne_cache_dn|webdyne_cache=s',
    'silent',
    'uninstall'
   ) ) ||  pod2usage(2);
pod2usage(1) if $option{'help'};
map { $ENV{uc($_)}=$option{$_} } keys %option;


#  By default the WebDyne::Install::Lighttpd::Constant module will issue a
#  warning if something not found, but will continue. In this case we want
#  to consider any warning fatal
#
local $SIG{'__WARN__'}=sub { pod2usage("error: $_[0]") };
require WebDyne::Install::Lighttpd;


#  Get location prefile, call install/uninstall routine
#
my $prefix_dn=realpath(File::Spec->rel2abs(File::Spec->updir(),$RealBin));
if ($option{'uninstall'}) {
    exit ${ WebDyne::Install::Lighttpd->uninstall($prefix_dn, $RealBin) || die errstr() }
}
else {
    exit ${ WebDyne::Install::Lighttpd->install($prefix_dn, $RealBin) || die errstr() }
}

__END__

=head1 SYNOPSIS

wdlighttpdinit [options]

Options:

=over

=item -h|-?|--help

brief help message

=item --silent

do not print any messages during install

=item --uninstall

remove Webdyne config files and any Lighttpd config file changes

=item --uname

user name used by the Lighttpd process

=item --gname

group name used by the Lighttpd process

=item --lighttpd_conf

directory where Lighttpd config file(s) are located

=item --webdyne_cache

directory where you would like the WebDyne cache located (will be created)

=back

The installer will attempt to guess the values of each option above using common defaults. If it cannot determine
the location of a file or directory it needs you must supply it as a command line or environment variable option.

=head2 EXAMPLE

=over

C<wdlighttpdinit --lighttpd_conf=/etc/lighttpd --webdyne_cache=/var/cache_webdyne>

