#!/usr/bin/perl -w

my $VERSION = '0.06.1';
my $COPYRIGHT = '(C) 2000, 2006, 2009 by Axel Beckert <wapua@deuxchevaux.org>';

# Copyright (C) 2000, 2006, 2009 by Axel Beckert <wapua@deuxchevaux.org>
#
#  This program is free software; you can redistribute it and/or
#  modify it under the terms of the GNU General Public License
#  as published by the Free Software Foundation; either version 2
#  of the License, or (at your option) any later version.
#
#  This program is distributed in the hope that it will be useful,
#  but WITHOUT ANY WARRANTY; without even the implied warranty of
#  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
#  GNU General Public License for more details.
#
#  You should have received a copy of the GNU General Public License
#  along with this program; if not, write to the Free Software
#  Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02111-1301, USA.
#
# You can reach the author by snail-mail at the following address:
#
#  Axel Beckert
#  Krbergstrasse 20
#  8049 Zrich, Switzerland

use wApua::WBMP2XBM;

my $verbose = 0;
my $debug = 0;

$0 =~ m([^/]+$);
my $basename = $&;

sub usage {
    &version();
    print <<EOT;

Usage: $basename [-v|--verbose] [--debug=<n>] <file>.wbmp
       $basename (-h|--help|--usage)
       $basename (-V|--version)

Will generate an X Bitmap file called <file>.xbm
EOT
}

sub version {
    print "$basename $VERSION $COPYRIGHT\n";
}

unless (@ARGV) {
    &usage();
    exit 1;
}

foreach (@ARGV) {
    if (/^(-h|--help|--usage)$/) {
	&usage();
        exit 0;
    }

    if (/^(-V|--version)$/) {
	print "$0 $VERSION  $COPYRIGHT\n";
        exit 0;
    }

    if (/^(-v|--verbose)$/) {
	$verbose = 1;
	next;
    }

    if (/^--debug=(\d+)$/) {
	$debug = $1;
	next;
    }

    my $wbmpfile = $_;
    my $filebasename = $_;
    $filebasename =~ s/\.wbmp$//i;
    my $xbmfile = $filebasename.".xbm";

    open(WBMP, '<', $wbmpfile) or die "Can't open $_ for reading";

    my $wbmp = "";
    while (<WBMP>) {
	$wbmp .= $_;
    }

    my $wbmpObj = new wApua::WBMP2XBM($wbmp,$filebasename,$debug);
    die "An error occured during conversion"
	unless $wbmpObj->xbm;

    print STDERR "Image dimension: ".$wbmpObj->dimension."\n" if $verbose;

    open(XBM, '>', $xbmfile) or die "Can't write to $xbmfile";
    print XBM $wbmpObj->xbm;

    close XBM;
    close WBMP;
}

__END__

=head1 NAME

wbmp2xbm - converts WBMP to XBM

=head1 SYNOPSIS

=over 1

=item wbmp2xbm [-v|--verbose] [--debug=I<n>] F<file.wbmp> F<...>

=item wbmp2xbm (-h|--help|--usage)

=item wbmp2xbm (-V|--version)

=back

=head1 DESCRIPTION

wbmp2xbm is a converter from WAP Wireless Bitmaps (WBMP) to X Bitmaps
(XBM) written in Perl. It uses the same converting library as the WAP
WML browser wApua.

wbmp2xbm needs at least one WBMP file as argument and will save it as
XBM file with the same basename but a .xbm suffix.

=head1 OPTIONS

=over 22

=item -h, --help, --usage

Shows a summary of options.

=item -V,  --version

Shows the version of wbmp2xbm.

=item -d I<n>, --debug=I<n>

Sets the level of debug output to I<n>.

=back

=head1 SEE ALSO

L<wApua(1p)>, L<wbmp2xpm(1)>, L<convert(1)>.

The wApua FAQ at L<http://fsinfo.noone.org/~abe/wApua/FAQ.html>

=head1 COPYRIGHT

Copyright (C) 2000, 2006, 2009 by Axel Beckert <wapua@deuxchevaux.org>

This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 2 of the License, or (at
your option) any later version.

This program is distributed in the hope that it will be useful, but
WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
General Public License for more details.

On Debian systems, the complete text of the GNU General Public License
can be found in /usr/share/common-licenses/GPL. It also came with
wApua in the file F<COPYING>.

=head1 AUTHOR

wbmp2xbm was written by Axel Beckert <wapua@deuxchevaux.org>
