#! /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 : Manoj Srivastava
# Last Modified On : Tue Nov  5 23:18:13 1996
# Last Machine Used: tiamat.datasync.com
# Update Count     : 46
# 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\@debian.org";
$main::Version     = "3.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,
		   "buildpackage"   => 1,
		   "binary"         => 1,
		   "binary-indep"   => 2,
		   "kernel_source"  => 3,
		   "kernel_headers" => 3,
		   "kernel-source"  => 3,
		   "kernel-headers" => 3,
		   "binary-arch"    => 2,
		   "kernel_image"   => 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                            |                                       |
| buildpackage                     | Builds the whole paclage              |
| binary                           | Builds kernel_{source,headers,image}  |
|       binary-indep               |                                       |
|            kernel_source         |                                       |
|            kernel_headers        |                                       |
|       binary-arch                |                                       |
|            kernel_image          | Builds build                          |
|                           build  |                                       |
| modules                          |                                       |
| modules_image                    |                                       |
| modules_config                   |                                       |
============================================================================
See /usr/lib/kernel-package/rules for details.
EOTRGT
  ;

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

  $rules_file = "/usr/lib/kernel-package/rules";
  
  $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 "$rules_file"){
    print STDERR <<EOERR1;
    Could not locate the rules file (Normally found at the location
 $rules_file), 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; 
}

## Now just call main
&main();

exit 0;
__END__
