#!/usr/bin/perl -w
#
# summary of how this script can be called:
#        * <new-config> pre-install
#        * <new-config> pre-upgrade
#        * <new-config> pre-remove
#        * <new-config> configure
#        * <new-config> reconfigure
#        * <old-script> remove
#        * <old-script> purge
#        * <old-script> upgrade
#
# for details, see http://www.debian.org/doc/debian-policy/ or
# the debian-policy package

use strict;

my $ALL_USER_CONFIG_FILE;
my $BEGIN_COMMENT;
my $END_COMMENT;

BEGIN {
	$ALL_USER_CONFIG_FILE = '/etc/bash.bashrc';
	$BEGIN_COMMENT = "jre_jni_path.perl: add JNI into library path - do not remove";
	$END_COMMENT = "jre_jni_path.perl: end of specific shell code";
}

#--------------------------------------------------------------
# Add shell code inside a file (assuming sh-like syntax)
# $_[0] : filename,
# $_[1] : comment begin
# $_[2] : comment end
# $_[3]... : code,
sub addShellCode($$$@) {
	my $filename = shift;
	my $bComment = shift;
	my $eComment = shift;
	my @lines = ();
	my $added = 0;

	my $code = join("\n", @_);
	return 1 unless ($code);

	$code .= "\n" unless ($code =~ /\n$/s);

	local *FILE;
	open(*FILE,"< $filename") or die("$filename: $!\n");
	while (my $line = <FILE>) {
		if ($line =~ /^\s*\#?\s*\Q$bComment\E/) {
			$added = 1;
		}
		push @lines, "$line";
	}
	close(*FILE);

	unless ($added) {
		push @lines, "\n";
		push @lines, "# $bComment\n";
		push @lines, "$code";
		push @lines, "# $eComment\n";
	}

	open(*FILE,"> $filename") or die("$filename: $!\n");
	foreach my $line (@lines) {
		print FILE "$line";
	}
	close(*FILE);
	return 1;
}

#--------------------------------------------------------------
# Remove shell code from inside a file (assuming sh-like syntax)
# $_[0] : filename,
# $_[1] : comment begin
# $_[2] : comment end
sub removeShellCode($$$) {
	my @lines = ();
	my $removed = 0;
	my $removing = 0;
	local *FILE;
	open(*FILE,"< $_[0]") or die("$_[0]: $!\n");
	while (my $line = <FILE>) {
		if ($removing) {
			if ($line =~ /^\s*\#?\s*\Q$_[2]\E/) {
				$removing = 0;
			}
		}
		elsif ($line =~ /^\s*\#?\s*\Q$_[1]\E/) {
			$removing = 1;
			$removed = 1;
		}
		else {
			push @lines, "$line";
		}
	}
	close(*FILE);

	if ($removed) {
		my $content = join('', @lines);
		$content =~ s/\n{2,}/\n\n/gs;
		$content =~ s/\n+$/\n/gs;
		open(*FILE,"> $_[0]") or die("$_[0]: $!\n");
		print FILE "$content";
		close(*FILE);
	}
	return 1;
}

#--------------------------------------------------------------
# Main program

# Avoid stdout buffering
$|=1;

if ($ARGV[0] && $ARGV[0] eq 'remove') {
	print "Removing LD_LIBRARY_PATH definition\n";
	removeShellCode($ALL_USER_CONFIG_FILE, 
		$BEGIN_COMMENT, $END_COMMENT);
}
else {
	print "Adding LD_LIBRARY_PATH definition\n";
	addShellCode($ALL_USER_CONFIG_FILE, 
		$BEGIN_COMMENT, $END_COMMENT,
		"if test -z \"\$LD_LIBRARY_PATH\"",
		"then",
		"\tLD_LIBRARY_PATH=\"\$JNI_LIBRARY_PATH\"",
		"else",
		"\tLD_LIBRARY_PATH=\"\$LD_LIBRARY_PATH:\$JNI_LIBRARY_PATH\"",
		"fi",
		"export LD_LIBRARY_PATH");

}

exit(0);
