#!/usr/bin/perl -w
# 
# Copyright © 1999 Ričardas Čepas <rch@WriteMe.Com> <rch@richard.eu.org>
# http://www.angelfire.com/me/rch/ll.html
# All rights reserved.
# 
# Redistribution and use in source and binary forms, with or without
# modification, are permitted provided that the following conditions
# are met:
# 1. Redistributions of source code must retain the above copyright
#    notice, this list of conditions and the following disclaimer.
# 2. Redistributions in binary form must reproduce the above copyright
#    notice, this list of conditions and the following disclaimer in the
#    documentation and/or other materials provided with the distribution.
# 
# THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
# ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
# IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
# ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR AND CONTRIBUTORS BE LIABLE
# FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
# DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
# OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
# HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
# LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
# OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
# SUCH DAMAGE.
# 
# NC_baltic v1.12

require 5.0;
@NC_dirs=defined $ENV{MOZILLA_HOME} ? ($ENV{MOZILLA_HOME}):();
@NC_dirs=(@NC_dirs, '/opt/netscape','/usr/local/lib/netscape','/usr/local/netscape','/usr/lib/netscape','/usr/share/netscape','ASK','ASK','ASK','ASK','ASK');

sub patch($$;$) {
	my($in, $out, $binary) = @_;
	my @num_of_subst=(0,0);

	open(INf,'<'.$in) || die ("NC_baltic: can't read from file ``$in''");
	open(OUTf,'>'.$out) || die ("NC_baltic: can't write to file ``$out''");
	while (<INf>) {
		s{125[37]}{ $& eq '1253' ? '1257':'1253' }ge;
		s{8859([-_])([47])}{ "8859$1" . ($2 eq '4' ? '7':'4') }ge;
		if ($binary) {
			s/Faeroese \[fo\]/Estonian [ee]/g;
			s/Korean \[ko\]\t/Latvian [lv]/g;
			s/Macedonian \[mk\]/Lithuanian [lt]/g;
			s/(EncCmdString:\s+)\sGreek /$1Baltic /g;
			s/documentFonts.charset\*tis620.2533-1:/documentFonts.charset*windows-1257:\t/;
			s/(\*defaultCharset:\s*iso-8859-)1\b/${1}4/;
### The following changes default fonts in Messenger's lists and all text !:
			s/(%s\.documentFonts\.defaultCharSet.iso-8859-)1\b/${1}4/; ###

			s/(pref\("intl\.accept_languages",\s*)\s\s\s"en"\);/$1"lt,en");/;
			s/(pref\("intl\.accept_charsets",)\s\s\s\s(\s*"iso-8859-1)(,\*,utf-8"\);)/$1$2,iso-8859-4$3/; #+11-4
			s/(pref\("editor\.author",)\s\s\s\s(\s*")/$1$2/; #-4
			s/(pref\("editor\.auto_save_delay",)\s\s\s(\s*["\d])/$1$2/; #-3
			s/(ecma-118|elot_928|o-ir-126)/________/g;
			s {.{32}\xA0\xB5\xB6\xA3..\xA6\xA7\xA8\xA9.\xAB\xAC\xAD.\xAF.{78}\xFE.}
{E?,?".+????<?\xA8\xB7\xb8?`'""*--?T?>?\xAF\xB2?\xA0"CP\xA4"|\xA7\xD8C\xA3<N\xADR\xC6\xB0?23\xB4uP.\xF81\xB3>42?\xE6\xA1\xC7\xC0C\xC4\xC5\xCA\xAA\xC8\xC9Z\xCC\xAB\xD3\xCF\xA6\xA9N\xD1O\xD2\xD5\xD6\xD7\xD9LS\xDE\xDCZ\xAE\xDF\xB1\xE7\xE0c\xE4\xE5\xEA\xBA\xE8\xE9z\xEC\xBB\xF3\xEF\xB6\xB9n\xF1o\xF2\xF5\xF6\xF7\xF9ls\xFE\xFCz\xBE\xFF}g and $num_of_subst[0]++;
##  /*8x*/  'E', '?', ',', '?', '"', '.', '+', '?', '?', '?','?', '<','?',0xA8,0xB7,0xb8,
#	E ? , ? " . + ? ? ? ? < ? \xA8 \xB7 \xb8
##/*9x*/  '?', '`', '\'', '"', '"', '*', '-', '-', '?', 'T','?', '>','?',0xAF,0xB2,'?',
#	? ` ' " " * - - ? T ? > ? \xAF \xB2 ?
##/*Ax*/ 0xA0,'"','C','P',0xA4,'"', '|',0xA7,0xD8, 'C',0xA3, '<', 'N',0xAD, 'R',0xC6,
#	\xA0 " C P \xA4 " | \xA7 \xD8 C \xA3 < N \xAD R \xC6
##/*Bx*/ 0xB0, '?','2','3',0xB4, 'u', 'P', '.',0xF8,'1',0xB3, '>','4','2','?',0xE6,
#	\xB0 ? 2 3 \xB4 u P . \xF8 1 \xB3 > 4 2 ? \xE6
##/*Cx*/ 0xA1,0xC7,0xC0,'C',0xC4,0xC5,0xCA,0xAA,0xC8,0xC9,'Z',0xCC,0xAB,0xD3,0xCF,0xA6,
#	\xA1 \xC7 \xC0 C \xC4 \xC5 \xCA \xAA \xC8 \xC9 Z \xCC \xAB \xD3 \xCF \xA6
##/*Dx*/ 0xA9,'N',0xD1,'O',0xD2,0xD5,0xD6,0xD7,0xD9,'L','S',0xDE,0xDC,'Z',0xAE,0xDF,
#	\xA9 N \xD1 O \xD2 \xD5 \xD6 \xD7 \xD9 L S \xDE \xDC Z \xAE \xDF
##/*Ex*/ 0xB1,0xE7,0xE0,'c',0xE4,0xE5,0xEA,0xBA,0xE8,0xE9,'z',0xEC,0xBB,0xF3,0xEF,0xB6,
#	\xB1 \xE7 \xE0 c \xE4 \xE5 \xEA \xBA \xE8 \xE9 z \xEC \xBB \xF3 \xEF \xB6
##/*Fx*/ 0xB9,'n',0xF1,'o',0xF2,0xF5,0xF6,0xF7,0xF9,'l','s',0xFE,0xFC,'z',0xBE,0xFF,
#	\xB9 n \xF1 o \xF2 \xF5 \xF6 \xF7 \xF9 l s \xFE \xFC z \xBE \xFF
#

			s {.{32}\xA0..\xA3..\xA6\xA7\xA8\xA9.\xAB\xAC\xAD.\xAF
			\xB0\xB1\xB2\xB3\xB4\xA1\xA2\xB7\xB8\xB9\xBA\xBB\xBC\xBD\xBE\xBF
			.{62}\xFE.}
			{????????????????????????????????\xA0\xC0k\xAA\xA4I\xCF\xA7\x8D\xD0\xC7\xCCT\xAD\xDE\x9D\xB0\xE0\x9E\xBA\xB4i\xEF\x8E\x8F\xF0\xE7\xECtN\xFEn\xC2AAA\xC4\xC5\xAF\xC1\xC8\xC9\xC6E\xCBII\xCED\xD2\xD4\xCDO\xD5\xD6\xD7\xA8\xD8UU\xDCU\xDB\xDF\xE2aaa\xE4\xE5\xBF\xE1\xE8\xE9\xE6e\xEBii\xEEd\xF2\xF4\xEDo\xF5\xF6\xF7\xB8\xF8uu\xFCu\xFB\xFF}gx and $num_of_subst[1]++;
#? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ?
#? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ?
#\xA0 \xC0 k \xAA \xA4 I \xCF \xA7 \x8D \xD0 \xC7 \xCC T \xAD \xDE \x9D
#\xB0 \xE0 \x9E \xBA \xB4 i \xEF \x8E \x8F \xF0 \xE7 \xEC t N \xFE n
#\xC2 A A A \xC4 \xC5 \xAF \xC1 \xC8 \xC9 \xC6 E \xCB I I \xCE
#D \xD2 \xD4 \xCD O \xD5 \xD6 \xD7 \xA8 \xD8 U U \xDC U \xDB \xDF
#\xE2 a a a \xE4 \xE5 \xBF \xE1 \xE8 \xE9 \xE6 e \xEB i i \xEE
#d \xF2 \xF4 \xED o \xF5 \xF6 \xF7 \xB8 \xF8 u u \xFC u \xFB \xFF
#/*8x*/  '?', '?', '?', '?', '?', '?', '?', '?', '?', '?', '?', '?', '?', '?', '?', '?',
#/*9x*/  '?', '?', '?', '?', '?', '?', '?', '?', '?', '?', '?', '?', '?', '?', '?', '?',
#/*Ax*/ 0xA0,0xC0,'k',0xAA,0xA4,'I',0xCF,0xA7,0x8D,0xD0,0xC7,0xCC,'T',0xAD,0xDE,0x9D,
#/*Bx*/ 0xB0,0xE0,0x9E,0xBA,0xB4,'i',0xEF,0x8E,0x8F,0xF0,0xE7,0xEC,'t','N',0xFE,'n',
#/*Cx*/ 0xC2,'A','A','A',0xC4,0xC5,0xAF,0xC1,0xC8,0xC9,0xC6,'E',0xCB,'I','I',0xCE,
#/*Dx*/ 'D',0xD2,0xD4,0xCD,'O',0xD5,0xD6,0xD7,0xA8,0xD8,'U','U',0xDC,'U',0xDB,0xDF,/*Ex*/ 0xE2,'a','a','a',0xE4,0xE5,0xBF,0xE1,0xE8,0xE9,0xE6,'e',0xEB,'i','i',0xEE,
#/*Fx*/ 'd',0xF2,0xF4,0xED,'o',0xF5,0xF6,0xF7,0xB8,0xF8,'u','u',0xFC,'u',0xFB,0xFF,
#

		} else {
			s{English/United States \[en-US\]}{$&$'$`Estonian [ee]\t}g;
			s/Korean \[ko\]/$&$'$`Latvian [lv]$'$`Lithuanian [lt]/g;
			s/(spellDialog.*?String:\s*)Greek/$1Lithuanian/;
			s/(documentFonts.charsetlang\*iso-8859-4:\s*)\sGreek/$1Baltic/;
			s/-adobe-/-*-/; ###
			s/[^-]\*-helvetica/-*-helvetica/;
			s/(String:\s*)Greek/$1Baltic/;
			s/greek/balt_/g;
			s/httpAcceptLanguage:.*/httpAcceptLanguage: lt, en/;
			s/localeCharset\*korean:.*$/$&\n*localeCharset*lt_LT.ISO_8859-4	iso-8859-4\n*localeCharset*lt_LT.iso8859-4	iso-8859-4\n*localeCharset*lv_LV.ISO_8859-4	iso-8859-4\n*localeCharset*lv_LV.iso8859-4	iso-8859-4/;
			s/documentFonts.charset\*wp-symbol:/documentFonts.charset*windows-1257:$'$`$&/;
			s/^\*defaultCharset:\s*iso-8859-1\s*$/!$&/;
			s/^!(\*defaultCharset:\s*iso-8859-4\s*)$/$1/;
			s/^!(\*noAboutSplash:.*?)$/$1/;
		}
		s/-adobe-(.*?[,\0\s])/-*-$1\t\t\t\t/; ###
		s/Greek(?! \[el\])/Balt_/g;
		s/greek/balt_/g;
#		s/\*-iso8859-1/*-iso8859-4/g;
#		s/\000iso8859-1\000/\000iso8859-4\000/g;
		s/\*-iso8859-\*/*-iso8859-4/g; ###

		print OUTf;
	}
	close INf; close OUTf;
return @num_of_subst;
}
sub patch_failed {
	$_[0] == 1 && die("NC_baltic: Patching failed (@_) - wrong Netscape version or patch already was applied.");
	die("NC_baltic: Patching failed (@_) - size of binary changed! ");
}

foreach my $NC_dir (@NC_dirs) {
	@files=();
	if ($NC_dir eq 'ASK') {
		print "Enter pathname for Netscape Communicator v4 binary or press <Return> to exit:\n";
		chomp ($_=<>);
		/^\s*$/ and exit 1;
		$files[0]=$_;
		s#/[^/]*$##;
		$NC_dir=$_;
	} else {
		$files[0]="$NC_dir/netscape";
	}
	if ( -f $files[0].'.NC_baltic.orig') {
		$files[0] .= '.NC_baltic.orig';
	}
	if ( -f $files[0] && (my $size = -s _)) {
		if ( ! -B _ ) {
			print "File $files[0] is not binary..\n";
			next;
		}
		$size /= 1000000;
		print "found binary $files[0] [$size Mb]. \n";
		$files[1]="$NC_dir/Netscape.ad";
		if ( -e $files[1].'.NC_baltic.orig') {
			$files[1] .= '.NC_baltic.orig';
		}
		if (-e $files[1]) {
			print "found resources file $files[1] \n";
		} else {
			$files[1]="";
			print "Resources file $NC_dir/Netscape.ad not found ! \n";
			print "Enter pathname or press <Return> to skip this directory:";
			chomp ($_=<>);
			/^\s*$/ and next;
			$files[1]=$_;
		}
		$files[0] eq $files[1] and die("Files are the same !");
		print "Warning: this will not work with older Netscape versions like 4.05. \nIf you have export version and want to upgrade it to the version with 128bit\nencryption (www.fortify.net), do it before applying this patch.\n";
		print " Trying to get version: \n";
		system $files[0], '-version';
		print "Original files will be saved with .NC_baltic.orig extention.\n";
		print "Patch these Communicator files to add Baltic encodings instead of Greek? [Y/n]";
		<> =~ '^[Nn]' and next;
		print "Wait a moment..\n";
		my @ret=patch $files[0], "$files[0].NC_baltic.tmp", 1;
		($ret[0]==1 && $ret[1]==1) or patch_failed 1,@ret;
		(-s $files[0] == -s "$files[0].NC_baltic.tmp") or patch_failed 2,@ret;
		@ret=patch $files[1], $files[1].'.NC_baltic.tmp';
		foreach my $file (@files) {
#			if ( -s "$file.NC_baltic.orig" != -s $file ) 
#($dev,$ino,$mode,$nlink,$uid,$gid,$rdev,$size,$atime,$mtime,$ctime,$blksize,$blocks)
			my $file_without_orig = $file;
			$file_without_orig =~ s/.NC_baltic.orig$//;
			my @stat=stat($file_without_orig);
			if ($file_without_orig eq $file) {
				rename $file, "$file.NC_baltic.orig" or die "Can't rename $file->$file.NC_baltic.orig";
			}
			rename "$file.NC_baltic.tmp", $file_without_orig or die "Can't rename $file.NC_baltic.tmp->$file_without_orig";
			
			chmod $stat[2], $file_without_orig;
			chown $stat[4], $stat[5], $file_without_orig;
		}
		$files[1] =~ s/.NC_baltic.orig$//;
		my $syml_msg="If you want that $files[1] resources file \nbe actually used by Netscape you should make symlink \n\$XAPPLRESDIR/Netscape->$files[1] or \n/usr/(X11R6|openwin)/lib/X11/app-defaults/Netscape->$files[1]. \n";
		foreach my $res_dir (qw(/usr/lib/X11/app-defaults /usr/X11R6/lib/X11/app-defaults /usr/openwin/lib/X11/app-defaults)) {
			if ( -d $res_dir) {
				if (symlink $files[1], "$res_dir/Netscape") {
					print "Making symlink $res_dir/Netscape->$files[1]\n";
					$syml_msg="";
				}
				last;
			}
		}
		
		print "Done. \n${syml_msg}Both Baltic encodings are served by ISO-8859-4 fonts. \nSee README about proper settings and where to get fonts.\n";
		exit 0;
		
	} else {
		print "File $files[0] not found..\n";
	}
}
exit;
$in='/opt/netscape/netscape-dist';
$out='/opt/netscape/netscape';
@ret=patch $in, $out, 1;
($ret[0]==1 && $ret[1]==1) or patch_failed 1,@ret;
(-s $in == -s $out) or patch_failed 2,@ret;
patch '/opt/netscape/Netscape.ad-dist', '/opt/netscape/Netscape.ad';

