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

require "getopt.pl";

#  ץ
#
#  -C <cpu>		ץå̾ɬܡ
#  -S <sys>		ƥ̾
#  -T <tool>		ȯĶ̾
#  -A <uname>		ץꥱץ̾
#  -U <utask>		¾Υץꥱץե
#			.o ե̾ǻꡥʣġ
#  -L <kernel_lib>	ͥ饤֥libkernel.aˤΥǥ쥯ȥ̾
#			ʾά硤ͥ饤֥ make 
#  -D <srcdir>		ͥΥ֤Ƥǥ쥯ȥ
#  -P <prefix>		ȯĶ󥹥ȡ PREFIX
#			ʸߤ PREFIX ȤäƤեϤʤ
#  -p <perl>		perl Υѥ̾Ū˻ꤹ
#  -l <lang>		ץߥ󥰸ʸǤC++Τߥݡȡ
#  -d <dir>		ƥץ졼ȥǥ쥯ȥʥǥեȤsample

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

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

$cpu = $opt_C;
$sys = $opt_S ? $opt_S : "";
$tool = $opt_T ? $opt_T : "";
$uname = $opt_A ? $opt_A : "sample1";
$utask = $opt_U ? $opt_U : "";
$kernel_lib = $opt_L ? $opt_L : "";
$prefix = $opt_P ? $opt_P : "/usr/local";
$perl = opt_p ? $opt_p : do get_path("perl", ("/usr/local/bin", "/usr/bin"));
$lang = $opt_l ? $opt_l : "";
$templatedir = $opt_d ? $opt_d : "sample";

#
#  ֥ȥե̾γĥҤ֤
#
sub get_objext {
	local(@uname);

	use POSIX;
	@uname = do uname();
	if ($uname[0] =~ /^cygwin/i) {
		return("exe");
	}
	else {
		return("");
	}
}

#
#  ץξ򸡺
#
sub get_path {
	local($progname, @pathlist) = @_;
	local($path);

	foreach $path (@pathlist) {
		if (-x $path."/".$progname) {
			return($path."/".$progname);
		}
	}
	return("");
}

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

	print STDERR "configure: Generating $outfile from $infile.\n";
	if (-f $outfile) {
		print STDERR "configure: $outfile exists.",
				"  Save as $outfile.bak.\n";
		rename($outfile, $outfile.".bak");
	}
	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.".".$cpu."-".$sys;
		if (-f $path) {
			do convert($path, $file);
			return;
		}

		$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);
	}	
}

#
#  ѿν
#

#
#  ǥ쥯ȥ̾Ф
#
$pwd = `pwd`; chop $pwd;
if ($opt_D) {
	$srcabsdir = $srcdir = $opt_D;
}
elsif ($0 =~ /(.*)\/configure/) {
	$srcdir = $1;
	if ($srcdir =~ /^\//) {
		$srcabsdir = $srcdir;
	}
	else {
		$srcabsdir = $pwd."/".$srcdir;
	}
}
else {
	$srcabsdir = $srcdir = $pwd;
}
$sampledir = $srcdir."/".$templatedir."/";

#
#  ѿơ֥κ
#
%vartable = ();
$vartable{"CPU"} = $cpu;
$vartable{"SYS"} = $sys;
$vartable{"TOOL"} = $tool;
$vartable{"UNAME"} = $uname;
$vartable{"UTASK"} = $utask;
$vartable{"KERNEL_LIB"} = $kernel_lib;
$vartable{"SRCDIR"} = $srcdir;
$vartable{"SRCABSDIR"} = $srcabsdir;
$vartable{"PREFIX"} = $prefix;
$vartable{"OBJEXT"} = do get_objext();
$vartable{"PERL"} = $perl;
$vartable{"LANG"} = $lang;

#
#  ǥ쥯ȥΥå
#

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

#
#  Makefile ȥץꥱե
#

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