#!/usr/bin/perl
# fort77 (compiler driver) script for f2c
# For use with gcc under Linux
# This code is in the public domain; use at your own risk.
# Version: $Id: fort77,v 1.4 1994/11/11 15:28:08 ig25 Exp $
# Parse options

$nnflag = '-Nn802';

while ($_ = $ARGV[0], /^-/) {
    shift;

# First, the f2c options.

    if (/^-[CUuaEhPRrz]$/ || /^-I[24]$/ || /^-onetrip$/ || /^-![clPR]$/ ||
    	/^-ext$/ || /^-!bs$/ || /^-W[1-9][0-9]*$/ || /^-w8$/ || /^-w66$/ ||
	/^-r8$/ || /^-N[^n][0-9]+$/) {
	push (@fopts, $_);
    }
    elsif (/^-Nn[0-9]+$/) {
	$nnflag = $_;
    }

# Does somebody want to run the preprocessor?

    elsif (/^-cpp$/) {
	$cpp++;
    }

# These are common to both f2c and gcc
    elsif (/^-w$/) {
	push(@fopts, $_);
	push(@copts, $_);
    }

# This is for the linker, too...
   elsif (/^-g$/) {
	push(@fopts, $_);
	push(@copts, $_);
	push(@lopts, $_);
   }

# gcc only options

# too many -f and -W options to list them all...

   elsif (/^-[fWUAm]/ || /^-[Evx]$/ || /^-pipe$/ ) {
	push(@copts, $_);
   }
   elsif (/^-[oIVb]$/) {
	(@ARGV > 0) || die "Missing argument to \"$_\"\n";
	$output = shift;
   }
   elsif (/^-[oIVb]/) {
	$output = (/^-.(.*)/)[0];
   }

# Options for both C compiler and linker

    elsif (/^-[Og]/ || /^-p$/ || /^-pg$/) {
	push(@copts, $_);
	push(@lopts, $_);
    }

# Linker options

    elsif (/^-[lL]$/) {
	push(@lopts, $_);
	(@ARGV > 0) || die "Missing argument to \"$_\"\n";
	$_ = shift;
	push(@lopts, $_);
    }
    elsif (/^-[lL]./ || /^-nostartfiles$/ || /^-static$/ || /^-shared$/ ||
		/^-symbolic$/) {
	push(@lopts, $_);
    }
    elsif (/^-[cS]$/) {
	$compile_only = $_;
    }
    elsif (/^-D/) {
	push(@cppopts, $_);
    }
    else {
	die "Illegal option: $_\n";
    }
}

push(@fopts,$nnflag);
push(@copts,'-ffast-math');

foreach (@ARGV) {
    $ffile = undef;
    $cfile = undef;
    $lfile = undef;

    if (/^-[lL]$/) {
        push(@lopts, $_);
        (@ARGV > 0) || die "Missing argument to \"$_\"\n";
        shift;
        push(@lopts, $_);
	next;
    }
    elsif (/^-[lL]./) {
	push(@lopts, $_);
	next;
    }
    elsif (/\.[fF]$/) {
	$ffile = $_;
	$files ++;
    }
    elsif (/\.[cCisSm]$/ || /\.cc$/ || /\.cxx$/) {
	$cfile = $_;
	$files ++;
    }
    else {
	push(@lfiles, $_);
	$files ++;
    }
    if ($ffile) {
	$cfile = ($ffile =~ /(.*\/)?(.*\.)/)[1] . "c";
	if ($cpp || $ffile =~ /\.F$/) {
	    $retcode = system("/lib/cpp -traditional $ffile " . 
		join(' ',@cppopts) . " | f2c " .
		join(' ',@fopts). " > $cfile")/256;
	    }
	    else {
        	$retcode = system("f2c", @fopts, $ffile)/256;
	}
	if ($retcode) {
	    unlink $cfile;
	    die "$0: aborting compilation\n";
	}
    }
    if ($cfile) {
	@command = ("cc",@cppopts,@copts);
	if ($compile_only) {
            push(@command,$compile_only);
	}
	else {
	    push(@command,"-c");
	}
	if ($output && $compile_only) {
	    push(@command,"-o", $output);
	    $lfile = $output if $compile_only eq '-c';
	}
	elsif (($compile_only eq '-c') || (!$compile_only)) {
	    $lfile = ($cfile =~ /(.*\.)/)[0] . "o";
	}
	push(@command,$cfile);
	$retcode = system(@command)/256;
	if ($retcode) {
	    die "$0: aborting compilation\n";
	}
	unlink $cfile if $ffile;
	if ($lfile) {
	    push (@gener_lfiles, $lfile); push(@lfiles, $lfile);
	    $lfile = undef;
	}
    }
    push (@lfiles, $lfile) if $lfile;
}


die "No input files specified\n" unless $files;

exit if $compile_only;

push (@output, "-o", $output) if $output;
$retcode = system("cc", @output, @lfiles, @lopts, "-lf2c", "-lm" );
unlink (@gener_lfiles);
exit $retcode;
