#! /usr/bin/env perl
# See bottom of file for license and copyright information
#
use strict;
use warnings;
require 5.008;

use File::Spec;
use Cwd;

=pod

---+ extension_installer
This is a generic installer script. The basic function of this script is to
locate an archive and unpack it. 

It is called as any of the named package installers, with the addition of the
Extension name to be installed as the first parameter to the command.  It 
*must* be run from the root of the Foswiki installation

| *Function* | *Dedicated Package* | *Generic Installer* |
| Install | =perl MyExtension_installer= | =perl tools/extension_installer MyExtension |
| uninstall | =perl MyExtension_installer uninstall= | =perl tools/extension_installer MyExtension uninstall |
| Manifest | =perl MyExtension_installer manifest= | =perl tools/extension_installer MyExtension manifest |
| Dependencies | =perl MyExtension_installer dependencies= | =perl tools/extension_installer MyExtension dependencies |

The script also functions as an *uninstaller* by passing the parameter
=uninstall= on the command-line. Note that uninstallation does *not* revert
the history of any topic changed during the installation.

=cut

# This is all done in package Foswiki so that reading LocalSite.cfg and Foswiki.cfg
# will put the config vars into the right namespace.
package Foswiki;

# The root of package URLs
my $PACKAGES_URL = 'http://foswiki.org/pub/Extensions';

# Establish where we are
my @path = ( 'tools', 'extender.pl' );
my $wd = Cwd::cwd();
$wd =~ /^(.*)$/;    # untaint
unshift( @path, $1 ) if $1;

# Extract the package name from the command.
my $pkg = shift;

die
"no package name provided on command \n  Example: \"perl tools/extension_installer MyPlugin usage\" for further instructions \n\nFailed"
  unless ($pkg);

if ( $pkg eq 'usage' ) {
    unshift( @ARGV, 'usage' );
    $pkg = 'SomeExtension';
}

my $script = File::Spec->catfile(@path);

unless ( my $return = do $script ) {
    my $message = <<MESSAGE;
************************************************************
Could not load $script
MESSAGE
    if ($@) {
        $message .= "There was a compile error: $@\n";
    }
    elsif ( defined $return ) {
        $message .= "There was a file error: $!\n";
    }
    else {
        $message .=
"An unspecified error occurred\n  Run the command from the root of your Foswiki installation!\n";
    }

    # Try again, using open. This cures some uncooperative platforms.
    if ( open( F, '<', $script ) ) {
        local $/;
        my $data = <F>;
        close(F);
        $data =~ /^(.*)$/s;    # untaint
        eval $1;
        if ($@) {
            $message .= "Error when trying to eval the file content: $@\n";
        }
        else {
            print STDERR
              "'do $script failed, but install was able to proceed: $message";
            undef $message;
        }
    }
    else {
        $message .= "Could not open file using open() either: $!\n";
    }
    die $message if $message;
}

Foswiki::Extender::install( $PACKAGES_URL, $pkg, $pkg, );

1;
__END__
Author: Crawford Currie http://wikiring.com

Foswiki - The Free and Open Source Wiki, http://foswiki.org/

Copyright (C) 2008-2010 Foswiki Contributors. Foswiki Contributors
are listed in the AUTHORS file in the root of this distribution.
NOTE: Please extend that file, not this notice.

Additional copyrights apply to some or all of the code in this
file as follows:

Copyright (C) 1999-2007 TWiki Contributors. All Rights Reserved.

This program is free software; you can redistribute it and/or
modify it under the terms of the GNU General Public License
as published by the Free Software Foundation; either version 2
of the License, or (at your option) any later version. For
more details read LICENSE in the root of this distribution.

This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.

As per the GPL, removal of this notice is prohibited.

