#! /usr/bin/perl
#
#  TOPPERS/JSP Kernel
#      Toyohashi Open Platform for Embedded Real-Time Systems/
#      Just Standard Profile Kernel
# 
#  Copyright (C) 2001 by Embedded and Real-Time Systems Laboratory
#                              Toyohashi Univ. of Technology, JAPAN
# 
#  嵭ԤϡFree Software Foundation ˤäƸɽƤ 
#  GNU General Public License  Version 2 ˵ҤƤ狼
#  ξΤ줫˸¤ꡤܥեȥܥեȥ
#  ѤΤޤࡥʲƱˤѡʣѡۡʰʲ
#  ѤȸƤ֡ˤ뤳Ȥ̵ǵ롥
#  (1) ܥեȥ򥽡ɤηѤˤϡ嵭
#      ɽѾ浪Ӳ̵ݾڵ꤬Τޤޤηǥ
#      ˴ޤޤƤ뤳ȡ
#  (2) ܥեȥѲǽʥХʥꥳɡʥ֥륪
#      ȥե饤֥ʤɡˤηѤˤϡ
#      ȼɥȡѼԥޥ˥奢ʤɡˤˡ嵭ɽ
#      Ѿ浪Ӳ̵ݾڵǺܤ뤳ȡ
#  (3) ܥեȥԲǽʥХʥꥳɤηޤϵ
#      ߹ѤˤϡΤ줫ξȡ
#    (a) ѤȼɥȡѼԥޥ˥奢ʤɡˤˡ嵭
#        ɽѾ浪Ӳ̵ݾڵǺܤ뤳ȡ
#    (b) Ѥη֤̤ˡˤäơ嵭Ԥ𤹤
#        ȡ
#  (4) ܥեȥѤˤľŪޤϴŪ뤤ʤ»
#      ⡤嵭Ԥդ뤳ȡ
# 
#  ܥեȥϡ̵ݾڤ󶡤ƤΤǤ롥嵭Ԥϡ
#  ܥեȥ˴ؤơŬѲǽޤơʤݾڤԤ
#  ʤޤܥեȥѤˤľŪޤϴŪ
#  ʤ»˴ؤƤ⡤Ǥʤ
# 
#  @(#) $Id: configure,v 1.6 2001/10/22 01:46:59 hiro Exp $
# 

require "getopt.pl";

#  ץ
#
#  -C <cpu>		ץå̾ɬܡ
#  -S <sys>		ƥ̾
#  -A <uname>		ץꥱץ̾
#  -U <utask>		¾Υץꥱץե
#			.o ե̾ǻꡥʣġ
#  -P <prefix>		ȯĶ󥹥ȡ PREFIX
#  -m			ե졼 make Ԥʤ
#  -d			ͥ make depend Ԥ

#  (1)
#
#  % ../configure -C m68k -S dve68k -A test1 -U test_support.o -P /usr/local
#
#  (2)
#
#  % ../configure -C linux
#	ץꥱץ sample1PREFIX  /usr/local ˤʤ롥

#
#  ץν
#
do Getopt("CSAUP");
unless ($opt_C) {
	print STDERR "configure: -C option is mandatory\n";
	exit(1);
}

$cpu = $opt_C;
$sys = $opt_S ? $opt_S : "";
$uname = $opt_A ? $opt_A : "sample1";
$utask = $opt_U ? $opt_U : "";
$prefix = $opt_P ? $opt_P : "/usr/local";

#
#  եѴ
#
sub convert {
	local($infile, $outfile) = @_;
	local($line, $varname, $varval);

	print STDERR "configure: Generating $outfile from $infile.\n";
	unless (open(INFILE, $infile)) {
		print STDERR "configure: can't open $infile\n";
		exit(1);
	}
	unless (open(OUTFILE, "> ".$outfile)) {
		print STDERR "configure: can't open $outfile\n";
		exit(1);
	}

	while ($line = <INFILE>) {
		chop $line;
		while ($line =~ /^(.*)\@\(([A-Za-z_]+)\)(.*)$/) {
			$line = $1.$vartable{$2}.$3;
		}
		print OUTFILE $line,"\n";
	}

	close(INFILE);
	close(OUTFILE);
}

#
#  ץ򸫤Ĥƥե
#
sub generate {
	local($file, $mandatory) = @_;
	local($path);

	if ($sys) {
		$path = $sampledir.$file.".".$sys;
		if (-f $path) {
			do convert($path, $file);
			return;
		}
	}	

	$path = $sampledir.$file.".".$cpu;
	if (-f $path) {
		do convert($path, $file);
		return;
	}	

	$path = $sampledir.$file;
	if ($mandatory || -f $path) {
		do convert($path, $file);
	}	
}

#
#  ѿν
#

%vartable = ();
$vartable{"CPU"} = $cpu;
$vartable{"SYS"} = $sys;
$vartable{"UNAME"} = $uname;
$vartable{"UTASK"} = $utask;
$vartable{"PREFIX"} = $prefix;

$pwd = `pwd`; chop $pwd;
if ($0 =~ /(.*)\/configure/) {
	$jspdir = $1;
	if ($jspdir =~ /^\//) {
		$jspabsdir = $jspdir;
	}
	else {
		$jspabsdir = $pwd."/".$jspdir;
	}
}
else {
	$jspabsdir = $jspdir = $pwd;
}
$vartable{"JSPDIR"} = $jspdir;
$vartable{"JSPABSDIR"} = $jspabsdir;
$sampledir = $jspdir."/sample/";

#
#  եΥå
#

if (! -d $jspdir."/config/".$cpu) {
	print STDERR "configure: $jspdir/config/$cpu not exist\n";
	exit(1);
}
if ($sys && ! -d $jspdir."/config/".$cpu."/".$sys) {
	print STDERR "configure: $jspdir/config/$cpu/$sys not exist\n";
	exit(1);
}

#
#  Makefile ȥץꥱե
#

do generate("Makefile", 1);
do generate($uname.".c", 0);
do generate($uname.".h", 0);
do generate($uname.".cfg", 0);

#
#  ե졼 Makefile  make
#

if ($sys) {
	$makefile = "Makefile.".$cpu."-".$sys;
}
else {
	$makefile = "Makefile.".$cpu;
}
do convert($jspdir."/cfg/Makefile.template", $jspdir."/cfg/$makefile");

unless ($opt_m) {
	print STDERR "configure: Generating configuration tools.\n";
	system("cd $jspdir/cfg; make -f $makefile");
}

#
#  ¸طե
#

if ($opt_d) {
	print STDERR "configure: Generating a dependency file.\n";
	system("make depend");
}
