#! /usr/bin/perl
###############################################################################
#
#  Netscape post-install script
#
#  Written by Brian C. White <bcwhite@verisim.com>
#
###############################################################################



#
# Avoid unnecessary "postinst" if the next version's "preinst" fails
#
if ($ARGV[0] eq "abort-upgrade") {
    exit 0;
}



#
# Find the tmp directory
#
if ( "$ENV{'TMPDIR'}" ne "") {
    $tmp = $ENV{'TMPDIR'};
} else {
    $tmp = "/tmp";
}


#
# If netscape is already available, then skip rest of setup!
#
if ( -f "/usr/doc/netscape/Copyright" ) {
    print "- Old external files found -- no need to reinstall them\n";
    exit 0;
}


#
# Unpack the Netscape archive
#
$netscapetar="$tmp/==DISTNAME==.tar";
$netscape="";
$nstmpdir="netscape-unpacked.$$";

$netscapefile="$netscapetar.Z";
if ( -f $netscapefile ) {
    $taropt="xzf";
    $netscape=$netscapefile;
}

$netscapefile="$netscapetar.gz";
if ( -f $netscapefile ) {
    $taropt="xzf";
    $netscape=$netscapefile;
}

$netscapefile="$netscapetar";
if ( -f $netscapefile ) {
    $taropt="xf";
    $netscape=$netscapefile;
}

if ( "$netscape" eq "" ) {
    print "\nERROR: The Netscape archive must be in $tmp under the name:\n";
    print "       $netscapetar.[gz|Z]\n\n";
    print "       Do NOT simply rename the archive of another version in order to\n";
    print "       force it to install with this package.  It will not work.\n\n";
    print "       Archive files can be found on ftp[2-9].netscape.com.\n\n";
    exit 1;
}

print "- extracting Netscape from archive in $tmp\n";
chdir "$tmp"				|| die "ERROR: cannot cd to $tmp -- $!\n";
system "rm -rf $nstmpdir"		|| die "ERROR: cannot remove old $nstmpdir -- $!\n";
mkdir "$nstmpdir",0700			|| die "ERROR: cannot create dir $nstmpdir -- $!\n";
chdir "$nstmpdir"			|| die "ERROR: cannot cd to $tmp/$nstmpdir -- $!\n";
system "tar $taropt $netscape"		|| die "ERROR: cannot extract netscape archive -- $!\n";
system "strip netscape"			|| die "ERROR: cannot strip netscape executable -- $!\n";


#
# Slurp the Netscape binary and apply pathname fixes
#
undef $/;
open(NETSCAPE,"<$tmp/$nstmpdir/netscape") || die "ERROR: Could not read netscape executable -- $!\n";
$netscape = <NETSCAPE>;
close(NETSCAPE);


$netscape =~ s|/usr/local/lib/netscape\0|/usr/lib/netscape\0\0\0\0\0\0\0|g;
$netscape =~ s|/usr/local/lib/netscape/mime.types\0|/etc/mime.types\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0|g;
$netscape =~ s|/usr/local/lib/netscape/mailcap\0|/etc/mailcap\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0|g;
$netscape =~ s|/usr/local/lib/netscape/plugins\0|/usr/lib/netscape/plugins\0\0\0\0\0\0\0|g;
$netscape =~ s|/usr/local/lib/netscape/docs/Welcome.html\0|/usr/lib/netscape/Welcome.html\0\0\0\0\0\0\0\0\0\0\0\0|g;
$netscape =~ s|/usr/local/netscape/java\0|/usr/lib/netscape/java\0\0\0|g;
$netscape =~ s|/usr/local/netscape/java/classes\0|/usr/lib/netscape/java/classes\0\0\0|g;
$netscape =~ s|/usr/local/netscape/lib/linux\0|/usr/lacol/lib/netscape\0\0\0\0\0\0\0|g;

#$netscape =~ s|/usr/X386|/usr/.NSX|g;

die "ERROR: Failed to remove all references to '/usr/local'; stopped" if ($netscape =~ m|/usr/local|);

$netscape =~ s|/usr/lacol/|/usr/local/|g;

#$outfile='/usr/X11R6/bin/netscape';
$outfile='/usr/lib/netscape/netscape';
open(NETSCAPE,">$outfile") || die "ERROR: Could not write '$outfile' -- $!\n";
print NETSCAPE $netscape;
close(NETSCAPE);
chmod 0755,$outfile;


if (-s "$tmp/$nstmpdir/netscape" != -s $outfile) {
    unlink $outfile;
    die "ERROR: Patched binary differed in size from original; stopped";
}


#
# Copy remaining files to their final location
#
chdir "$tmp/$nstmpdir";
system "install -m  644 Netscape.ad /usr/X11R6/lib/X11/app-defaults/Netscape";
#ystem "install -m  755 hot-convert.sh /usr/X11R6/bin/hot-convert";
system "install -m 2755 -g mail movemail /usr/lib/netscape";

foreach (==CLASSES==) {
    system "install -m 644 $_ /usr/lib/netscape/java/classes";
}
foreach (==PLUGINS==) {
    system "install -m 755 $_ /usr/lib/netscape/plugins";
}

system "install -m  644 LICENSE /usr/doc/netscape/Copyright";
system "touch /usr/doc/netscape/Version-==VERSION==";


#
# Clean up unpacked netscape
#
system "rm -rf $tmp/$nstmpdir";
print "- Netscape archive is still in $tmp -- it should probably be removed\n";



#
# Print info message
#
print "- Netscape will not be able to read user mail spool files unless you set\n";
print "  the \"external movemail program\" to \"/usr/lib/netscape/movemail\".  Changing\n";
print "  the permissions of /var/spool/mail to 1777 (as suggested by Netscape) will\n";
print "  introduce a small security hole which, under some circumstances, could\n";
print "  allow someone else to get access to another's mail.\n";


## local variables:
## tab-width: 8
## end:
