#!perl

use strict;
use Getopt::Std;

my $input_file = undef;
my $output_file = undef;
my $column = undef;
my $separate = ',';
my $dont_ignore_first = undef;
my $type = 0;
my @data_types = ('','"','\'');
my $show_cols = undef;

sub showUsage {
  print <<OUT;
	Usage: $0 
        -h Show this screen
        -n Show the column names in the file
        -w Separate on whitespace (default is a comma)
        -i Don't ignore first line, i.e. it contains the names of the columns
        -s Treat the data as a string, i.e. data in generated array is in double quotes
        -r Treat the data as characters, i.e. data in generated array is in quotes
        -f <in_file> Input file
        -c <id> column to include in array (can be either a number, zero based, or column name)
        -o <out_file> File array is output to (any other content will be over-written)
       
   Outputs a Java/C# array initaliser with values from column <id> from file <in_file> and
   send it out to <out_file>
OUT
  exit 0;
}

sub getArguments {
    my %Options;
    getopts("hwisrnf:c:o:", \%Options);
      
    if ($Options{'h'}){
        showUsage();
    }
     
    $input_file = $Options{'f'};
    $output_file = $Options{'o'};
    $column = $Options{'c'};
    $show_cols = $Options{'n'};
    $dont_ignore_first = $Options{'i'};
     
    if(!defined($input_file) || (!defined($show_cols) && !defined($column))) {
        showUsage();
    }
    
    if(defined($Options{'w'})) {
        $separate = "\\s+";
    }
     
    if(defined($Options{'s'})) {
        $type = 1;
    }
    
    if(defined($Options{'r'})) {
        $type = 2;
    }
    
    if(defined($column) && defined($dont_ignore_first)) {
        print "-c option cannot be used in conjunction with the -i option";
        exit;
    }

}

sub generateColumnIndex {    
    my $line = shift;
    my @indices = split(/$separate/, $line);
    for(my $i = 0; $i <= $#indices; $i++) {
        if(trim($indices[$i]) eq $column) {
            $column = $i;
            return;
        }
    }
    
    print "Column ".$column." not found on first line of file." .
          " Unable to establish index\n";
    exit;
}

sub setupInput {
    open(IN, $input_file) or die "Cannot read file ".$input_file;
    my @text = <IN>;
    close IN;
    
    if(!defined($dont_ignore_first) && !($column =~ /\d+/)) {
        generateColumnIndex($text[0]);
    }
    
    if(!defined($dont_ignore_first)) {
        shift(@text);
    }
    
    return @text;
}

# Perl trim function to remove whitespace from the start and end of the string
sub trim($)
{
	my $string = shift;
	$string =~ s/^\s+//;
	$string =~ s/\s+$//;
	return $string;
}

sub checkForShowCols {
    if(!defined($show_cols)) {
        return;    
    }
    
    open(MYFILE, $input_file) or die "Cannot read file ".$input_file;
    my $first = <MYFILE>;
    my @elems = split(/$separate/, trim($first));
    print "\n".join("   ", @elems)."\n";
    close MYFILE;
    exit;
}

sub process {
    my @text = setupInput();
    my $out ="{";
    for(my $i = 0; $i <= $#text; $i++) {
        my @elems = split(/$separate/, trim($text[$i]));
        $out .=  $data_types[$type].$elems[$column].$data_types[$type].', ';
    }

    chop $out;chop $out;
    $out .= "}";  

    if(defined($output_file)) {
        open(MYOUTFILE, '>'.$output_file);
        print MYOUTFILE $out;
        close MYOUTFILE;
        return;
    }
    
    print $out;
}

getArguments();
checkForShowCols();
process();
