#! /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
# 
#  L쌠҂́Cȉ (1)`(4) ̏CFree Software Foundation 
#  ɂČ\Ă GNU General Public License  Version 2 ɋL
#  qĂ𖞂ꍇɌC{\tgEFAi{\tgEFA
#  ς̂܂ށDȉjgpEEρEĔzziȉC
#  pƌĂԁj邱Ƃ𖳏ŋD
#  (1) {\tgEFA\[XR[ȟ`ŗpꍇɂ́CL̒
#      \C̗pщL̖ۏ؋K肪Ĉ܂܂̌`Ń\[
#      XR[hɊ܂܂Ă邱ƁD
#  (2) {\tgEFACCu`ȂǁC̃\tgEFAJɎg
#      pł`ōĔzzꍇɂ́CĔzzɔhLgip
#      ҃}jAȂǁjɁCL̒쌠\C̗pщL
#      ̖ۏ؋Kfڂ邱ƁD
#  (3) {\tgEFAC@ɑgݍނȂǁC̃\tgEFAJɎg
#      płȂ`ōĔzzꍇɂ́Ĉꂩ̏𖞂
#      ƁD
#    (a) ĔzzɔhLgip҃}jAȂǁjɁCL̒
#        쌠\C̗pщL̖ۏ؋Kfڂ邱ƁD
#    (b) Ĕzž`ԂCʂɒ߂@ɂāCTOPPERSvWFNg
#        񍐂邱ƁD
#  (4) {\tgEFA̗pɂ蒼ړI܂͊ԐړIɐ邢Ȃ鑹
#      QCL쌠҂TOPPERSvWFNgƐӂ邱ƁD
# 
#  {\tgEFÁCۏ؂Œ񋟂Ă̂łDL쌠҂
#  TOPPERSvWFNǵC{\tgEFAɊւāC̓Kp\
#  ܂߂āCȂۏ؂sȂD܂C{\tgEFA̗pɂ蒼
#  ړI܂͊ԐړIɐȂ鑹QɊւĂC̐ӔC𕉂ȂD
# 
#  @(#) $Id: configure,v 1.1 2008/06/17 00:04:58 suikan Exp $
# 

require "getopt.pl";

#  IvV̒`
#
#  -C <cpu>		vZbTiK{j
#  -S <sys>		VXe
#  -T <tool>		J
#  -A <uname>		AvP[VvO
#  -U <utask>		̃AvP[VvOt@C
#			i.o t@CŎwDwj
#  -L <kernel_lib>	J[lCuilibkernel.aj̃fBNg
#			iȗꍇCJ[lCu make j
#  -D <srcdir>		J[l̃\[X̒uĂfBNg
#  -P <prefix>		JCXg[ PREFIX
#			i݂ PREFIX gĂt@C͂Ȃj
#  -p <perl>		perl ̃pXiIɎw肷ꍇj
#  -l <lang>		vO~Oi_łC++̂݃T|[gj
#  -d <dir>		ev[gfBNgwiftHgsamplej

#  gp(1)
#
#  % ../configure -C m68k -S dve68k -A test1 -U test_support.o -L .
#
#  gp(2)
#
#  % ../configure -C linux
#	AvP[VvO sample1 ɂȂD

#
#  IvV̏
#
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";

#
#  IuWFNgt@C̊gqԂ
#
sub get_objext {
	local(@uname);

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

#
#  vȌꏊ
#
sub get_path {
	local($progname, @pathlist) = @_;
	local($path);

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

#
#  t@Cϊ
#
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);
}

#
#  Tvăt@C𐶐
#
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);
	}	
}

#
#  ϐ̏
#

#
#  \[XfBNgo
#
$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."/";

#
#  ϐe[u̍쐬
#
%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;

#
#  fBNg̃`FbN
#

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 ƃAvP[Vt@C̐
#

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