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

#
#  ե˥͡ŬѤ
#
sub rename
{
	local($infile) = @_;
	local($outfile) = $infile.".new";
	local($line);

	open(INFILE, "< ".$infile) || die;
	open(OUTFILE, "> ".$outfile) || die;

	while ($line = <INFILE>) {
		$line =~ s/\b(_?)($syms)\b/$1_kernel_$2/gc;
		print OUTFILE $line;
	}

	close(INFILE);
	close(OUTFILE);

	if (`diff $infile $outfile`) {
		rename($infile, $infile.".bak");
		rename($outfile, $infile);
		print STDERR "Modified: ",$infile,"\n";
	}
	else {
		unlink($outfile);
	}
}

#
#  顼å
#
if ($#ARGV < 1) {
	die "Usage: rename <prefix> <filelist>\n";
}

#
#  
#
@syms = ();
$name = $ARGV[0];

#
#  ܥꥹȤɤ߹
#
$deffile = $name."_rename.def";
open(INFILE, $deffile) || die "Cannot open $deffile";
while ($line = <INFILE>) {
	chomp $line;
	if ($line ne "" && $line !~ /^#/) {
		@syms = (@syms, $line)
	}
	
}
close(INFILE);

$syms = join("|", @syms);
print STDERR $all_syms;

#
#  ե˥͡ŬѤ
#
shift @ARGV;
foreach $infile (@ARGV) {
	# եǤʤХå
	next unless (-f $infile);

	do rename($infile) if ($infile ne $deffile);
}
