#!/usr/bin/perl -w

use strict;

use vars qw/ $OUTPUT_DIR $INPUT_FILE $WIZARD_DIR/;

BEGIN {
    require Cwd;
    require File::Basename;

    $INPUT_FILE = Cwd::abs_path( $ARGV[0] );
    $OUTPUT_DIR = File::Basename::dirname( $INPUT_FILE );

    $WIZARD_DIR = Cwd::abs_path(
            File::Basename::dirname(
                File::Basename::dirname( $0  )
                ));
}

#
# FIND LIBRARY
#
print STDERR "[MSG] Starting validation of your xml file.\n\n";
print STDERR qx/ xmlproc_val $INPUT_FILE /;
print STDERR "[MSG] Finished validation of your xml file.\n\n";

if ( not -e "$OUTPUT_DIR/modwizard.dtd" )
    {
    print STDERR "Copy modwizard.dtd to $OUTPUT_DIR and try again!\n\n";
    exit 1;
    }

print STDERR "[MSG] Executing the wizard\n\n";
print STDERR qx/ xsltproc --stringparam output $OUTPUT_DIR $WIZARD_DIR\/lib\/xarmodule.xsl $INPUT_FILE /;
print STDERR "[MSG] Finished the wizard\n\n";

#
# Take all the generated xd files and remove the lame xmlns things
# this is a fault of Xaraya, not the xst processor (although its implementation could be a bit more flexible)
#
print STDERR "[MSG] Doing some postprocessing to compensate for limitations\n\n";
#print STDERR qx/ find $OUTPUT_DIR -name '*.xd' -exec gawk '{gsub("xmlns:xar=\"dd\""," "); print {} > FILENAME }' '{}' ';'/;
print STDERR qx/ find $OUTPUT_DIR -name '*.xd' -exec perl -pi -e 's\/xmlns\:xar\="dd"\/ \/g' '{}' ';'/;
