@rem = '--*-Perl-*--
@echo off
if "%OS%" == "Windows_NT" goto WinNT
perl -x -S "%0" %1 %2 %3 %4 %5 %6 %7 %8 %9
goto endofperl
:WinNT
perl -x -S "%0" %*
if NOT "%COMSPEC%" == "%SystemRoot%\system32\cmd.exe" goto endofperl
if %errorlevel% == 9009 echo You do not have Perl in your PATH.
goto endofperl
@rem ';
#!perl -w


use FindBin;
use lib $FindBin::Bin;
use strict;
use Getopt::Long;
use File::Path; # mkpath
use Config;
use IPM_InstallPkg;
use IPM_RM;
use IPM_Util;
use Config;


our %cfg;
$cfg{datadir} = $Config{'sitelib'} . '/ipm';            # InstallPkg, RM
mkpath($cfg{datadir}, 0, 755) if (!-d $cfg{datadir});
$cfg{cachedir} = $Config{'sitelib'} . '/ipm';      # InstallPkg, RM
mkpath($cfg{cachedir}, 0, 755) if (!-d $cfg{cachedir});

$| = 1;

return 1 if (caller());      # included by another application

our %opt;
GetOptions( 'location=s' => \$opt{'location'},
            'verbose' => \$opt{'verbose'},
            'quiet', \$opt{'quiet'},
            'debug' => \$opt{'debug'},
            'platform=s' => \$opt{'platform'},    # define platform of given tar ball
            'noclean' => \$opt{'noclean'},
            'noremoveman', \$opt{'noremoveman'}
          ) || die $!;


if (@ARGV == 0) {
    shell();
    exit;
}
else {
    docommand();
}

sub docommand
{
    # command line options
    GetOptions( 'location=s' => \$opt{'location'},
            'verbose' => \$opt{'verbose'},
            'quiet', \$opt{'quiet'},
            'debug' => \$opt{'debug'},
            'platform=s' => \$opt{'platform'},    # define platform of given tar ball
            'noclean' => \$opt{'noclean'},
            'noremoveman', \$opt{'noremoveman'}
          ) || die $!;

    my $command = shift @ARGV;

    if ($command =~ /^install$/) {
        install(@ARGV);
    }
    elsif ($command =~ /^remove$/) {
        RemovePackages(@ARGV);
    }
    elsif ($command =~ /^query$/) {
        ListInstalledPackages();
    }
    elsif ($command =~ /^search$/) {
        search(@ARGV);
    }
    elsif ($command =~ /^maketoc$/) {
        MakeTOC();
    }
    elsif ($command =~ /^exit$/ || $command =~ /^quit$/) {
        exit 0;
    }
    elsif ($command =~ /^help$/) {
        help();
    }
    elsif ($command =~ /^installpkg$/) {
        my $version = undef;
        foreach (@ARGV) { InstallPkg($_, $version); }
    }
    else {
        print "Error: Invalid command $command\n";
    }
}

sub shell
{
    print "IPM interactive shell (0.0) - type 'help' for available commands.\n";
    while (1) {
        print "IPM>";
        $_ = <STDIN>;
        chomp $_;
        next unless $_; 
        @ARGV = split (/ /, $_);
        docommand();
    }
}

sub ipm_GetRepositoryList()
{
    my @list = split(/\n/, ReadFile("$cfg{'datadir'}/repository_list.txt") || "");
    return @list;
}
sub ipm_SaveRepositoryList
{
    WriteFile("$cfg{'datadir'}/repository_list.txt", join("\n", @_));
}

sub ipm_ListInstalledPackages
{
    return get_package_list;
}

sub ipm_ListPackages
{
    my ($path) = @_;
    return ListPackages($path);
}




sub help
{
    print <<EOT
Commands:
    install PACKAGES  - installs specified PACKAGES.
    remove PACKAGES   - removes the specified PACKAGES from the system.
    query             - print list of installed packages.
    search [package]  - print list of packages in repository.
    exit              - exit the program.

Options:
  -verbose
  -quiet
  -location=repository_path     - can be a local directory or a URL

The 'package' arguments may be either package names ('foo'), pathnames
(p:/packages/foo.ppd) or URLs (http://www.indigostar.com/packages/foo.ppd).
For searches it can be a regex.

To add or remove repository paths edit the file  $cfg{'datadir'}/repository_list.txt

EOT
}


__END__
:endofperl
