#!perl

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

# Version Info:
# Version 001: calls the correct version of datazip for compressing epoc help files

# ------------------------------------- Global variables -----------------------------------
# -------- filenames --------
$input_file = "";
$unicode = ""; #don't compress unicode database by default
$log_name = pwd()."\\aleppo.log";
# ------------------------------------------------------------------------------------------

# ------------------------------------- Start of Main() ------------------------------------
	
if(&handle_command_line_args())						# get arguments from command-line
	{

	&get_datazip_path();								# get path for datazip

	# print $datazip_path."datazip $output_directory$output_no_extender$output_extension $output_directory$output_no_extender.c$output_extension";
	$ret_val = system($datazip_path."datazip $input_file $input_file.c");
	if ($ret_val != 0) {
		&log_error("ERROR - datazip failed. Exiting aleppo shell.");
		exit;
	}
}

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

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

sub get_datazip_path {
	# work out where datazip.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 "datazip.exe") {
			$datazip_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;
					$datazip_path = $this_path."\\";
				}
				$path_index++;
			}
		}
	} else {
		# invoked from $call_dir
		$datazip_path = $call_dir;
	}

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

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

sub extract_command_line_args {
	my($program_title) = @_;
	my($success)=1;

	$this_arg=pop(@ARGV);

	if($this_arg ne "")
		{
		while ($this_arg)
			{
			if ($this_arg =~ /^[^-]/)
				{
				$input_file = $this_arg;			# input filename is the last argument
				}
			elsif ($this_arg =~ /-unicode/)
				{
				$unicode = "-unicode";
				log_error("Compression of unicode databases is not currently supported");
				$success=0;				
				}
			else
				{
				print "Illegal argument: $this_arg\n";
				exit;
				}
			$this_arg = pop(@ARGV)
			}
		}			
	else
		{
		print "Use of $program_title:\n\t$program_title [-unicode] filename";
		print "\nCompressed help is written to filename.c"; 
		exit;
		}
	$success;
	}

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__
