#!/usr/local/bin/perl
#
# Get rid of given block element (typically div) of the given class
# Arnaud Le Hors - lehors@w3.org
# $Id: htmltrim,v 1.4 2006-10-09 18:55:52 ihickson Exp $

$PROGNAME = substr($0, rindex($0, "/") + 1);

if ($#ARGV < 2) {
    print "Usage: $PROGNAME element class input output\n";
    exit 1;
}

$elem = $ARGV[0];
$class = $ARGV[1];
$input = $ARGV[2];
if ($ARGV[3]) {
    $output = $ARGV[3];
} else {
    $output = "-";
}

# copy file in memory
sub readfile {
    $buf = "";
    if (!open(INPUT, $_[0])) {
	print STDERR "$PROGNAME Error: Cannot open file: $_[0]\n";
	return;
    }
    while (<INPUT>) {
	$buf .= $_;
    }
    close(INPUT);
}

readfile($input);

$buf =~ s/<$elem[ \t\n]+[^>]*class=["']?$class(>|["' \t\n]+[^>]*>).*?<\/$elem>//sigoe;

open(OUTPUT, "> $output");
print OUTPUT $buf;
close(OUTPUT);
