#!/usr/bin/perl

eval 'exec /usr/bin/perl  -S $0 ${1+"$@"}'
    if 0; # not running under some shell
## ----------------------------------------------------------------------
## Debian update-perl-sax-parsers version 0.1
## ----------------------------------------------------------------------
## Copyright (C) 2001 Ardo van Rangelrooij <ardo@debian.org>.
##
## This is free software; see the GNU General Public Licence version 2
## or later for copying conditions.  There is NO warranty.
## ----------------------------------------------------------------------

## ----------------------------------------------------------------------
use File::Spec ();
use XML::SAX ();

## ----------------------------------------------------------------------
$0    =~ m|[^/]+$|;
$name = $&;

## ----------------------------------------------------------------------
while ( $ARGV[0] =~ m/^--/ )
{
    $_ = shift( @ARGV );
    last if $_ eq '--';
    if ( $_ eq '--add' )
    {
        $add = 1;
    }
    elsif ( $_ eq '--remove' )
    {
        $remove = 1;
    }
    elsif ( $_ eq '--update' )
    {
        $update = 1;
    }
    elsif ( $_ eq '--quiet' )
    {
        $quiet = 1;
    }
    elsif ( $_ eq '--test' )
    {
        $debug = 1;
    }
    elsif ( $_ eq '--help' )
    {
        &usage;
	exit 0;
    }
    elsif ( $_ eq '--version' )
    {
        &version;
	exit 0;
    }
    else
    {
        print STDERR "$name: unknown option \`$_'\n";
	&usage;
	exit 1;
    }
}

## ----------------------------------------------------------------------
if ( ( $add || $remove ) && ! @ARGV )
{
    print STDERR "$name: too few arguments\n";
    exit 1;
}

## ----------------------------------------------------------------------
$parser_module = shift( @ARGV ) if $add || $remove;

## ----------------------------------------------------------------------
if ( @ARGV )
{
    print STDERR "$name: too many arguments\n";
    exit 1;
}

## ----------------------------------------------------------------------
print STDERR "$name: test mode - Perl SAX parsers file will not be updated\n"
    if $debug && ! $quiet;

## ----------------------------------------------------------------------
my $PARSER_DETAILS_DIR  = "/etc/XML/SAX/ParserDetails.d";
my $PARSER_DETAILS_FILE = "/etc/XML/SAX/ParserDetails.ini";

## ----------------------------------------------------------------------
if ( $add )
{
    print "$name: adding Perl SAX parser module info file of $parser_module...\n"
        unless $quiet;
    
    XML::SAX->save_parsers_debian( $parser_module );
}
elsif ( $remove )
{
    print "$name: removing Perl SAX parser module info file of $parser_module...\n"
        unless $quiet;
    
    my $parser_file = File::Spec->catfile( $PARSER_DETAILS_DIR, $parser_module);
    unlink( $parser_file );
}
elsif ( $update )
{
    print "$name: updating overall Perl SAX parser modules info file...\n"
        unless $quiet;

    open( PARSER_DETAILS_FILE, ">$PARSER_DETAILS_FILE" )
	|| die "Cannot write to $PARSER_DETAILS_FILE: $!";
    opendir( PARSER_DETAILS_DIR, $PARSER_DETAILS_DIR )
	|| die "Cannot access $PARSER_DETAILS_DIR: $!";
    while ( defined( $parser_details = readdir( PARSER_DETAILS_DIR ) ) )
    {
	next if $parser_details =~ /^\.\.?$/; # skip . and ..
	open( PARSER_DETAILS, "$PARSER_DETAILS_DIR/$parser_details" )
	    || die "Cannot read from $parser_details: $!";
	while ( <PARSER_DETAILS> ) { print PARSER_DETAILS_FILE; }
	close( PARSER_DETAILS );
    }
    closedir( PARSER_DETAILS_DIR );
    close( PARSER_DETAILS_FILE );
}

## ----------------------------------------------------------------------
exit 0;

## ----------------------------------------------------------------------
sub usage
{
    print STDERR <<END;
Usage:
    $name <options> --add <parser_module>
    $name <options> --remove <parser_module>
    $name <options> --update

Options:
    --quiet         be quiet
    --test          do not modify any files, enables debugging mode
    --version       display version number
    --help          display this help text (usage)
END
}

## ----------------------------------------------------------------------
sub version
{
    print "Debian $name version 0.1\n";
}

## ----------------------------------------------------------------------
