#!perl

# Copyright (c) 1997-1999 Symbian Ltd.  All rights reserved.

# Version Info:
# Version 009: added command line -epoc as a twin to -epoc32 (-epoc32 is depricated)
# Version 008: added a comand line argment (-compress) to compress the resulting help file
#              added a command line argument (-unicode) to allow aleppo to generate a unicode
#              database
# Version 007: recognises that the customisation .alg file is read from the working directory
# Version 006:	- Able to find almain in paths containing ;; and those that start with ;
# Version 005: renamed smmkptml to rtf2ptml
# Version 004: now uses the $style_table variable, which now points to altable.sty
# Version 003: fixed 2 bugs
#				- check for existence of working directory now runs under Win95
#				- new check (early in the process) for the existence of the output directory
#				- path search (for almain) now case-independent
# Version 002: fixed bug - now honours the outputfile extension specified in the .alp file (for -epoc32 output)
# Version 001: aleppo shell - pipeline for modules (somewhat based on samaria.pl 017)

# ------------------------------------- Global variables -----------------------------------
# -------- filenames --------
$style_table = "altable.sty";
$input_directory = "";
$output_directory = "";
$working_directory = "";
$order_filename = "";
$customization = "";
$compress = 0; #don't compress by default
$unicode = ""; #don't emit unicode database by default
$log_name = pwd()."\\aleppo.log";
# ----- data structures -----
@source_filelist = ();
# -------- arguments --------
# ------------------------------------------------------------------------------------------

# ------------------------------------- Start of Main() ------------------------------------
	
&handle_command_line_args();						# get arguments from command-line
if ($target_format eq "epoc32") {
	&get_almain_path();								# get path for almain
}
&get_project_file();								# get rest of project info from project file

$test_directory = $output_directory;
$test_directory =~ s/(.*)\\/$1/;
if (-e "$test_directory") {} else {
	&log_die("Output directory '$output_directory' does not exist. Please create it. Exiting...");
}

$test_directory = $working_directory;
$test_directory =~ s/(.*)\\/$1/;
if (-e "$test_directory") {} else {
	print "Creating working directory...\n";
        system("md $test_directory");
}

if (-e "$log_name") {								# delete old logfile, if present
	system("attrib -r $log_name");
	unlink($log_name);
}

# convert each of the source files to ptml
	$index = 0;
	while ($current_filename = $source_filelist[$index]) {
		&convert_file_to_ptml($current_filename);
		$index++;
	}

# convert ptml files to one sgml file
&log_error("Translating cs-ptml to epochelp-sgml");
$ret_val = system("perl -S altopic.pl $project_filename");
if ($ret_val != 0) {
	&log_error("ERROR - altopic failed. Exiting aleppo shell.");
	exit;
}

# convert that file into the target format
if ($target_format eq "epoc32") {
	&convert_to_help_target();
} elsif ($target_format eq "html") {
	&convert_to_html_target();
} else {
	&log_die("Illegal target format: $target_format");
}

# -------------------------------------- End of Main() ----------------------------------------

sub convert_file_to_ptml {
	my($filename) = @_;
	&log_error("Processing $input_directory"."$filename".".rtf");

	&log_error("Translating rtf to ptml");
	#print "rtf2ptml $unicode -id$input_directory -od$working_directory -pd$working_directory -ml$log_name $repress_manual_errors_in_html $filename";
	$ret_val = system("rtf2ptml $unicode -id$input_directory -od$working_directory -pd$working_directory -ml$log_name $repress_manual_errors_in_html $filename");
	if ($ret_val != 0) {
		&log_error("ERROR - rtf2ptml failed. Exiting aleppo shell.");
		exit;
	}

	&log_error("Translating ptml to cs-ptml");
	$ret_val = system("perl -S alptm2cs.pl -id$working_directory -od$working_directory -ml$log_name $filename");
	if ($ret_val != 0) {
		&log_error("ERROR - alptm2cs failed. Exiting aleppo shell.");
		exit;
	}
}

sub convert_to_html_target {
	&log_error("Producing gifs for html proof");
	$ret_val = system("perl -S alimgcon.pl $project_filename");
	if ($ret_val != 0) {
		&log_error("ERROR - alimgcon failed. Exiting aleppo shell.");
		exit;
	}

	&log_error("Converting sgml to html");
	$ret_val = system("perl -S alhtml.pl -id$working_directory -od$output_directory -ml$log_name -sf$style_table $output_no_extender");
	if ($ret_val != 0) {
		&log_error("ERROR - alhtml failed. Exiting aleppo shell.");
		exit;
	}
}

sub convert_to_help_target {
	&log_error("Converting sgml to epochelp");

	$input_file = &get_abs_filename($working_directory).$output_no_extender.".sgml";
	$input_graphics_file = &get_abs_filename($working_directory).$output_no_extender.".mbm-list";
	$output_file = &get_abs_filename($output_directory).$output_no_extender.$output_extension;
	if ($customization_file ne "") {
		#strip off the path
		$customization_file=~s/.*\\//;
		#add the working directory
		$customization_file=$working_directory.$customization_file;
	
		$customization_file =~ /(.*)\./;
		$customization_file = $1.".alg";
		$customization_file = &get_abs_filename($customization_file);
		$customization = "-ic".$customization_file;
	}
	#print $almain_path."almain -if$input_file -ig$input_graphics_file -of$output_file $customization\n";
	$ret_val = system($almain_path."almain -if$input_file -ig$input_graphics_file -of$output_file $customization");
	if ($ret_val != 0) {
		&log_error("ERROR - almain failed. Exiting aleppo shell.");
		exit;
	}
	#check if a compressed database is required
	if($compress)
		{
		# print $almain_path."datazip $output_directory$output_no_extender$output_extension $output_directory$output_no_extender.c$output_extension";
		$ret_val = system("perl -S datazip.pl $unicode $output_directory$output_no_extender$output_extension");
		if ($ret_val != 0) {
			&log_error("ERROR - datazip failed. Exiting aleppo shell.");
			exit;
			}
		}
}

