#!/usr/bin/perl -w

# install dir in /
my $installdir='/usr/lib64/ooo';
# user dir in ~
my $home_dir='.ooo-2.0';
# user-dict name
my $custom_dict='corporate.dic';
# what type of action to perform
my $mode;
# locale - if any
my $locale = '';
# whether to install to users's setup
my $install_user = 0;


# debugging
if ($installdir =~ /^\@/) {
    $installdir = $ENV{OOINSTBASE};
    $installdir = '/usr/lib/ooo-2.1' if (!$installdir);
}

sub localize_path($)
{
    my $path = shift;
    if ($locale ne '') {
	$path .= "/$locale";
    }
    return $path;
}

sub ensure_path($)
{ # hack for now:
    my $path = shift;
    `mkdir -p $path`;
}

sub print_help_and_exit()
{
    print <<"EOF";
Usage: ootool [OPTION] [MODE] [FILE]...
Perform file based lock-down tasks on OpenOffice.org:
Options:
  --user           install in the user\'s setup not the system
  --locale=<br-PT> make this install locale-specific: eg. to Brazil
  --help           print this help
Modes:
  --template       install template(s)
  --user-dict      add words from a (text based) user-dictionary
EOF
    ;
    exit (0);
}

my @files = ();

for my $arg (@ARGV) {
# flags
    if ($arg eq '--user') {
	$install_user = 1;
    } elsif ($arg =~ m/--locale=(.*)/) {
	$locale = $1;
    }
# modes
    elsif ($arg eq '--template') {
	$mode = 'template';
    } elsif ($arg eq '--user-dict') {
	$mode = 'user-dict';
    }
# help
    elsif ($arg eq '-h' || $arg eq '--help') {
	print_help_and_exit ();
    }
# file arguments
    else {
	push @files, $arg;
    }
}

if (!defined $mode) {
    print "Must use a supported mode\n";
    print_help_and_exit();
}

@files || print_help_and_exit();

my $dest;

if ($install_user) {
    print STDERR "Warning: hard-coded home path\n";
    $dest = $ENV{HOME} . '/' . $home_dir . "/user";
} else {
    $dest = $installdir . "/share";
}

# Template mgmt
if ($mode eq 'template') {
    $dest .= "/template";
    $dest = localize_path ($dest);
    ensure_path ($dest);

    for my $file (@files) {
	`cp $file $dest` && die "Failed to copy $file to $dest: $!";
    }
}
elsif ($mode eq 'user-dict') {
    my @keys;
    $dest .= "/wordbook";
    ensure_path ($dest);
    $dest .= "/$custom_dict";
    if ($locale ne '') {
	print "Warning: no localized user-dict support yet";
    }
    my @lines = ();
    my $fh;
    for my $file (@files) {
	if ($file eq '-') {
	    $fh = STDIN;
	} else {
	    open ($fh, $file) || die "Can't open $file\n";
	}
	print "Reading words from $file\n";
	push @lines, <$fh>;
	close ($fh) if ($file ne '-');
	undef $fh;
    }
    if (!-f $dest) {
	print "Creating $custom_dict\n";
	open ($fh, ">", "$dest") || die "Can't create $dest\n";
	print $fh <<"EOF";
OOoDICT1
lang: <none>
type: positive
---
EOF
;
    } else {
	print "Appending to $custom_dict\n";
	open ($fh, ">>", "$dest") || die "Can't append to $dest\n";
    }
# Of course this may create duplicates but OO.o should
# elide them on re-writing the file.
    for my $line (@lines) {
	print $fh "$line";
    }
    close ($fh);
}
else {
    die "Unknown mode '$mode'";
}


