#!/usr/bin/perl -w

use warnings;
use strict;
use IPC::Cmd 0.36 qw( can_run run );
use XML::Writer;
use Getopt::Long qw( GetOptions :config auto_help auto_version gnu_getopt );
use Pod::Usage qw( pod2usage );

my $inputPath = undef;
my $outputPath = "photcodes.xml";

# get user args
GetOptions(
        'input|i=s' => \$inputPath,
        ) or pod2usage( 2 );

pod2usage( -msg => "Unknown option: @ARGV", -exitval => 2 ) if @ARGV;

# tell off user for not providing proper args
pod2usage(
        -msg => "\n   Required options:\n\n".
        "--input <path to dvo.photcodes files>\n".
        -exitval => 3
        ) unless
defined $inputPath;



my $output = new IO::File(">$outputPath");
my $writer = new XML::Writer(OUTPUT => $output, DATA_MODE => 1, DATA_INDENT=>2);
$writer->xmlDecl('UTF-8');

$writer->startTag('table', "name" => "PhotoCal");

open (PHOTCODES, $inputPath);
my $photCode;
my $zeroPoint;
my $filter;
my $description;

while (<PHOTCODES>) {

    chomp;

    if ($_ =~ m/\s+([0-9]+)\s+GPC1.*/) {

        my @columns = split(/\s+/, $_);

        my $filter = $columns[11];
        my $description = $columns[2];
        my $photCode = $columns[1];
        my $zeroPoint = $columns[4];

        $writer->startTag('row',
                "photoCalID" => $photCode,
                "filterID" => $filter,
                "photoCodeDesc" => $description,
                "AB" => "0.0", # TODO
                "zeropoint" => $zeroPoint,
                "extinction" => "0.0", # TODO
                "colorterm" => "0.0", # TODO
                "colorExtn" => "0.0", # TODO
                "orphanCalColor" => "0.0", # TODO
                "orphanCalColorErr" => "0.0", # TODO
                "startDate" => "54000.");

        $writer->endTag();



    }
}

$writer->endTag();


close PHOTCODES;

# finish up XML
#$writer->endTag();
$writer->end();