# ------------------------------------------- Misc functions ----------------------------------------

sub get_almain_path {
	# work out where aleppo.pl was invoked from
	$call_dir =~ s/(.*)aleppo/$1/;
	if ($call_dir eq "") {
		# invoked from the current directory or the path - find out where...
		# is it in the current dir?
		if (-e "aleppo.bat") {
			$almain_path = "";
		} else {
			# is it in the path
			$path = $ENV{"PATH"};
			$path =~ s/\;(\;*)/\;/g;
			$path =~ s/^;//;
			@path_list = split(/;/,$path);
			$found = 0;
			$path_index = 0;
			while (($found == 0) && ($this_path = $path_list[$path_index])) {
				if ($this_path =~ /\\epoc32\\tools/i) {
					$found = 1;
					$almain_path = $this_path."\\";
				}
				$path_index++;
			}
		}
	} else {
		# invoked from $call_dir
		$almain_path = $call_dir;
	}

# check if aleppo is to produce a unicode database
if($unicode eq "")
	{
	$almain_path=$almain_path."aleppo\\";
	}
else
	{
	$almain_path=$almain_path."ualeppo\\";
	}
}

sub get_abs_filename {
	my($rel_filename) = @_;

	if ($rel_filename =~ /:/) {
		$abs_filename = $rel_filename;
	} else {
		$cur_dir = &pwd();
		if (substr($rel_filename,0,1) eq "\\") {
			$cur_dir =~ s/(.*):(.*)/$1:/;
			$abs_filename = $cur_dir.$rel_filename;
		} else {
			$abs_filename = $cur_dir."\\".$rel_filename;
		}
	}
	return $abs_filename;
}

sub handle_command_line_args {
	&extract_command_line_args("aleppo");
}

sub extract_command_line_args {
	my($program_title) = @_;
	my($number_of_args) = 0;

	if ($this_arg = pop(@ARGV)) {
		if ($this_arg =~ /-calldir/) {
			print "Use of $program_title:\n\t$program_title [-epoc [-compress] | -html] [-unicode] project-file\n";
			exit;
		} else {
			$project_filename = $this_arg;			# project_filename is the last argument
			while ($this_arg = pop(@ARGV)) {
				if ($this_arg =~ /-epoc32/) {
					$target_format = "epoc32";
				}elsif ($this_arg =~ /-epoc/) {
					$target_format = "epoc32";
				}elsif ($this_arg =~ /-html/) {
					$target_format = "html";
				} elsif ($this_arg =~ /-calldir/) {
					$call_dir = $this_arg;
					$call_dir =~ s/-calldir(.*)/$1/;
				} elsif ($this_arg =~ /-compress/) {
					$compress = 1;
				} elsif ($this_arg =~ /-unicode/) {
					$unicode = "-unicode";
				}else {
					print "Illegal argument: $this_arg\n";
					exit;
				}
			}
		}
	} else {
		print "Use of $program_title:\n\t$program_title [-epoc [-compress] | -html] [-unicode] project-file\n";
		exit;
	}
}

sub get_project_file {
	open(PROJFILE,"$project_filename") || &log_die("Cannot find $project_filename");	# open project file

	while ($_ = <PROJFILE>)	{							# Iterate over all lines of the project file
		s/\n//;												# remove the end of line character from the line
		s/(.*)#(.*)/$1/;									# remove any comment from the line
		if (/inputdirectory/) {
			s/inputdirectory (.*)/$1/;
			$input_directory = $_;
		} elsif (/outputdirectory/) {
			s/outputdirectory (.*)/$1/;
			$output_directory = $_;
		} elsif (/sourcefile/) {
			s/sourcefile (.*)/$1/;
			$source_files = $_;
			@source_filelist = split(/ /,$source_files);
		} elsif (/outputfile/) {
			s/outputfile (.*)/$1/;
			$output_filename = $_;
			$output_filename =~ /(.*)\.(.*)/;
			$output_no_extender = $1;
			if ($2 ne "") {
				$output_extension = ".".$2;
			} else {
				$output_extension = "";
			}
		} elsif (/graphicsdirectory/) {
			s/graphicsdirectory (.*)/$1/;
			$graphics_directories = $_;
		} elsif (/workingdirectory/) {
			s/workingdirectory (.*)/$1/;
			$working_directory = $_;
		} elsif (/customizationfile/) {
			s/customizationfile (.*)/$1/;
			$customization_file = $_;
		} elsif (/orderfile/) {
			s/orderfile (.*)/$1/;
			$order_filename = $input_directory.$_;
		} else {
			print "Illegal project-file line: $_\n";
			exit;
		}
	}

	close(PROJFILE);
}

sub log_error {
	my($error_string) = @_;

	open(LOGFILE,">>$log_name") || die "Cannot create $log_name";				# open error log for appending
	print LOGFILE "$error_string\n";
	close(LOGFILE);
	print "$error_string\n";
}

sub log_die {
	my($error_string) = @_;

	print LOGFILE "ERROR - $error_string\n";
	die "ERROR - $error_string";
}

sub pwd {
	$dir = `cd`;								# find current directory
	chop($dir);									# remove the eoln
	return $dir;								# and return it
}

__END__
