#! /usr/bin/perl

#   postinst - StarOffice 3.1 postinstallation script
#   Copyright (c) by Martin Schulze <joey@orgatech.de>
#
#   This program is free software; you can redistribute it and/or modify
#   it under the terms of the GNU General Public License as published by
#   the Free Software Foundation; either version 2 of the License, or
#   (at your option) any later version.
#
#   This program is distributed in the hope that it will be useful,
#   but WITHOUT ANY WARRANTY; without even the implied warranty of
#   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
#   GNU General Public License for more details.
#
#   You should have received a copy of the GNU General Public License
#   along with this program; if not, write to the Free Software
#   Foundation, Inc.,59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.

#
#   Parts of this program are stolen from the netscape-installer
#   which is written for Debian GNU/Linux by Brian White <bcwhite@pobox.com>
#

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

%files = (
	  "StarOffice31-common.tar.gz"        ,"All shared libraries, resources, fonts and other files",
	  "StarOffice31-english.tar.gz"       ,"English language demo documents and templates",
	  "StarOffice31-german.tar.gz"        ,"German language demo documents and templates",
	  "StarOffice31-dynbin.tar.gz"        ,"Dynamically vs. Motif linked binaries",
	  "StarOffice31-statbin.tar.gz"       ,"Statically vs. Motif linked binaries",
	  "StarOffice31-doc-german.tar.gz"    ,"German language manuals in pdf format",
	  "StarOffice31-upgrade2final.tar.gz" ,"Upgrade to final version"
	  );
@sofiles = ();

sub source
{
    print "Your archive in $tmp is not complete.  Please fetch the needed files\n";
    print "from one StarOffice server/mirror.  One possible ftp server would\n";
    print "be ftp://ftp.gwdg.de/pub/linux/staroffice/.\n";
    print "\n";
    print "You'll need at least StarOffice31-common.tar.gz and one of\n";
    print "StarOffice31-english.tar.gz or StarOffice31-german.tar.gz\n";
    print "depending your experience with the german language.  ;-)\n";
    print "You'll also need one of StarOffice31-dynbin.tar.gz or\n";
    print "StarOffice31-statbin.tar.gz\n";

    return 0;
}

sub motif
{
    print "Your archive in $tmp is not complete.  You tried to install binaries\n";
    print "which are linked dynamically against Motif 2.0 but haven't installed\n";
    print "Motif.  Please remove StarOffice31-dynbin.tar.gz and use\n";
    print "StarOffice31-statbin.tar.gz instead.  One possible ftp server would\n";
    print "be ftp://ftp.gwdg.de/pub/linux/staroffice/.\n";
    print "\n";
    print "You'll need at least StarOffice31-common.tar.gz and one of\n";
    print "StarOffice31-english.tar.gz or StarOffice31-german.tar.gz\n";
    print "depending your experience with the german language.  ;-)\n";
    print "You'll also need one of StarOffice31-dynbin.tar.gz or\n";
    print "StarOffice31-statbin.tar.gz\n";

    return 0;
}

sub checkmotif
{
    local (@ldso);
    local ($path);

    if (open (LDSO, "</etc/ld.so.conf")) {
	@ldso = <LDSO>;
	close (LDSO);
        push (@ldso, "/lib");
	foreach $path (@ldso) {
	    chomp $path;
	    return 1 if ( -r "$path/libXm.so.2");
	}
    }
    return 0;
}

sub lookup_files
{

    if (! -r "$tmp/StarOffice31-common.tar.gz" ) {
	print "I can't find the StarOffice archives in $tmp.  Did you place them\n";
	print "in a different directory?  Please give me another directory.\n> ";
	$tmp = <STDIN>;chop ($tmp);
	print "\n";
    }

    foreach $file (keys(%files)) {
	push (@sofiles, $file) if (-r "$tmp/$file");
    }
    if ($#sofiles > -1) {
	print "Found the following files in $tmp:\n";
	    foreach $file (@sofiles) {
		printf "  o %-35s- %s\n", $file, $files{$file};
	    }
	print "\n";
	return (&source()) if (! -r "$tmp/StarOffice31-common.tar.gz" );
	return (&source()) if (! -r "$tmp/StarOffice31-english.tar.gz" && ! -r "$tmp/StarOffice31-german.tar.gz" );
	return (&source()) if (! -r "$tmp/StarOffice31-dynbin.tar.gz" && ! -r "$tmp/StarOffice31-statbin.tar.gz" );
	return (&motif()) if (-r "$tmp/StarOffice31-dynbin.tar.gz" && ! -r "$tmp/StarOffice31-statbin.tar.gz" && ! &checkmotif() );
	return 1;
    }
    return (&source());
}

sub untar
{
    local ($tmp) = $_[0];
    local ($file) = $_[1];

    print "Untaring $file...\n";
    system "tar xfz $tmp/$file";
}

