#!/usr/bin/perl -l
use warnings;
use strict;
use Getopt::Long;

# update-locale-config: $id
# Based on same script from Skolelinux by Petter Reinholdtsen
# License is GPL.
# Konstantinos Margaritis, 2004
#
# This script updates localization configuration of a given
# Debian system. It will work on both woody and sarge and is 
# designed to be upgraded easily for future releases.
# So far it supports the configuration of localization for the 
# following packages:
#
# woody:
#    XFree86 (pre-install, 4.1.x-4.2.x)
#    locales (post-install)
#    timezone (post-install)
#    ispell (post-install)
#    KDE 2 (post-install)
#    ktouch (post-install)
#    links (post-install)
#    gdm (post-install)
#    
# sarge:
#    XFree86 (pre-install, 4.3.x)
#    dictionaries-common (pre-install, instead of ispell)
#    ktouch (post-install)
#    links (post-install)
#    KDE 3 (post-install)
#
#    In sarge locales, timezone information are set in debian-installer 
#    and do not need configuration afterwards.
#
# The script accepts 1 argument (for the locale)
# and a few options.
# The locale argument must be a valid locale entry.
# 
# if '--preinst/-p' is given then run all pre-install scripts.
# Otherwise (default) run post-install scripts.
#
# '--list/-l' lists all supported locales

require '/usr/lib/localization-config/common/log.pl';

use vars qw($LIB @scripts $listflag $preinstflag $lang);

$LIB = '/usr/lib/localization-config';

# Get command line options
GetOptions("list" => \$listflag,
           "preinst" => \$preinstflag);

# Find all scripts in $LIB
# These will be either .postinst or .preinst scripts
# accordingly if -p is given run all scripts ending in .preinst.
# Otherwise, run .postinst scripts.
if ($preinstflag) {
   @scripts = grep { $_ if -f $_ } glob "$LIB/*.preinst";
} else {
   @scripts = grep { $_ if -f $_ } glob "$LIB/*.postinst";
}

# list supported locales.
if ($listflag) {
    list_supported();
    exit 0;
}

# Get next argument, the locale entry
$lang = $ARGV[0];

if (defined($lang)) {
	log_msg("$0: Got langcode $lang");
}

# Print usual usage info if no args given or if user is not root.
unless ($lang) {
   usage();
   exit 1;
}
die "$0: You must be root\n" if $>;

my $numscripts = scalar(@scripts);

# Run each script 
for my $script (@scripts) {
    log_msg("$0: Running '$script $lang'");
    system ($script, $lang) if -x $script;
}

sub usage {
  print STDERR <<EOF;
Usage: $0 [options] <language>\n";
  Options:
       -d | --debug    Debugging mode
       -l | --list     List supported locales.
       -p | --preinst  Run the pre-install scripts instead of the (default) postinst scripts.
EOF

}

# Helper subroutine to search each script and print 
# supported locales
sub list_supported {
    my %locales;
    my %scriptsupport;
    for my $script (@scripts) {
        if (-x $script) {
            my @lines = `$script supported`;
            chomp(@lines);
            for my $locale (@lines) {
                $locales{$locale} = 1;
                $scriptsupport{"$script:$locale"} = 1;
            }
        }
    }
    print "Supported locales:";
    for my $locale (sort keys %locales) {
        print "  $locale";
        for my $script (sort @scripts) {
            if ( ! exists $scriptsupport{"$script:$locale"}) {
                 print "    Missing in $script";
            }
        }
    }
}#
