#!/usr/bin/perl

# RPM Package Management System
# Copyright (C) 1995 Red Hat, Inc
#
# 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.
# 
# 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.  See the
# GNU General Public License for more details.
# 
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.

# rpm - we can rebuild it
#       we have the technology
#       better, stronger, faster

# Copyright (C) 1995 - Red Hat Software

unshift(@INC, "/usr/lib/rhs/rpm");

require "rpmgetopts.pl";
require "message.pl";
require "misc.pl";
require "opts.pl";

$DO_PREP       = 1;
$DO_BUILD      = 2;
$DO_INSTALL    = 4;
$DO_SOURCE     = 8;
$DO_BINARY     = 16;
$DO_TEST       = 32;
$DO_LIST_CHECK = 64;
$DO_CLEAN      = 128;

$ENV{'PATH'} = $rpm{'PATH'};
$ENV{'PWD'} = `pwd`;

sub sigint_handler {
    &error("SIGINT received - aborted");
}
$SIG{"INT"} = 'sigint_handler';

&setmessagelevel(1);
@args = ();
$mode = 0;

$rpm{"options"} = join(' ', @ARGV);
@rest = &rpm_getopts("ab:cdfhiUlpqsuyVFPv", @ARGV);

$bigmode = "none";
$lopt_y && ($bigmode = "verify");
$lopt_V && ($bigmode = "verify");
$lopt_q && ($bigmode = "query");
$lopt_U && ($bigmode = "install") && ($rpm{"upgrade"} = 1);
$lopt_i && ($bigmode ne "query") && ($bigmode = "install");
$lopt_i && ($bigmode eq "query") && ($querymode{"info"} = 1);
$lopt_u && ($bigmode = "uninstall");
$lopt_a && ($querywhat = "all");
$lopt_f && ($querywhat = "whence");
$lopt_F && ($querywhat = "Whence");
$lopt_p && ($querywhat = "package");
$lopt_P && ($querywhat = "Package");
$lopt_c && ($rpm{"clean"} = 1, $querymode{"list"} = 1, $querymode{config} = 1);
$lopt_l && ($querymode{"list"} = 1);
$lopt_d && ($querymode{"list"} = 1, $querymode{"doc"} = 1);
$lopt_s && ($querymode{"state"} = 1, $querymode{"list"} = 1);
$lopt_h && ($rpm{"marker"} = "hash");

if ($lopt_v == 1) {
    &setmessageverbose();
} elsif ($lopt_v >= 2) {
    &setmessagedebug();
}

# Now we can process the long arguments
foreach $_ (@rest) {
    ($rpm{"root"} == 1) && ($rpm{"root"} = $_, $rpm{"rootspec"} = $_, next);
    ($rpm{"timecheck"} == -1) && ($rpm{"timecheck"} = $_, next);
    /^--upgrade$/ && ($bigmode = "install") && ($rpm{"upgrade"} = 1, next);
    /^--root$/ && ($rpm{"root"} = 1, next);
    /^--short-circuit$/ && ($rpm{"short_circuit"} = 1, next);
    /^--clean$/ && ($rpm{"clean"} = 1, next);
    /^--time-check$/ && ($rpm{"timecheck"} = -1, next);
    /^--test$/ && ($rpm{"test"} = 1, $rpm{"keeptemps"} = 1, next);
    /^--quiet$/ && (&setmessagequiet(), next);
    /^--percent$/ && ($rpm{"marker"} = "percent", next);
    /^--hash$/ && ($rpm{"marker"} = "hash", next);
    /^--force$/ && ($rpm{"force"} = 1, next);
    /^--query$/ && ($bigmode = "query", next);
    /^--verify$/ && ($bigmode = "verify", next);
    /^--keep-temp/ && ($rpm{"keeptemps"} = 1, next);
    /^--verbose$/ && (&setmessageverbose(), next);
    /^--install$/ && ($bigmode ne "query") && ($bigmode = "install");
    /^--uninstall$/ && ($bigmode = "uninstall");
    /^--debug$/ && (&setmessagedebug(), next);
    /^--help$/ && ($bigmode = "help", next);
    /^-/ && (&error("Unrecognized option: $_"));
    push(@args, $_);
}
($rpm{"root"} == 1) && &error("--root requires argument");

if ($lopt_b) {
    $_ = $lopt_b;
    $bigmode = "build";
    /^p$/ && ($mode = $DO_PREP);
    /^l$/ && ($mode = $DO_LIST_CHECK);
    /^b$/ && ($mode = $DO_PREP | $DO_BUILD | $DO_INSTALL | $DO_BINARY | $DO_CLEAN);
    /^a$/ && ($mode = $DO_PREP | $DO_BUILD | $DO_INSTALL | $DO_BINARY | $DO_SOURCE | $DO_CLEAN);
    if ($rpm{"short_circuit"}) {
	/^c$/ && ($mode = $DO_BUILD);
	/^i$/ && ($mode = $DO_INSTALL);
    } else {
	/^c$/ && ($mode = $DO_PREP | $DO_BUILD);
	/^i$/ && ($mode = $DO_PREP | $DO_BUILD | $DO_INSTALL);
    }
}

## All options are processed!

if ($bigmode ne "build") {
    $rpm{"require_vendor"} = 0;
    $rpm{"require_distribution"} = 0;
    $rpm{"require_group"} = 0;
    $rpm{"require_icon"} = 0;
}

if ($bigmode eq "build") {
    $h = `hostname`;
    if ($h =~ /redhat\.com/) {
	if (! ($rpm{"require_vendor"} &&
	       $rpm{"require_distribution"} &&
	       $rpm{"require_group"})) {
	    error("Bad /etc/rpmrc for redhat.com");
	}
    }
    require "build.pl";
    &do_build($mode, *args);
} elsif ($bigmode eq "uninstall") {
    require "uninstall.pl";
    &uninstallpackages($rpm{"force"}, $rpm{"test"}, $rpm{"nosub"}, @args);
} elsif ($bigmode eq "verify") {
    require "verify.pl";
    &doverify($querywhat, @args);
} elsif ($bigmode eq "install") {
    require "install.pl";
    &installpackages($rpm{"force"}, $rpm{"test"}, $rpm{"marker"}, 
			$rpm{"upgrade"}, @args);
} elsif ($bigmode eq "query") {
    require "query.pl";
    &doquery($querywhat, *querymode, @args);
} else {
    require "help.pl";
    &usage();
}

if (! $rpm{"keeptemps"}) {
    &remove_temps;
}
exit (0);
