#!/usr/local/bin/perl

$debug = 0;

$logsSubsystem = "MFE";  # The Subsystem that contains the logging stuff

while ($arg = $ARGV[0]) {
    last if ! ($arg =~ /^-/);
    $hFile = (shift @ARGV, shift @ARGV) if $arg eq "-hfile";
    $mFile = (shift @ARGV, shift @ARGV) if $arg eq "-mfile";
    $localizationFile = (shift @ARGV, shift @ARGV) if $arg eq "-localization";
    push(@INC, (shift @ARGV, shift @ARGV)) if $arg eq "-include_dir";
}

$localizationBase = $localizationFile;
$localizationBase =~ s/.localization//;
$hFileName = "$localizationBase.h";
$mFileName = "$localizationBase.m";


push(@INC, "$ENV{OMNIMAKE_ROOT}/utilities");
$debug && print STDERR join(":", @INC) . "\n";

require "arch.perl";

$tmpDir = &temporaryFileDirectory();
$tmpHFile = "$tmpDir/$hFileName." . $$;
$tmpMFile = "$tmpDir/$mFileName." . $$;

if ($ENV{OMNIMAKE_INSTALL_ROOT}) {
    $toolsRoot = "$ENV{OMNIMAKE_INSTALL_ROOT}/Tools";
} else {
    $toolsRoot = "$ENV{OMNIMAKE_ROOT}/Tools";
}

# Setup projectInfo
$projectInfo = "$toolsRoot/projectInfo" . $executableExtension;
-x $projectInfo || &fatal("Cannot find '$projectInfo'!\n");

# Setup dictionaryKeys
$dictionaryKeys = "$toolsRoot/dictionaryKeys" . $executableExtension;
-x $dictionaryKeys || &fatal("Cannot find '$dictionaryKeys'!\n");

# Get the subsystem name
$subsystemName = `$projectInfo PROJECTNAME`;
chop($subsystemName);
$debug && print STDERR "subsystemName = $subsystemName\n";

# Get the project type
$projectType = `$projectInfo PROJECTTYPE`;
chop($projectType);
$debug && print STDERR "projectType = $projectType\n"; 


#  Set how to define an extern symbol
if ($projectType eq "Framework") {
	$externDecl = $subsystemName . "_EXTERN";
} else {
	$externDecl = "extern";
}
$debug && print STDERR "externDecl = $externDecl\n";

#
#  Build the new files into $tmpDir
#

$autogenerated_warning =
"/*****************************************************/
/***** Don't edit this file; it is autogenerated. ****/
/*****************************************************/
";

open (HFILE, ">$tmpHFile") || die "$!: $tmpHFile\n";
open (MFILE, ">$tmpMFile") || die "$!: $tmpMFile\n";

print MFILE $autogenerated_warning;
print MFILE "#import \"$hFileName\"\n";

print HFILE $autogenerated_warning;

$debug && print STDERR "Subsystem = $subsystemName\n";

print HFILE <<EOF;
#if defined(__cplusplus)
extern "Objective-C" {
#endif
EOF

print HFILE "#import <$logsSubsystem/MFELogs.h>\n";
if ($projectType eq "Framework") {
	print HFILE "#import <$subsystemName/FrameworkDefines.h>\n";
}

print HFILE <<EOF;
#if defined(__cplusplus)
}
#endif
EOF


#
#  Touch the output files to ensure that they exist since they might be one of the input
#  arguments.
#
&touchIfAbsent($hFile);
&touchIfAbsent($mFile);

# Allow multi-line regular expression matching
$* = 1;

$lastFile = "";

open(TAGS, "$dictionaryKeys $localizationFile|") || &fatal("Error executing $dictionaryKeys\n");

while ($tag = <TAGS>) {
	chop $tag;

	# Print definitions and declarations for the tag. 
	print HFILE $externDecl . " NSString *$tag;\n";
	print MFILE "NSString *$tag = @\"$tag\";\n";
}

close(HFILE);
close(MFILE);

# Move the temp files over the real files...
&moveFile($tmpHFile, $hFile);
&moveFile($tmpMFile, $mFile);
