#!/usr/bin/perl -w

# update-openoffice-dicts -- update OpenOffice.org's dictionary.lst
# (c) 2003 Ren Engelhard <rene@debian.org>
#          Agustin Martin Domingo <agmartin@debian.org>
# Public Domain


@print_comments=("## List of All Dictionaries to be Loaded by OpenOffice.org",
"## ---------------------------------------------------",
"## Each Entry in the list have the following space delimited fields",
"##",
"## Field 1: Entry Type \"DICT\" - spellchecking dictionary",
"##                     \"HYPH\" - hyphenation dictionary",
"##                     \"THES\" - thesaurus files",
"##",
"## Field 2: Language code from Locale \"en\" or \"de\" or \"pt\" ...",
"##",
"## Field 3: Country Code from Locale \"US\" or \"GB\" or \"PT\"",
"##",
"## Field 4: Root name of file(s) \"en_US\" or \"hyph_de\" or \"th_en_US\"",
"##          (do not add extensions to the name)",
"##",
"## This file is automatically updated by update-openoffice-dicts script",
"##",
"");

# put all entries not in the automatic section in the new dictionary.lst
# file (manually added dictionaries by the user)
sub write_manually_added() {
    my $line;
    my $inheader = 1;
    open(DL_ORIG, "$dictlist_orig") # open the existing file for reading...
	or die("Opening $dictlist_orig failed.\n");
    while (<DL_ORIG>) {
	chomp;
	$line = $_;
	undef $inheader if not m/^\#/;
	
	push @dictionary_orig,$line; # save original file
	
	if ($line eq $begin_string) {
	    # if we find the start of the section; set the flag that we are
	    # in the automatic section, ignore the following lines ...
	    $we_are_in_auto=1;
	    next;
	} elsif ($line eq $end_string) {
	    # ... until we find the end and set the flag back.
	    $we_are_in_auto=0;
	    next;
	} elsif ($we_are_in_auto == 0) {
	    # we are in non-automatic section; copy the user added entries
	    # into the temporary array; but that only if the line is not a ##
	    # comment or no blank line
	    next if $line =~ /^\#\#/;
	    next if $line =~/^\s*$/;
	    if ($inheader) {
		next if $line =~ /^\#/;
	    }
	    push @dictionary_lst, $line;
	}
    }
    close(DL_ORIG);
}

# read all files in $entrydir and put their contents verbatim in
# the new dictionary.lst
sub write_automatic_section() {
    push @dictionary_lst, "$begin_string";
    push @dictionary_lst, "$str_instruct_add";
    foreach $file (<$entrydir/*>) {
	open(ENTRY,"$file");
	while (<ENTRY>){
	    chomp;
	    push @dictionary_lst, $_;
	}
	close(ENTRY);
    }
    push @dictionary_lst, "$end_string", "";
}

sub write_dictionary_lst() {
	push @dictionary_lst, @print_comments;
	write_automatic_section();

	if ( -e $dictlist_orig ) {
    	    $we_are_in_auto=0;
            write_manually_added();
	}

	open(NEWFILE, "> $dictlist_orig") # open the existing file for writing.
    	    or die("Opening $dictlist_orig failed.\n");
	foreach (@dictionary_lst) { print NEWFILE $_, "\n"; }
	close(NEWFILE);

	open(OLDFILE, "> $dictlist_orig.old") # open the backup file for writing
    	    or die("Opening $dictlist_orig.old failed.\n");
	foreach (@dictionary_orig) { print OLDFILE $_, "\n"; }
	close(OLDFILE);

	# make sure the file has the right permissions
	chown(0, 0, $dictlist_orig);
	chmod(0644, $dictlist_orig);
}

# --------------------------------------------------------------------
# The main program
# --------------------------------------------------------------------

$dictlist_orig    = "/etc/openoffice/dictionary.lst";
$entrydir         = "/usr/share/myspell/infos/ooo";
$begin_string     = "## !!! BEGIN AUTOMATIC SECTION -- DO NOT CHANGE !!!";
$end_string       = "## !!! END AUTOMATIC SECTION -- DO NOT CHANGE !!!";
$str_instruct_add = "## !!! ADD YOUR ADDITIONAL ENTRIES BELOW THIS SECTION !!!";
@dictionary_orig  = (); # The array with the original strings from dictionary.lst
@dictionary_lst   = (); # The array with the strings to go to new dictionary.lst

die "$0: This script needs root privileges...\n" if ($< != 0);

print STDOUT "Updating OpenOffice.org's dictionary list... ";

# we may need to create the dir...
unless ( -d "/etc/openoffice" ) {
    mkdir("/etc/openoffice",0755) || die "can't mkdir /etc/openoffice: $!\n";;
}

write_dictionary_lst();

print STDOUT "done.\n";

__END__

=head1 NAME

update-openoffice-dicts - rebuild dictionary.lst for OpenOffice.org

=head1 SYNOPSIS

update-openoffice-dicts

=head1 DESCRIPTION

update-openoffice-dicts can be used to regenerate dictionary.lst for
openoffice.  This script is run by myspell dictionaries when they are
installed or removed.

It is not normally necessary to run update-openoffice-dicts by hand. 
Packages containing dictionaries create a file in /usr/share/myspell/infos
and call this script automatically.

update-openoffice-dicts will create a new dictionary.lst if it does not
already exist.

=head1 FILES

/etc/openoffice/dictionary.lst, /usr/share/myspell/infos/ooo/*

=head1 SEE ALSO

openoffice(1), dictionary.lst(5)

=head1 AUTHORS

Rene Engelhard, Agustin Martin Domingo

=cut

#  LocalWords:  openoffice myspell dicts
