#! /usr/bin/perl
###############################################################################
#
#  Netscape post-install script
#
#  Written by Brian White <bcwhite@pobox.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/lib/netscape/LICENSE" ) {
    print "- Old external files found -- no need to reinstall them\n";
    print "- If you want to force a reinstall, first do: dpkg --purge ==PACKAGE====VERSION==\n";
    exit 0;
}


#
# Unpack the Netscape archive
#
@nsfiles = sort <$tmp/==DSTNAM1==>,<$tmp/==DSTNAM2==>;
$nstmpdir="netscape-unpacked.$$";

foreach $file (@nsfiles) {
    print "- Found '$file'...  ";
    if (-o $file) {
	if ( -r $file ) {
	    print "(valid)\n";
	    $netscape = $file;
	    if ($netscape =~ m/\.tar\.(gz|Z)$/) {
		$taropt="xzf";
	    } elsif ($netscape =~ m/\.tar$/) {
		$taropt="xf";
	    }
	} else {
	    print "(ignored -- not readable)\n";
	}
    } else {
	print "(ignored -- not owned by root)\n";
    }
}


unless ($netscape) {
    print "\nERROR: The Netscape archive must be in $tmp, owned by root,\n";
    print "       and under a name matching one of the following:\n";
    print "             ==DSTNAM1==\n";
    print "             ==DSTNAM2==\n";
    print "\n       Archive files can be found on ftp.netscape.com and its mirrors.\n\n";
    exit 1;
}

print "- extracting from $netscape\n";
chdir "$tmp"				|| die "ERROR: cannot cd to $tmp -- $!\n";
if (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";
if (system "tar $taropt $netscape")	{  die "ERROR: cannot extract netscape archive -- $!\n"; }
system "chown -R root.root .";
system "chmod -R go=rX . ";


#
# Deal with archives that have a sub-directory (v4.05+)
#
if (! -f "ns-install") {
    foreach $dir (<*>) {
	if (-f "$dir/ns-install") {
	    chdir $dir;
	    last;
	}
    }
}
if (! -f "ns-install") {
    die "ERROR: could not locate 'ns-install' script in archive\n";
}
chmod 0755,"ns-install";


#
# Determine what was previously installed
#
$existingfiles = `cd /usr/lib/netscape; find . -type f -print`;
$existingfiles =~s|^./||mg;
@existingfiles = split(/\s*\n\s*/,$existingfiles);


#
# Call the netscape installer
#
rename "$ENV{HOME}/.netscape","$ENV{HOME}/.netscape-real";
system "rm -rf $ENV{HOME}/.netscape";
delete $ENV{GZIP};
$ENV{MOZILLA_HOME} = "/usr/lib/netscape";
if (system "./ns-install </dev/null") { die "ERROR: netscape install script failed -- $!\n"; }
open(FILE,">/usr/doc/netscape/Version-==VERSION==");
close(FILE);
system "chown -R root.root /usr/lib/netscape";


#
# Clean up stupid things the installer does
#
foreach $file (<$ENV{HOME}/.netscape/*>) {
    print "Found '$file'...\n";
    system "install -m 644 $file /usr/lib/netscape/.netscape/";
    unlink($file);
}
system "rm -rf $ENV{HOME}/.netscape";
rename "$ENV{HOME}/.netscape-real","$ENV{HOME}/.netscape";
system "chgrp mail /usr/lib/netscape/movemail";
chmod 02755,	  "/usr/lib/netscape/movemail";
system "rm -f /usr/lib/netscape/*-dynMotif*";
system "chmod -R u+w /usr/lib/netscape/";


#
# Determine what was newly installed
#
$allfiles = `cd /usr/lib/netscape; find . -type f -print`;
$allfiles =~s|^\./||mg;
open(NEWFILES,">/usr/lib/netscape/.manifest");
foreach $file (split(/\s*\n\s*/,$allfiles)) {
    print NEWFILES $file,"\n" if (!grep(/^\Q$file\E$/,@existingfiles));
}
close(NEWFILES);


#
# Make local directories
#
mkdir "/usr/local/lib/netscape",0755;


#
# Clean up unpacked netscape
#
chdir $tmp;
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";


#
# Install menu file
#
open(FILE,">/usr/lib/menu/==PACKAGE==") || die "ERROR: Could not write file '/usr/lib/menu/==PACKAGE==' -- $!\n";
print FILE '?package(netscape4):needs=x11 section=Apps/Net title="Netscape Navigator" command="/usr/bin/X11/netscape"'."\n";
if ($netscape =~ m|communicator-[^/]*$|) {
    print FILE '?package(netscape4):needs=x11 section=Apps/Net title="Netscape Mail" command="/usr/bin/X11/netscape mailbox:"'."\n";
    print FILE '?package(netscape4):needs=x11 section=Apps/Net title="Netscape News" command="/usr/bin/X11/netscape news:"'."\n";
}
close(FILE);


#
# Remaining postinst stuff
#
if (-x "/usr/bin/update-menus") {
    system "/usr/bin/update-menus";
}
if (-x "/usr/sbin/update-mime") {
    system "/usr/sbin/update-mime";
}


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