#!/usr/bin/perl -w 

# This script has three uses:
# 1. From the command line to install straight into a given directory:
#    bin/ooinstall /opt/Foo
# 2. From the command line to link into a given directory:
#    bin/ooinstall -l /opt/FooLinked
# 3. When packaging (called from package-ooo), to install to DESTDIR

%setup_vars = ();
%configure_vars = ();

sub suck_setup($)
{
    my $file = shift;
    if (-f $file) {
	print "Reading setup from $file\n";
	open ($Vars, "bash -c '. $file ; set'|") || die "Can't find $file: $!";
	while (<$Vars>) {
	    /([^=]*)=(.*)/ || next;
	    $setup_vars{$1} = $2;
	}
	close ($Vars);
	return 1;
    }
    return 0;
}

( -f "/proc/meminfo" ) || die "The installer cannot work without javaldx running, which requires /proc to be mounted";

suck_setup ("./setup") || suck_setup ("bin/setup") || die "can't find bin/setup";

print "Sucking env from build setup\n";
my $fname = `ls $setup_vars{'OOBUILDDIR'}/*.[sS]et.sh`;
open ($Vars, $fname) || die "Can't open $fname: $!";
while (<$Vars>) {
    /^alias/ && next;
    /([^=]*)=\S*"(.*)"\S*/ || next;
    if (!defined $configure_vars{$1}) {
	$configure_vars{$1} = $2;
#	print "Configure var '$1' -> '$2'\n";
    }
}
close ($Vars);

print "Performing environment substitutions ...\n";
my @substitutions = ( 'SOLARENVINC', 'SOLARENV', 'SRC_ROOT', 'UPD', 'INPATH',
		      'OUTPATH', 'SOLARVER', 'JAVA_HOME' );
# perform substitutions
for my $subst (@substitutions) {
    for my $key (keys %configure_vars) {
#	$configure_vars{$key} =~ m/\$$subst/ && print "Match $subst in $key\n";
	$configure_vars{$key} =~ s/\$$subst/$configure_vars{$subst}/g;
    }
}    

# Workaround for system Mozilla
if ( $configure_vars{'SYSTEM_MOZILLA'} eq 'YES' ) {
    $configure_vars{'LD_LIBRARY_PATH'} = "$configure_vars{'MOZ_LIB'}:$configure_vars{'LD_LIBRARY_PATH'}";
}

# Workaround for the Python
$configure_vars{'PYTHONPATH'} = "$setup_vars{'OOBUILDDIR'}/instsetoo_native/$configure_vars{'INPATH'}/bin:$configure_vars{'SOLARVER'}/$configure_vars{'UPD'}/$configure_vars{'INPATH'}/lib";

my $path = '';
my $do_link = 0;

for $arg (@ARGV) {
    if ($arg eq '-l') {
	$do_link = 1;

    } elsif ($arg eq '-h' || $arg eq '--help') {
	$help = 1;
    } else {
	$path = $arg;
    }
}

$help = 1 if $path eq '';

if ($help) {
    print "ooinstall [-l] <prefix to install to>\n";
    print "  -l - performs a linkoo on the installed source\n";
    exit 1;
}

print "Setting up environment\n";
for $a (keys %configure_vars) {
    $ENV{$a} = $configure_vars{$a};
}
my $BUILD=undef;
my $LAST_MINOR=undef;
open MINORMK, "$ENV{'SOLARENV'}/inc/minor.mk";
while (<MINORMK>) {
	my $t = "\$" . $_;
	if(/^BUILD/ || /^LAST_MINOR/) {
		eval $t;
	}
}
close MINORMK;
$ENV{LAST_MINOR} = $LAST_MINOR;
$ENV{OUT} = "../$configure_vars{'INPATH'}";
$ENV{LOCAL_OUT} = $ENV{OUT};
$ENV{LOCAL_COMMON_OUT} = $ENV{OUT};
# FIXME: the following variable helps to install localizations even if some
# files are not localized (like Japanese, Chinese wordbook), it makes
# the installer to use the English localization of the file instead.
$ENV{DEFAULT_TO_ENGLISH_FOR_PACKING} = 1;

$langs=$setup_vars{OOO_LANGS_LIST};
$langs =~ s/\s+/,/g;
# FIXME: hack... we get a useless , at the end which makes it being e.g. zu#
# which breaks the build...
$langs =~ s/,'/'/;

$destdir='';
if ( defined $setup_vars{OODESTDIR} &&
     $setup_vars{OODESTDIR} ne "" ) {
    $destdir = "-destdir \"$setup_vars{OODESTDIR}\"";
}

$strip='';
if ( defined $setup_vars{OOO_STRIP} &&
     $setup_vars{OOO_STRIP} eq "no" ) {
    $strip = "-dontstrip";
}

print "Running installer\n";
system ("cd $setup_vars{OOBUILDDIR}/instsetoo_native/util ; " .
	"perl -w $configure_vars{SOLARENV}/bin/make_installer.pl " .
	"-f openoffice.lst -l $langs -p OpenOffice " .
	"-packagelist ../inc_openoffice/unix/packagelist.txt " .
	"-addpackagelist ../inc_openoffice/unix/packagelist_language.txt " .
	"-buildid $BUILD $destdir $strip " .
	"-simple $path") && die "Failed to install: $!";

if (($setup_vars{'VENDORNAME'} eq 'Novell' || $setup_vars{'VENDORNAME'} eq 'Debian' ) && $configure_vars{BUILD_TYPE} =~ m/ODK/) {
    print "Running SDK installer\n";
    system ("cd $setup_vars{OOBUILDDIR}/instsetoo_native/util ; " .
	"perl -w $configure_vars{SOLARENV}/bin/make_installer.pl " .
	"-f openoffice.lst -l en-US -p OpenOffice_SDK " .
	"-packagelist ../inc_sdkoo/unix/packagelist.txt " .
	"-buildid $BUILD $destdir $strip " .
	"-simple $path/sdk") && die "Failed to install: $!";
}
print "Installer finished\n";

if ($do_link) {
    `$configure_vars{SOLARENV}/bin/linkoo $path $configure_vars{SRC_ROOT}`;
}

print "Installing extra dictionaries...\n";
system ("cd $setup_vars{TOOLSDIR}/bin ; " .
       	"sh ./install-dictionaries $path/share/dict/ooo") && die "Failed to install dictionaries: $!";

print "Building galleries...\n";
system ("cd $setup_vars{TOOLSDIR}/bin ; " .
       	"sh ./build-galleries $path") && die "Failed to build extra galleries: $!";

system ("cd $setup_vars{TOOLSDIR}/bin ; " .
       	"sh ./install-mono $path") && die "Failed to finish mono installation: $!";
