#!/usr/bin/perl
eval 'exec /usr/bin/perl -w -S $0 ${1+"$@"}'
	if 0; # not running under some shell

use strict;
use XML::DOM;
use WAP::wbxml;

usage() unless @ARGV;
usage() if $ARGV[0] eq '-h';

my $infile = $ARGV[0];
my $outfile = $ARGV[1];

my $parser = new XML::DOM::Parser;
my $doc = $parser->parsefile($infile);
my $encoding = $doc->getXMLDecl()->getEncoding();
my $publicid = $doc->getDoctype()->getPubId();
die "$0: error: no PublicId.\n" unless ($publicid);

my $rules = WbRules->Load();
my $wbxml = new WbXml($rules, $publicid);
my $output = $wbxml->compile($doc, $encoding);
$outfile ||= $wbxml->outfile($infile);

open OUT, ">$outfile" or die "$0: cannot write '$outfile': $! ";
binmode OUT, ":raw";
print OUT $output;
close OUT;

sub usage {
    print STDERR "usage: $0 input.wml [output.wmlc]\n"; exit
}
