#!/usr/local/bin/perl
# Insert some file after the first occurence of a given tag
#
# Arnaud Le Hors - lehors@w3.org
# $Id: insafter,v 1.3 2006-10-09 18:55:52 ihickson Exp $

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

if ($#ARGV < 3) {
    print "Usage: $PROGNAME src src2 tag trgt\n";
    exit 1;
} else {
    $input = $ARGV[0];
    $input2 = $ARGV[1];
    $tag = $ARGV[2];
    $output = $ARGV[3];
}


### main

if (!open(INPUT, $input)) {
    print STDERR "$PROGNAME Error: Cannot open file: $input\n";
    exit 1;
}
if (!open(INPUT2, $input2)) {
    print STDERR "$PROGNAME Error: Cannot open file: $input2\n";
    exit 1;
}
open(OUTPUT, "> $output");
LOOP: while (<INPUT>) {
    print OUTPUT $_;
    if (/<$tag>/i) {
	while (<INPUT2>) {
	    print OUTPUT $_;
	}
	last LOOP;
    }
}
while (<INPUT>) {
    print OUTPUT $_;
}
close(INPUT);
close(INPUT2);
close(OUTPUT);
