#!/usr/local/bin/perl 

use strict;
require Spreadsheet::WriteExcel;

# Function Prototypes
sub process_options();
sub read_input_file($);
sub put_data_in_sheet();
sub cleanup();
sub print_help();

# Global Variables
$main::input_file = "";
$main::output_file = "";
$main::title = "";
$main::seperator = "";
@main::data = ();
$main::col = 0;
$main::row = 0;

# Process the command line options
process_options();

# Excel spreadsheet definitions;
$main::workbook = Spreadsheet::WriteExcel->new($main::output_file);
$main::worksheet = $main::workbook->addworksheet("$main::title");
$main::num_format = $main::workbook->addformat(num_format => '0.00');
$main::num_format->set_align('center');
$main::txt_format = $main::workbook->addformat();
$main::txt_format->set_align('center');


# Here is the main procedure.
{
    read_input_file($main::input_file);
    put_data_in_sheet();
    cleanup();
    exit;
}

# Place the data in the spreadsheet
sub put_data_in_sheet() {
    my $colct = 0;
    foreach (@main::data) {
        chomp;
        my @row = ();
        if ( $main::seperator eq "" ) { @row = split; }
        else                          { @row = split /$main::seperator/; }
        foreach (@row) {
           chomp;
           s/^\s+//;
           s/\s+$//;
           if ( /^([+-]?)(?=\d|\.\d)\d*(\.\d*)?([Ee]([+-]?\d+))?$/ ) { $main::worksheet->write($main::row,$main::col,$_,$main::num_format); }
           else { $main::worksheet->write($main::row,$main::col,$_,$main::txt_format); }
           $main::col = $main::col + 1;
        }
        $main::row = $main::row + 1;
        $main::col = 0;
    }
    return 0;
}

# Read the input file into an array.
sub read_input_file($) {
    my $input_file = $_[0];
    open (INPUTF,$input_file) || die ("Unable To Open Input File: $input_file");
    @main::data = <INPUTF>;
    close (INPUTF);
    return 0;
}

# This routine parses the command line options.
sub process_options() {
    my $argcnt = 0;
    my $argmax = @ARGV;
    if ( $argmax == 0 ) { print_help(); exit; }
    while ( $argcnt <= $argmax ) {
        my $arg = $ARGV[$argcnt];
        if    ( $arg eq "-input" )  { $argcnt = $argcnt + 1; $main::input_file = $ARGV[$argcnt]; }
        elsif ( $arg eq "-output" ) { $argcnt = $argcnt + 1; $main::output_file = $ARGV[$argcnt]; }
        elsif ( $arg eq "-title" )  { $argcnt = $argcnt + 1; $main::title = $ARGV[$argcnt]; }
        elsif ( $arg eq "-sep" )    { $argcnt = $argcnt + 1; $main::seperator = $ARGV[$argcnt]; }
        elsif ( $arg eq "-h" )      { print_help(); exit; }
        $argcnt = $argcnt + 1;
    }
    return 0;
}

# This routine prints the help screen.
sub print_help() {
    print "\n";
    print "   Usage: tabl2xls  << OPTIONS >> \n";
    print "\n";
    print "   Options:\n";
    print "          -input <infile>     <== Input Filename \n";
    print "          -output <outfile>   <== Output Filename \n";
    print "          -title <title>      <== Worksheet Title \n";
    print "          -sep <seperator>    <== Field Delimeter (space by default)\n";
    print "\n";
    print "   This utility will take a flat file (delimited or not) and produce an \n";
    print "Excel spreadsheet containing a single worksheet with the data from the \n";
    print "flat file.  This is useful for processing MeasureWare data and other items\n";
    print "where you would like a quick and dirty spreadsheet for management.\n";
    print "\n";
    return 0;
}

# Cleanup routing
sub cleanup() {
    $main::workbook->close();
    return 0;
}
