eval '(exit $?0)' && eval '[ -f /usr/local/bin/perl ] && exec /usr/local/bin/perl -S $0 ${1+"$@"}; exec perl -S $0 ${1+"$@"};'
& eval 'if ( -f /usr/local/bin/perl ) exec /usr/local/bin/perl -S $0 $argv:q ; exec perl -S $0 $argv:q'
     if 0;

#  Gerald Pani
# 
# $Id: hgimake,v 1.10 1997/01/31 15:41:38 bheide Exp $
# $Log: hgimake,v $
# Revision 1.10  1997/01/31 15:41:38  bheide
# added support for BSDI
#
# Revision 1.9  1996/10/07 16:41:35  bheide
# changed HG_OSMaj/HG_OSMin to 2/0 for LINUX_ELF
#
# Revision 1.8  1996/07/25 14:56:57  bheide
#  o changed Linux section to support kernel versions 2.0.x and Linux ELF
#  o added -DFreeBSD to HGExtraCCDefines
#
# Revision 1.7  1996/04/10 12:25:31  bheide
# added () for split in FreeBSD definition
# changed Linux version handling
#
# Revision 1.6  1996/04/10 11:34:55  bheide
# added support for FreeBSD
#
# Revision 1.5  1996/02/06 08:05:04  hleitner
# *** empty log message ***
#
# Revision 1.4  1996/02/06 07:56:45  hleitner
# -DCOMPAT_43 added to -DHGExtraCCDefines; hope this will solve some
# troubles with new socket interface calls.
#
# Revision 1.3  1996/01/30 11:39:10  hleitner
# hgimake extended to support AIX 4.x.
#
# Revision 1.2  1995/11/27 14:39:00  gpani
# Initial Revision
#
# 
#  imake.pl
#

@args = @ARGV;

@defs = &machine();

$comm = join(' ', 'imake', @defs, @args);
print "$comm\n";
system($comm);

exit(0);
  
