#!perl

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

# Version Info:
# Version 002: fixed bug in calculating mbm-list filename for output filenames with an extender
# Version 001: module to prepare jpegs of all those pictures needed for aleppo's html proofing

# ------------------------------------------ Global variables ---------------------------------------
# -------- filenames --------
$input_directory = "";
$output_directory = "";
$working_directory = "";
$mbm_list_filename = "";
$log_name = pwd()."\\aleppo.log";
# ----- data structures -----
@mbm_list = ();
# -------- arguments --------
# ---------------------------------------------------------------------------------------------------

# ------------------------------------------- Start of Main() ---------------------------------------
	
&handle_command_line_args();						# get arguments from command-line
&get_project_file();								# get rest of project info from project file

$output_no_extender = $output_filename;
$output_no_extender =~ s/(.*)\.(.*)/$1/;
$mbm_list_filename = $output_no_extender.".mbm-list";
$mbm_list_filename = $working_directory.$mbm_list_filename;

open(MBMLISTFILE,$mbm_list_filename) || &log_die("Cannot find $mbm_list_filename");		# open input file (.mbm-list);

while ($_ = <MBMLISTFILE>) {
	/<graphic index=(.*) archive=(.*) name=(.*) bitmapfile=(.*) bitmapindex=(.*)>/;
	
	$bmp_filename = $3.".bmp";
	$archive_filename = $4;
	$bitmap_index = $5;

	if (length($bitmap_index)==1) {
		$bitmap_index = "0".$bitmap_index;
	}

	$mbm_list{$archive_filename} .= ":$bitmap_index,$bmp_filename";
}

foreach $archive_filename (keys %mbm_list) {
	&create_image_proofs($archive_filename,$mbm_list{$archive_filename});
}

if (-e "$working_directory"."trash*.bmp") {
	system("del $working_directory"."trash*.bmp");
}

close(MBMLISTFILE);

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

sub create_image_proofs {
	my($archive_filename,$mbm_list) = @_;

	@bitmaps = split(/:/,$mbm_list);
	@sorted_bitmaps = sort(@bitmaps);

	$extract_string = "";
	$list_index = 1;
	$current_pad_point = 0;
	while ($this_bitmap = $sorted_bitmaps[$list_index]) {
		$this_bitmap =~ /(.*),(.*)/;
		$bitmap_index = $1;
		$bitmap_name = $2;
		if ($current_pad_point <= $bitmap_index) {
			while ($current_pad_point < $bitmap_index) {
				$extract_string .= $working_directory."trash$current_pad_point.bmp ";
				$current_pad_point++;
			}
			$extract_string .= $working_directory.$bitmap_name." ";
			$current_pad_point++;
		}
		$list_index++;
	}
	
	system("bmconv /u $archive_filename $extract_string");

	# convert the bitmaps from bmp => jpeg (using cjpeg)
	$list_index = 1;
	while ($this_bitmap = $sorted_bitmaps[$list_index]) {
		$this_bitmap =~ /(.*),(.*)/;
		$bitmap_name = $2;
		$output_name = $bitmap_name;
		$output_name =~ s/bmp/jpg/;
		system("cjpeg -quality 95 $working_directory$bitmap_name $output_directory$output_name");
		$list_index++;
	}
}

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

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

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

	if ($this_arg = pop(@ARGV)) {
		$project_filename = $this_arg;			# project_filename is the last argument
	} else {
		print "Use of $program_title:\n\t$program_title 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 (/outputdirectory/) {
			s/outputdirectory (.*)/$1/;
			$output_directory = $_;
		} elsif (/outputfile/) {
			s/outputfile (.*)/$1/;
			$output_filename = $_;
		} elsif (/workingdirectory/) {
			s/workingdirectory (.*)/$1/;
			$working_directory = $_;
		}
	}

	close(PROJFILE);
}

sub log_error {
	$error_string = $_[0];

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

sub log_die {
	$error_string = $_[0];
	if ($log_error == 1) {
		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__
