#!/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
# '--debug/-d' enable debugging

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

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

# Get command line options
GetOptions("list" => \$listflag,
           "debug" => \$debug,
           "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];

debug("Got langcode '$lang'\n");

# Print usual usage info if no args given or if user is not root.
die "Usage: $0 <language>\n" unless $lang;
die "$0: You must be root\n" if $>;

my $numscripts = scalar(@scripts);

# Run each script 
for my $script (@scripts) {
    debug("Running '$script $lang'\n");
    system ($script, $lang) if -x $script;
}

#
sub debug {
    print @_ if $debug;
}

# 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";
            }
        }
    }
}#