sub machine {
    local( $uname, $sysname, $release, @defs);
    $uname = &which('uname') || (&warning( "uname not found") && return ());
    
    chop( $sysname = `$uname -s`) || (&warning( "no sysinfo from uname -s") && return ());
    chop( $release = `$uname -r`) || (&warning( "no release from uname -r") && return ());
    
    if ($sysname =~ /^HP-UX$/) {
	# HPUX: mars, venus (HP-UX mars A.09.01 E 9000/720 64954132 8-user license)
	local($major, $minor) = ($release =~ /^A.([0-9]+)\.([0-9]+)/);
	if ($major != 9) {
	    &warning( "Major OS release must be 9 for HP-UX.");
	    return ();
	}
	if ($minor < 1) {
	    &warning( "OS release must be 9.01 for HP-UX.");
	    return ();
	}
	local($cpu) = $ENV{'CPU'};
	if (!defined($cpu) || !$cpu) {
	    &warning( "CPU environment variable not set");
	    exit(1);
	}
	if ($cpu eq 'HPUX9_GNU') {
	    return ("-DHPUX -D__GNUC__", 
		    "-DHGExtraCCDefines=\"-DHPUX -DSYSV -DHG_OSMaj=9 -DHG_OSMin=1\"");
	}
	elsif ($cpu eq 'HPUX9') {
	    return ("-DHPUX", 
		    "-DHGExtraCCDefines=\"-DHPUX -DSYSV -DHG_OSMaj=9 -DHG_OSMin=1\"");
	}	
	else {
	    &warning( "use either HPUX9 or HPUX9_GNU for the CPU environment variable");
	    exit(1);
	}
    }
    elsif ($sysname =~ /^SunOS$/) {
	# SUNX: ganymed, pluto
	local($major, $minor) = ($release =~ /^([0-9]+)\.([0-9]+)/);
	if ($major != 4 && $major != 5) {
	    &warning( "Major OS release must be 4 or 5 for SunOS.");
	    return ();
	}
	if ($major == 4) {
	    if ($minor != 1) {
		&warning( "OS release must be 4.1.x for SunOS4");
		return ();
	    }
	    # SunOS ganymed 4.1.3 1 sun4m
	    local($cpu) = $ENV{'CPU'};
	    if (!defined($cpu) || !$cpu) {
		&warning( "CPU environment variable not set");
		exit(1);
	    }
	    if ($cpu eq 'SUN4_GNU') {
		return ("-DSUN4 -D__GNUC__", 
			"-DHGExtraCCDefines=\"-DSUN -DBSD -DHG_OSMaj=4 -DHG_OSMin=1\"");
	    }
	    elsif ($cpu eq 'SUN4') {
		return ("-DSUN", 
			"-DHGExtraCCDefines=\"-DSUN -DBSD -DHG_OSMaj=4 -DHG_OSMin=1\"");
	    }	
	    else {
		&warning( "use either SUN4 or SUN4_GNU for the CPU environment variable");
		exit(1);
	    }
	}
	if ($major == 5) {
	    if ($minor < 3) {
		&warning( "OS release must be al least 5.3 for Solaris");
		return ();
	    }
	    # SunOS pluto 5.3 Generic_101674-01 sun4m sparc
	    return ("-DSOLARIS -DSUN -D__GNUC__", 
		    "-DHGExtraCCDefines=\"-DSUN -DSOLARIS -DSYSV -DSVR4 -DHG_OSMaj=$major -DHG_OSMin=$minor\"");
	}
    }
    elsif ($sysname =~ /^ULTRIX$/) {
	# PMAX: triton, titan (ULTRIX titan 4.4 0 RISC)
	local($major, $minor) = ($release =~ /^([0-9]+)\.([0-9]+)/);
	if ($major != 4) {
	    &warning( "Major OS release must be 4 for Ultrix.");
	    return ();
	}
	if ($minor < 4) {
	    &warning( "OS release must be 4.4 for Ultrix");
	    return ();
	}
	local($cpu) = $ENV{'CPU'};
	if (!defined($cpu) || !$cpu) {
	    &warning( "CPU environment variable not set");
	    exit(1);
	}
	if ($cpu eq 'PMAX_GNU') {
	    return ("-DULTRIX -D__GNUC__", 
		    "-DHGExtraCCDefines=\"-DULTRIX -DBSD -DHG_OSMaj=4 -DHG_OSMin=4\"");
	}
	elsif ($cpu eq 'PMAX') {
	    return ("-DULTRIX",
		    "-DHGExtraCCDefines=\"-DULTRIX -DBSD -DHG_OSMaj=4 -DHG_OSMin=4\"");
	}
	else {
	    &warning( "use either PMAX or PMAX_GNU for the CPU environment variable");
	    exit(1);
	}
    }
    elsif ($sysname =~ /^IRIX$/) {
	# SGI: uranus (IRIX fiicmsg01 5.[2-9] 02282013 IP12 mips)
	local($major, $minor) = ($release =~ /^([0-9]+)\.([0-9]+)/);
	if ($major != 5) {
	    &warning( "Major OS release must be 5 for Irix.");
	    return ();
	}
	if ($minor < 2) {
	    &warning( "OS release must be at least 5.2 for Irix");
	    return ();
	}
	local($cpu) = $ENV{'CPU'};
	if (!defined($cpu) || !$cpu) {
	    &warning( "CPU environment variable not set");
	    exit(1);
	}
	if ($cpu eq 'SGI_GNU') {
	    return ("-DIRIX -D__GNUC__",
		    "-DHGExtraCCDefines=\"-DIRIX -DSYSV -DSVR4 -DHG_OSMaj=5 -DHG_OSMin=$minor\"");
	}
	elsif ($cpu eq 'SGI') {
	    return ("-DIRIX",
		    "-DHGExtraCCDefines=\"-DIRIX -DSYSV -DSVR4 -DHG_OSMaj=5 -DHG_OSMin=$minor\"");
	}
	else {
	    &warning( "use either SGI_GNU or SGI for the CPU environment variable");
	    exit(1);
	}
    }
    elsif ($sysname =~ /^OSF1$/) {
	# ALPHA_OSF1: jupiter (OSF1 jupiter V3.2 214 alpha)
	local($major, $minor) = ($release =~ /^V([0-9]+)\.([0-9]+)/);
	if ($major < 2) {
	    &warning( "Major OS release must be at least 2 for OSF1.");
	    return ();
	}
# 	if ($minor != 1) {
# 	    &warning( "OS release must be 2.1 for OSF1");
# 	    return ();
# 	}
	local($cpu) = $ENV{'CPU'};
	if (!defined($cpu) || !$cpu) {
	    &warning( "CPU environment variable not set");
	    exit(1);
	}
	if ($cpu eq 'ALPHA_GNU') {
	    return ("-DOSF1 -D__GNUC__",
# At least GCC 2.6.3 defines BSD in his own param.h -- bheide230895
#		    "-DHGExtraCCDefines=\"-DOSF1 -DBSD -DHG_OSMaj=$major -DHG_OSMin=$minor\"");
		    "-DHGExtraCCDefines=\"-DOSF1 -DHG_OSMaj=$major -DHG_OSMin=$minor\"");
	}
	elsif ($cpu eq 'ALPHA_OSF1') {
	    return ("-DOSF1",
		    "-DHGExtraCCDefines=\"-DOSF1 -DBSD -DHG_OSMaj=$major -DHG_OSMin=$minor\"");
	}
	else {
	    &warning( "use either ALPHA_GNU or ALPHA_OSF1 for the CPU environment variable");
	    exit(1);
	}
    }
    elsif ($sysname =~ /^Linux$/) {
	# LINUX: fiicmpc21 (Linux fiicmpc21 1.2.11 #1 Wed Jun 28 21:52:03 MET DST 1995 i586)
	local($major, $minor) = ($release =~ /^([0-9]+)\.([0-9]+)/);
        local($cpu) = $ENV{'CPU'};
        if (!defined($cpu) || !$cpu) {
            &warning( "CPU environment variable not set");
            exit(1);
        }
	if ($major < 1) {
	    &warning( "Major OS release must be 1 for Linux (a.out).");
	    exit(1);
	}
	if ($major == 1 && $minor != 2) {
	    &warning( "only tested for kernel version 1.2.x");
	}
        if ($major == 2 && $minor != 0) {
	    &warning( "only tested for kernel version 2.0.x");
	}
        if ($cpu eq 'LINUX') {
	    return ("-DLINUX",
		    "-DHGExtraCCDefines=\"-DLINUX -DHG_OSMaj=1 -DHG_OSMin=2\"");
        }
        elsif ($cpu eq 'LINUX_OLD') {
	    return ("-DLINUX_OLD",
		    "-DHGExtraCCDefines=\"-DLINUX -DHG_OSMaj=1 -DHG_OSMin=2\"");
        }
        elsif ($cpu eq 'LINUX_ELF') {
	    return ("-DLINUX_ELF",
		    "-DHGExtraCCDefines=\"-DLINUX -DHG_OSMaj=2 -DHG_OSMin=0\"");
        }
        else {
            &warning( "use either LINUX_ELF, LINUX or LINUX_OLD for the CPU environment variable");
            exit(1);
        }
    }
    elsif ($sysname =~ /^AIX$/) {
	# AIX: fsmtiw01 AIX fsmtiw01 2 3 000029423700()
	local($major, $minor);
	$minor = $release;
	chop( $major = `$uname -v`) || (&warning( "no version from uname -v") && return ());
	if ($major < 3) {
	    &warning( "Major OS release must be at least 3 for AIX.");
	    return ();
	}
#	if ($minor != 2) {
#	    &warning( "OS release must be 3.2.x for AIX");
#	    return ();
#	}
	if ($major == 4) {
	    return ("-DAIX -DAIXV4 -D__GNUC__",
		    "-DHGExtraCCDefines=\"-DAIX -D_AIX -DAIXV4 -DCOMPAT_43 -DSYSV -DHG_OSMaj=$major -DHG_OSMin=$minor\"");
	}
	else {
	    return ("-DAIX -D__GNUC__",
		    "-DHGExtraCCDefines=\"-DAIX -DAIXV3 -DSYSV -DHG_OSMaj=$major -DHG_OSMin=$minor\"");
	}
    }
    elsif ($sysname =~ /^FreeBSD$/io) {
	# FreeBSD: tested on FreeBSD 2.1.0-RELEASE
	local($maj, $min, $rest) = split (/\./, $release, 3);
	local($cpu) = $ENV{'CPU'};
	if ($maj != 2) {
	    &warning("only tested on FreeBSD 2.1");
	    # but continue...
	}
	if (!$cpu) {
	    &warning("CPU environment variable not set");
	    exit(1);
	}
	elsif ($cpu eq 'FREEBSD') {
	    return("-DFreeBSD",
                   "-DHGExtraCCDefines=\"-DFreeBSD -DHG_OSMaj=$maj -DHG_OSMin=$min\"");
	}
	else {
	    &warning( "use FREEBSD for the CPU environment variable");
	    exit(1);
	}
    }
    elsif ($sysname =~ /^BSD\/OS$/) {
	# BSDI: BSD/OS hwpc02.hyperwave.com 2.1 BSDI BSD/OS 2.1 Kernel #1: Tue May 28 16:31:24 MET DST 1996     schneck@auryn.genua.de:/bsdos/2.1/sys-keyb/compile/GENERIC_DE  i386
	local($maj, $min, $rest) = split (/\./, $release, 3);
	local($cpu) = $ENV{'CPU'};
	if ($maj != 2) {
	    &warning("only tested on BSD/OS 2.1");
	    # but continue...
	}
	if (!$cpu) {
	    &warning("CPU environment variable not set");
	    exit(1);
	}
	elsif ($cpu eq 'BSDI') {
	    return("-DBSDI",
                   "-DHGExtraCCDefines=\"-DBSDI -DHG_OSMaj=$maj -DHG_OSMin=$min\"");
	}
	else {
	    &warning( "use BSDI for the CPU environment variable");
	    exit(1);
	}
    }
    &warning( "OS unknown");
    return ();
}

sub warning {
    local( $msg) = @_;
    print "********************************************************\n";
    print "WARNING: $msg\n";
    print "********************************************************\n";
    return 1;
}



sub which {
  local( $prog) = @_;
  local(@path) = split( /:/, $ENV{'PATH'});
  while(defined($p = shift(@path))) {
    $p .= '/' . $prog;
    return $p if (-x $p) && (-f $p);
  }
  return '';
}
