#!/usr/local/bin/perl -w

use Getopt::Long;
use English;
use strict;

my @importers = qw(xml ppr);

sub usage($)
{   my $error = shift;
    print <<USAGE;
$PROGRAM_NAME [-warn|-nowarn] [-help|-h]
              [-xml|-ppr] [file]
USAGE

    exit $error;
}

my %option = (warn => 1, help => 0);

GetOptions( \%option
          , 'warn!', 'help|h'
          , map {"$_:s"} @importers
          );

if(@ARGV>1)
{   warn "ERROR: Only one filename can be specified.\n";
    usage 1;
}

usage 0 if $option{help};

#
# Determine the type of input-file.
#

my ($type, $file);
foreach (@importers)
{   next unless exists $option{$_};
    $type = $_;
    $file = $option{$_} || '-';
    last;
}

($type, $file) = ('ppr', '-')
    unless $type || @ARGV;

unless($type)
{   $file      = $ARGV[0];

    if(($type) = $file =~ /\.(...)$/)
    {   unless(grep {$type eq $_} @importers)
        {   warn <<ERROR;
ERROR: Unknown extention $type for $file.
       Select from @importers.
ERROR

            usage 1;
        }
    }
}

unless($type)
{   foreach (@importers)
    {   next unless -f "$file.$_";
        $type = $_;
        last;
    }
}

$type  = 'ppr'       unless $type;
$file .= '.' . $type if ! -f $file && -f "$file.$type";

#
# Check for the file's existence.
#

die "Cannot find input file $file\n"
    unless $file eq '-' || -r $file;

if($type eq 'ppr')
{   system '/usr/local/bin/perl'
         , ($option{warn} ? ('-w') : (''))
         , $file;
}
elsif($type eq 'xml')
{   warn "XML may come soon :)\n";
}

