#!/usr/athena/bin/perl
#
# Build "all" the the RedHat-Athena RPMS using 'makepackage'.  This
# will walk through the package-list file and see which packages need
# to be rebuilt due to changes in package list.
#
# Created by:	Derek Atkins <warlord@MIT.EDU>
#
# $Author: warlord $
# $Id: build.pl,v 1.5 1999/02/13 21:10:56 warlord Exp $
#

$pkglist = "/mit/linux/packages/package-lists";
$makepkg = "/mit/linux/bin/makepackage";
$srvd_buildroot = "/tmp/buildroot";

# This variable disallows rebuilding of packages!
#$nobuild = 1;

close (STDERR);
open (STDERR, ">/dev/null");

#
# ListPkgs (pkglist)
# 
# Returns an array of all the packages in the file pkglist, assuming the
# 'standard' package-list format (looks for '%newpackage' and then grabs
# the next line)
#
sub ListPkgs {
    local($file) = @_;
    local(@pkgs) = ();

    open (F, "<$file") || die "ListPkgs: Cannot open $file";
    while (<F>) {
	chop;
	next unless m/^%newpackage$/;
	$_ = <F>;
	chop $_;
	($_) = split (/\s/) if (m/\s/);
	push (@pkgs, $_);
    }
    close (F);
    return (@pkgs);
}

#
# TestPkg (pkg)
#
# Tests pkg to see if package-list is different than the SPECfile
# Uses global variable $rhversion
#
# Returns 0 if nothing needs to be changed, !0 otherwise
#
sub TestPkg {
    local ($pkg) = @_;
    local (@args) = ("-v");

    push (@args, "-c", $rhversion) if ($rhversion);
    push (@args, $pkg);
    return (system ($makepkg, @args));
}

#
# BuildPkg (pkg)
#
# Builds a new package
#
sub BuildPkg (pkg) {
    local ($pkg) = @_;
    local (@args) = ();

    print "Rebuilding $pkg\n";

    return 0 if ($nobuild);

    push (@args, "-c", $rhversion) if ($rhversion);
    push (@args, "-s", $pkg);
    return (system ($makepkg, @args));
}


#
# Usage (whoami, verbose)
#
# Print out a usage message and then exit.  Print a more verbose
# help message if $verbose == 1
#
sub Usage {
    local ($whoami, $verbose) = @_;
    print "Usage:\n\t$whoami -h{elp} | --h{elp}\n";
    print "\t$whoami [ -n ] [ -c current ]\n";
    if ($verbose == 1) {
	print "\n\t-n\tWalk through the steps without actually rebuilding\n";
	print "\t\tanything.  Notify the user what would have happened.\n";
	print "\t-c current\tuse <current> as the current RedHat version\n";
	print "\t\tnumber, otherwise it will use the current directory\n";
	print "\t\tas linked.\n";
	print "\n\t-h{elp}\n\t--h{elp}\tPrint out this help message\n";
    }

    exit (!$verbose);
}

$whoami = $0;
undef $grabarg;
undef $rhversion;
$noact = 0;

# Parse @ARGV
foreach (@ARGV) {
    if ($grabarg) {
	${$grabarg} = $_;
        undef $grabarg;
        next;
    }
    &Usage($whoami, 1) if (m/^-h/ || m/^--h/); 
    if (m/^-n$/) {
	$noact = 1;
	next;
    }
    if (m/^-c$/) {
	$grabarg = "rhversion";
	next;
    }
    &Usage($whoami, 0);
}
&Usage($whoami, 0) if ($grabarg);

# Verify the srvd buildroot directory exists
if ($noact == 0) {
    print "Checking $srvd_buildroot...";
    opendir (FOO, $srvd_buildroot) || die
    	"Cannot find the SRVD Buildroot, $srvd_buildroot";
    closedir (FOO);
    print "ok\n";
}

print "Verifying packages in ";
if ($rhversion) {
    print "$rhversion\n";
} else {
    print "current\n";
}

foreach (&ListPkgs($pkglist)) {
    if (&TestPkg($_)) {
	if ($noact > 0) {
	    print "I would rebuild $_\n";
	} else {
	    print "Running buildpackage\n";
	    &BuildPkg ($_);
	}
    }
}
