#! /usr/bin/perl
#                              -*- Mode: Perl -*-
# make-kpkg ---
# Author           : root ( root@melkor.pilgrim.umass.edu )
# Created On       : Mon Jun 17 01:10:11 1996
# Created On Node  : melkor.pilgrim.umass.edu
# Last Modified By : root
# Last Modified On : Mon Jun 17 03:27:05 1996
# Last Machine Used: melkor.pilgrim.umass.edu
# Update Count     : 34
# Status           : Unknown, Use with caution!
# HISTORY          :
# Description      :
#
#
require 5.002;
use strict;
use Getopt::Long;

($main::MYNAME     = $main::0) =~ s|.*/||;
$main::Author      = "Manoj Srivastava";
$main::AuthorMail  = "srivasta\@pilgrim.umass.edu";
$main::Version     = "2.00";

my $help_opt=0;
my $revision=0;
my $signature="";
my $targ_opt=0;
my $noexec;

my %option_ctl = ("help"       => \$help_opt,
		  "noexec"     => \$noexec,
		  "revision=s" => \$revision,
		  "pgpsign=s"  => \$signature,
		  "targets"    => \$targ_opt);

my $usage = <<"EOUSAGE";

This program should be run in a linux kernel source top level directory.

usage: $main::MYNAME [options] target [target ...]
  where options are:
 --help                This message.
 --revision number     The debian revision number.
 --pgpsign  name       A ID used to sign the changes file using pgp.
 --targets             Lists the known targets.

Option Format:
The options may be shortened to the smallest unique string, and may
be entered with either a - or a -- prefix, and you may use a space
betweenan option string and a value.

Version: $main::Version
$main::Author <$main::AuthorMail>
EOUSAGE
;
my %Known_targets=("clean"          => 1,
		   "dist"           => 1,
		   "source"         => 2,
		   "diff"           => 2,
		   "binary"         => 2,
		   "kernel_source"  => 3,
		   "kernel_headers" => 3,
		   "kernel_image"   => 3,
		   "kernel-source"  => 3,
		   "kernel-headers" => 3,
		   "kernel-image"   => 3,
		   "build"          => 4,
		   "modules"        => 1,
		   "modules_image"  => 1,
		   "modules_config" => 1,
		   "modules-image"  => 1,
		   "modules-config" => 1
		  );

my $targets_help =<<EOTRGT;
 Known Targets are:
============================================================================
|     Targets                      |   Automatically builds                |
============================================================================
| clean                            |                                       |
| dist                             | Builds source,diff, and binary        |
|      source                      |                                       |
|      diff                        |                                       |
|      binary                      | Builds kernel_{source,headers,image}  |
|            kernel_source         |                                       |
|            kernel_headers        |                                       |
|            kernel_image          | Builds build                          |
|                           build  |                                       |
| modules                          |                                       |
| modules_image                    |                                       |
| modules_config                   |                                       |
============================================================================
See /usr/lib/kernel-package/debian/kernel.rules for details.
EOTRGT
  ;

sub main (){
  my $ret;
  my $rules_file;
  my $target;

  $ret = GetOptions(%option_ctl);
  die "$usage\n" unless $ret;
  if ($help_opt || $targ_opt){
    print "$usage" if $help_opt;
    print "$targets_help" if $targ_opt;
    exit 0;
  }
  # Fine. Now we check targets.
  my $errors = "";
  my $Targets = "";
  foreach $target (@ARGV){
    if ($Known_targets{$target}){
      $Targets .= "$target ";
    }
    else {
      $errors .= "$target ";
    }
  }
  if ($errors){
    print STDERR "Unknown target $errors\n";
    die "$targets_help\n";
  }
  # See if we are running in a linux kernel directory
  if (!(-d "drivers" && -d "kernel" && -d "fs" && -d "modules" && 
        -d "include/linux")){
    print STDERR <<EOERR;
We do not seem to be in a top level linux kernel source directory
tree. Since we are trying to make a kernel package, that does not make
sense.  Please change directory to an top level linux kernel source
directory, and try again. (If in case I am wrong, and this is indeed
a top level linux kernel source directory, then I have gotten sadly
out of date with current kernels, and you should upgrade kernel-package)
EOERR
  ;
    exit 1;
  }
  if (-x "debian/kernel.rules"){
    $rules_file = "debian/kernel.rules";
  }
  elsif (-x "/usr/lib/kernel-package/debian/kernel.rules"){
    $rules_file = "/usr/lib/kernel-package/debian/kernel.rules";
  }
  else {
    print STDERR <<EOERR1;
Could not locate the rules file in either debian/kernel.rules or
/usr/lib/kernel-package/debian/kernel.rules, which would seem an 
impossibility. I give up.
EOERR1
  ;
  }
  # Fine. Just do it!
  my $command = "$rules_file ";
  if ($signature){
    $command .= " PGP_SIGNATURE=$signature ";
  } 
  if ($revision){
    $command .= "DEBIAN_REVISION=$revision";
  }
  if ($noexec){
    $command .= " -n ";
  }
  $command .= " $Targets";
  exec $command; 
}
&main();

exit 0;
__END__