sub install_files
{
    local ($tmp) = $_[0];
    local ($german) = 0;

    chdir ("/usr/lib");
    do untar ($tmp, "StarOffice31-common.tar.gz");
    if ( -r "$tmp/StarOffice31-english.tar.gz" ) {
	do untar ($tmp, "StarOffice31-english.tar.gz");
    } else {
	do untar ($tmp, "StarOffice31-german.tar.gz");
	$german = 1;
    }
    if ( -r "$tmp/StarOffice31-dynbin.tar.gz" && checkmotif() ) {
	do untar ($tmp, "StarOffice31-dynbin.tar.gz");
    } else {
	do untar ($tmp, "StarOffice31-statbin.tar.gz");
    }
    do untar ($tmp, "StarOffice31-doc-german.tar.gz") if ($german && -r "$tmp/StarOffice31-doc-german.tar.gz");
    do untar ($tmp, "StarOffice31-upgrade2final.tar.gz") if ($german && -r "$tmp/StarOffice31-upgrade2final.tar.gz");
}

sub update_ldso
{
    local (@ldso);

    if (open (LDSO, "</etc/ld.so.conf")) {
	@ldso = <LDSO>;
	close (LDSO);

	if (!grep (/\/usr\/lib\/StarOffice\-3\.1\/linux-x86\/lib/, @ldso)) {
	    push (@ldso, "/usr/lib/StarOffice-3.1/linux-x86/lib\n");
	    if (open (LDSO, ">/etc/ld.so.conf")) {
		print LDSO @ldso;
		close (LDSO);
		system "ldconfig";
	    }
	}
    }
}

sub fix_setup
{
    my $setup = "/usr/lib/StarOffice-3.1/linux-x86/bin/setup";
    my $libdir = "/usr/X11R6/lib";

    system "sed s+$libdir/libXt.so+$libdir/notXt.xx+g < $setup > $setup.new"
	|| die "Can't modify binary file setup";
    system "mv -f $setup.new $setup; chmod a+x $setup";
}

sub query_daemon
{
    local ($svdaemon) = 0;
    local ($svportmap) = 0;
    local ($ans);
    local (@rc);

    print "\n";
    print "In order to start the online help from within the applications, the\n";
    print "program 'svdaemon' must be started.\n";
    print "Shall I start 'svdaemon' for you (y/N)? ";
    $ans = <STDIN>;
    $svdaemon = 1 if ($ans =~ /[yY]/);

    print "\n";
    print "In order to use inter-application communication features between\n";
    print "StarWriter, StarCalc and StarDraw, the program 'svportmap' must\n";
    print "be started.\n";
    print "Shall I start 'svportmap' for you (y/N)? ";
    $ans = <STDIN>;
    $svportmap = 1 if ($ans =~ /[yY]/);

    if (!$svdaemon || !$svportmap) {
	print "\n";
	print "If you change you mind just edit /etc/init.d/staroffice.\n";
	print "\n";
    }

    if (open (RC, "</etc/init.d/staroffice")) {
        @rc = <RC>;
        close (RC);
	open (RC, ">/etc/init.d/staroffice") || die "Can't open /etc/init.d/staroffice, $!\n";
	foreach $line (@rc) {
	    if ($line =~ /start-stop-daemon --start.*svportmap/) {
		if ($svportmap) {
		    $line =~ s/^#//;
		} else {
		    $line =~ s/^/#/ if ($line !~ /^#/);
		}
	    }
	    if ($line =~ /start-stop-daemon --start.*svdaemon/) {
		if ($svdaemon) {
		    $line =~ s/^#//;
		} else {
		    $line =~ s/^/#/ if ($line !~ /^#/);
		}
	    }
	    print RC $line;
	}
	close (RC);
    }
}

exit 0 if (-r "/usr/lib/StarOffice-3.1/linux-x86/bin/setup"); # already installed

print "Trying to install Star Office 3.1 on a Debian GNU/Linux system\n\n";
exit -1 if (! &lookup_files () );

do install_files ($tmp);

do update_ldso();

do fix_setup();

do query_daemon();

system "update-alternatives --install /usr/bin/swriter swriter /usr/bin/swriter3 20 "
       . "--slave /usr/man/man1/swriter.1.gz swriter.1.gz  /usr/man/man1/swriter3.1.gz > /dev/null";
system "update-alternatives --install /usr/bin/scalc scalc /usr/bin/scalc3 20 "
       . "--slave /usr/man/man1/scalc.1.gz scalc.1.gz  /usr/man/man1/scalc3.1.gz > /dev/null";
system "update-alternatives --install /usr/bin/schart schart /usr/bin/schart3 20 "
       . "--slave /usr/man/man1/schart.1.gz schart.1.gz  /usr/man/man1/schart3.1.gz > /dev/null";
system "update-alternatives --install /usr/bin/simage simage /usr/bin/simage3 20 "
       . "--slave /usr/man/man1/simage.1.gz simage.1.gz  /usr/man/man1/simage3.1.gz > /dev/null";
system "update-alternatives --install /usr/bin/sdraw sdraw /usr/bin/sdraw3 20 "
       . "--slave /usr/man/man1/sdraw.1.gz sdraw.1.gz  /usr/man/man1/sdraw3.1.gz > /dev/null";
system "update-alternatives --install /usr/bin/smath smath /usr/bin/smath3 20 "
       . "--slave /usr/man/man1/smath.1.gz smath.1.gz  /usr/man/man1/smath3.1.gz > /dev/null";
system "update-menus" if ( -x "/usr/bin/update-menus" );

system 'update-rc.d staroffice defaults >/dev/null';

system 'if [ -x /etc/init.d/staroffice ]; then /etc/init.d/staroffice start; fi';
