#!/usr/local/links/bin/perl

use strict;		#safer mode of programming

use vars	'$name',	# system name
		'@names',	# list of system names
	 	'@cc',		# the CC definition
	 	'@ff',		# the FF definition
	 	'@cflags',	# the C flags
		'@fflags',	# the F flags
		'@ld',		# the linker
		'@ldflags',	# the load flags
		'@ldlibs',	# the load libs
		'$line',	# each line of the existing makefile
		'$newfile',	# fill up with the new makefile
		'@words';	# array to hold split words of a line

my ($i, $j);			# for loop counters

# CVS info 
# $Date: 2005/01/10 21:05:27 $
# $Revision: 1.2 $
# $RCSfile: config,v $
# $Name: rel_5 $

#read file in for config information
open (FILE, "config.txt");

#get configuration information for each system from config.txt, line by line
$i = -1;
while($line = <FILE>){

	#split each line into words
	@words = split /\s+/, $line;

	if($words[0] eq "NAME:"){
		$i++;
		$names[$i] = $words[1];
	}
	elsif($words[0] eq "CC:"){
		$cc[$i] = $words[1];
	}
	elsif($words[0] eq "CFLAGS:"){
	   for($j = 1; $j < @words; $j++){
		$cflags[$i] .= "$words[$j] ";
	   }
	}
	elsif($words[0] eq "FF:"){
		$ff[$i] = $words[1];
	}
	elsif($words[0] eq "FFLAGS:"){
	   for($j = 1; $j < @words; $j++){
		$fflags[$i] .= "$words[$j] ";
	   }
	}
	elsif($words[0] eq "LD:"){
		$ld[$i] = $words[1];
	}
	elsif($words[0] eq "LDFLAGS:"){
	   for($j = 1; $j < @words; $j++){
		$ldflags[$i] .= "$words[$j] ";
	   }
	}
	elsif($words[0] eq "LDLIBS:"){
	   for($j = 1; $j < @words; $j++){
		$ldlibs[$i] .= "$words[$j] ";
	   }
	}
}

close (FILE);

# arg = system name to build makefile for
$name = $ARGV[0];

#search for name from the filed list of names
my $match = -1;
for($i=0; $i<@names; $i++){
	if($names[$i] =~ /$name/i){
		$match = $i;	
	}
}

#if there is not a name match, print error information
if(($name eq "") || ($match == -1)){
	print "Please specify a system name.\n";
	print "Choose from:\n"; 
	for($i=0; $i<@names; $i++){
		print "    $names[$i]\n";
	}
}

#otherwise, print information about chosen system
else{ 
	print "You have chosen system $names[$match]\n";
	print "CC: $cc[$match]\n";
	print "CFLAGS: $cflags[$match]\n";
	print "FF: $ff[$match]\n";
	print "FFLAGS: $fflags[$match]\n";
	print "LD: $ld[$match]\n";
	print "LDFLAGS: $ldflags[$match]\n";
	print "LDLIBS: $ldlibs[$match]\n";
}

#begin reprinting makefile
open (MAKE, "makefile");

#replace the first 2 lines with lines for specified system
$line = <MAKE>;
$line = <MAKE>;
$newfile .= "# This makefile is configured to run on $names[$match]. \n" ;
$newfile .= "# To configure for another sysem, please run ./config.\n";

#if a line begins with modifiable information, change it
#othewise, just reprint it the same as before
#keep concatenating into $newfile
while ($line = <MAKE>){

	if ($line =~ /^\s*CC\s*=/) {
      		$newfile .= "CC       = $cc[$match]\n";
      		next;
    	}
    	if ($line =~ /^\s*CFLAGS\s*=/) {
      		$newfile .= "CFLAGS   = $cflags[$match]\n";
      		next;
    	}
    	if ($line =~ /^\s*FF\s*=/) {
      		$newfile .= "FF       = $ff[$match]\n";
      		next;
    	}
    	if ($line =~ /^\s*FFLAGS\s*=/) {
      		$newfile .= "FFLAGS   = $fflags[$match]\n";
      		next;
    	}
    	if ($line =~ /^\s*LD\s*=/) {
      		$newfile .= "LD       = $ld[$match]\n";
      		next;
    	}
    	if ($line =~ /^\s*LDFLAGS\s*=/) {
      		$newfile .= "LDFLAGS  = $ldflags[$match]\n";
      		next;
    	}
    	if ($line =~ /^\s*LDLIBS\s*=/) {
      		$newfile .= "LDLIBS   = $ldlibs[$match]\n";
      		next;
    	}
	# otherwise
	$newfile .= $line;
}

close MAKE;			# consumed all of old makefile

open MAKEFILE, ">makefile";
print MAKEFILE $newfile;	# new makefile
close MAKEFILE;