#!c:\perl\perl.exe -s
# (c) 1996 Reini Urban. All rights reserved
#
# Translate AutoLISP function names (from german to english)
# from the dbm file l:/dbtrans.db, which will be created from a simple
# SDF text file. (sample included)
#
# Translates in place, accepts more arguments, saves old as .bak
# Translates all occurances of C:<old-name> to C:<new-name> and
# "<old-name>" to "<new-name>"
#
# Usage: perl -s translat.pl [ -create | -print | -v ] filename.lsp
# Options:
#	-create : recreates the database ignoring same and special chars
#	-print  : prints the database
#	-v      : verbose, prints number of each substitution
#-----------------------------------------------------------------------

$version="1.0";

#create DBTRANS database
if ($create) {
	open (DBTXT, "L:/DBTRANS.TXT");
	unlink "L:/DBTRANS.DB";
	dbmopen (trans,"L:/DBTRANS",0666);
	while (<DBTXT>) {
		#(@trans) = split (' ');
		m/(\S+)\s+(\S+)/;
		$trans{$1} = $2 if ((! m/\+\-\*/) && ($1 ne $2));
	}
	dbmclose (trans);
	print STDERR "\nL:\DBTRANS.DB created out of L:\DBTRANS.TXT";
	exit;
}

if ($print) {
	dbmopen (trans,"L:/DBTRANS",0666);
	print STDERR "\nContents of L:\DBTRANS.DB";
	foreach $key (keys %trans)
	  { printf "\n%10s = %s", $key, $trans{$key}; }
	dbmclose (trans);
	exit;
}

$/ = ();
dbmopen (trans,"L:/DBTRANS",0666);
while (<>) {
	if ($oldargv ne $ARGV) {
		$bak =~ s/\..+/\.bak/;
		rename ($ARGV, $bak);
		open (ARGVOUT, ">$ARGV");
		select (ARGVOUT);
		$oldargv = $ARGV;
	}
	foreach $key (keys %trans)
	  {	$num{$key} += s/C:$key/C:$trans{$key}/gi;	# C:function
	  	$num{$key} += s/"$key"/"$trans{$key}"/gi;   # "function"
	  	$num{$key} += s/\^C\^C$key/\^C\^C$trans{$key}/gi;   # MENU
	  	$i += $num{$key};}
}
continue {
	print;
}

select (STDOUT);

if ($i) {
	printf STDERR "\n%d substitutions made.", $i;
	if ($v) {
	foreach $key (keys %num) {
		printf STDERR ("\n %10s %3d-> %s",$key, $num{$key}, $trans{$key})
			if $num{$key};
	}}
}
else {
	print STDERR "\nno function translated.";
}
dbmclose (trans);

