#!/usr/bin/perl

###############################################################################
###############################################################################
##
##  Copyright (C) Sistina Software, Inc.  1997-2003  All rights reserved.
##  Copyright (C) 2004 Red Hat, Inc.  All rights reserved.
##  
##  This copyrighted material is made available to anyone wishing to use,
##  modify, copy, or redistribute it subject to the terms and conditions
##  of the GNU General Public License v.2.
##
###############################################################################
###############################################################################

use Getopt::Long;

print "\nConfiguring Makefiles for your system...\n";

# Set a bunch of variables
$CC = gcc;

$ret = 0;

%options = (
	help => \$help,
        incdir => \$incdir,
	kernel_src => \$kernel_src,
	libdir => \$libdir,
	mandir  => \$mandir,
	prefix => \$prefix,
	sbindir => \$sbindir,
	verbose => \$verbose
);

$err = &GetOptions (\%options,
		    'help',
		    'incdir=s',
		    'kernel_src=s',
		    'libdir=s',
		    'mandir=s',
		    'prefix=s',
		    'sbindir=s',
		    'sharedir=s',
		    'verbose');

if(!$err) {
  $ret = 1;
  print "*** ERROR: Invalid option detected ***\n";
}

# Check for the --help flag
if ($help || !$err) {
  $_ = $0;
  s/.*\.\/(.*)/\1/;
  print "Usage: $_ [flags]\n";
  print "--help\t\tPrints this usage information\n\n";
  print "install flags:\n";
  print "--incdir=\tthe base directory for include files.  (Default: /usr/include)\n";
  print "--kernel_src=\tthe directory containing the kernel source you wish to\n\t\tcompile against. (Default: system dependant)\n";
  print "--libdir=\tthe base directory for libraries.  (Default: /usr/lib)\n";
  print "--mandir=\tthe base directory for man pages.  (Default: /usr/share/man)\n";
  print "--prefix=\tthe base directory to install into.  (Default: /)\n";
  print "--sbindir=\tthe base directory for system binaries.  (Default: /sbin)\n";
  print "--sharedir=\tthe base directory for misc cluster files.  (Default: /usr/share/cluster)\n";
  print "--verbose\t\tVerbose output\n";
  exit $ret;
}

verbose("------------------------------------------------");

$pwd = `pwd`;
chomp($pwd);

if (!$incdir) {
  $incdir="${prefix}/usr/include";
}
if (!$libdir) {
  $libdir="${prefix}/usr/lib";
}
if (!$mandir) {
  $mandir="${prefix}/usr/share/man";
}
if (!$sbindir) {
  $sbindir="${prefix}/sbin";
}
if (!$sharedir) {
  $sharedir="${prefix}/usr/share/cluster";
}


open IFILE, "<make/defines.mk.input" or die "Can't redirect stdin";
open OFILE, ">make/defines.mk" or die "Can't redirect stdout";

print OFILE "# This file was generated by configure from defines.mk.input\n";

while (<IFILE>) {
  chomp;
  $_ =~ s/\@CC\@/${CC}/;
  $_ =~ s/\@INCDIR\@/$incdir/;
  $_ =~ s/\@KERNEL_SRC\@/$kernel_src/;
  $_ =~ s/\@LIBDIR\@/$libdir/;
  $_ =~ s/\@MANDIR\@/$mandir/;
  $_ =~ s/\@SBINDIR\@/$sbindir/;
  $_ =~ s/\@SHAREDIR\@/$sharedir/;
  print OFILE "$_\n";
}

close IFILE;
open IFILE, "<make/release.mk.input" or die "Can't redirect stdin";
while (<IFILE>) {
  chomp;
  if($_ =~ /RELEASE_MAJOR\s=\s(.*)/){
      $release_major = $1;
  } elsif($_ =~ /RELEASE_MINOR\s=\s(.*)/){
      $release_minor = $1;
  }
}

if($release_minor =~ m/DATE/i){
    $release_minor = `date +%s`;
    chomp $release_minor;
}

print OFILE "RELEASE_MAJOR = $release_major\n";
print OFILE "RELEASE_MINOR = $release_minor\n";
print OFILE "RELEASE = $release_major.$release_minor\n";
close OFILE;

verbose("--------------------------------");
print "Completed Makefile configuration\n\n";

sub find_tool($tool_name) 
{
    my($tool_name) = shift(@_);
    preverbose("Checking for the presence of $tool_name");
    open SV, "scripts/find_executable $tool_name |";
    if (<SV>) {
	print "\n'$tool_name' not found.  Make sure you have '$tool_name' installed and in your path.\n";
	die "Configure error: Can't find '$tool_name'.";
    }
    postverbose("found $tool_name");
}

sub preverbose($msg) {
  if($verbose) {
    $msg = shift(@_);
    print "$msg...";
  }
}

sub verbose($msg) {
  if($verbose) {
    $msg = shift(@_);
    print "$msg\n";
  }
}

sub postverbose($msg) {
  if($verbose) {
    $msg = shift(@_);
    print "$msg\n";
  }
}


