#!/usr/bin/perl
## ----------------------------------------------------------------------
## install-sgmlcatalog : Debian GNU/Linux SGML catalog management tool
## ----------------------------------------------------------------------
## Copyright (c) 1997 Christian Schwarz
## Copyright (c) 2001-2003 Ardo van Rangelrooij
##
## This is free software; see the GNU General Public Licence version 2
## or later for copying conditions.  There is NO warranty.
## ----------------------------------------------------------------------

## ----------------------------------------------------------------------
$catalog = '/usr/lib/sgml/transitional.cat';
$backup  = '/usr/lib/sgml/transitional.cat.old';

## ----------------------------------------------------------------------
$top_marker    = '-- START SGML CATALOG ENTRY FOR PACKAGE';
$bottom_marker = '-- END SGML CATALOG ENTRY FOR PACKAGE';
$eol_marker    = '--';

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

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

## ----------------------------------------------------------------------
if ( ! @ARGV )
{
    &version;
    print STDERR "\n";
    &usage;
    exit 1;
}

## ----------------------------------------------------------------------
$package = shift( @ARGV );

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

## ----------------------------------------------------------------------
open( CATALOG, "<$catalog" )
    or die "$name: cannot open SGML catalog $catalog for reading: $!";
while ( <CATALOG> )
{
    chop;
    if ( /$top_marker $package $eol_marker/o )
    {
	if ( $data[ $#data ] =~ /^\s*/o )
	{
	    pop( @data );
	}
	while ( !/$bottom_marker $package $eol_marker/o )
	{
	    if ( not ($_ = <CATALOG>) )
	    {
		print STDERR "$name: error: cannot find bottom marker for package $package:\n";
		print STDERR "$name:    $bottom_marker $package $eol_marker\n";
		print STDERR "$name: please remove the entry for $package yourself.\n";
		exit 1;
	    }
	}
    }
    else
    {
	push( @data, $_ );
    }
}
close( CATALOG );

## ----------------------------------------------------------------------
if ( -f $catalog )
{
    if ( -f $backup )
    {
	unlink( $backup )
	    or die "$name: cannot remove backup copy of catalog $backup: $!";
    }
    rename( $catalog, $backup )
	or die "$name: cannot rename $catalog to $backup: $!";
}
open( CATALOG, ">$catalog" )
    or die "$name: cannot open SGML catalog $catalog for writing: $!";
for ( @data )
{
    print CATALOG "$_\n";
}
close( CATALOG );

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

## ----------------------------------------------------------------------
sub version
{
    print STDERR <<END;
Debian GNU/Linux install-sgmlcatalog version 1.0

Copyright (c) 1997 Christian Schwarz
Copyright (c) 2001-2003 Ardo van Rangelrooij

This is free software; see the GNU General Public Licence version 2
or later for copying conditions.  There is NO warranty.
END
}

## ----------------------------------------------------------------------
sub usage
{
    print STDERR <<END;
Usage:
    install-sgmlcatalog --remove package
END
}

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