#!/usr/bin/perl -w

use strict;
use DBI;
use Getopt::Std;
use Opals::Equipment qw(
     eq_record_findByRId
     eq_item_findByRId
);

my %options = ();
getopts("c:",\%options);
my $configFile = $options{c};
if (!$configFile || ! -f $configFile) {
    print "Usage: $0 -c CONFIG_FILE\n";
    exit 1;
}

my $config = loadConfig($configFile);
my $dbh = makeConnection($config);

my $header = getHeader($dbh);

foreach my $h (@$header){
    print $h->{'name'}, "  ";
}
print "\n";


END {
    if ($dbh) {
        $dbh->disconnect();
    }
}

$| = 1;
# Codes start...


# Codes end.

exit 0;
################################################################################

sub getHeader {

    my ($dbh) = @_;

    my $sql = " select name , id from eq_def where defType = 'record' order by id";
    my $sth = $dbh->prepare($sql);
    $sth->execute();
    my @header = ();
    push @header, {
        id  => 0, 
        name => 'RID'
    };

    push @header, {
        id  => 0, 
        name => 'Equipment Name'
    };

    while ( my $def = $sth->fetchrow_hashref){
         push @header, {
             id     => $def->{'id'},
             name   => $def->{'name'}
         };
    }
    $sql = " select name , id from eq_def where defType = 'item' order by id";
    $sth = $dbh->prepare($sql);
    $sth->execute();
    push @header, {
        id  => -1, 
        name => 'Barcode'
    };
    push @header, {
        id  => -2, 
        name => 'Item Type'
    };

    while ( my $def = $sth->fetchrow_hashref){
         push @header, {
             id     => $def->{'id'},
             name   => $def->{'name'}
         };
    }
    $sth->finish;
    return \@header;
}

sub getRecordInfo {
    my ($dbh ,$rid) = @_;

    my $sql = " select rid, rname from eq_record where rid = ? order by rid";
    my $sth = $dbh->prepare($sql);
    $sth->execute($rid);
    my @recs;
    while ( my $rec = $sth->fetchrow_hashref){
         push @recs, {
             rid     => $rec->{'rid'},
             rname   => $rec->{'rname'}
         };
    }







}



sub makeConnection {
    my ($config) = @_;
    if (!$config) {
        return;
    }
    my ($db_driver, $db_name, $db_host, $db_port, $db_user, $db_password);

    $db_driver   = $config->{'db_driver'} || 'mysql';
    $db_name     = $config->{'db_name'};
    $db_host     = $config->{'db_host'};
    $db_port     = $config->{'db_port'}   || '3306';
    $db_user     = $config->{'db_user'};
    $db_password = $config->{'db_password'};

    my $dsn = "dbi:$db_driver:$db_name:$db_host:$db_port";

    return DBI->connect($dsn, $db_user, $db_password);
}
############################################################


sub loadConfig {
    my ($configFile) = @_;
#    print "Enter the config filename of Opals: ";
#    $configFile = <STDIN>;
    my $config = {};

    open CONF, $configFile || die "Cannot open file $configFile";
    while (<CONF>) {
        chomp;
        s/#.*//;                # remove comments
        next if /^\s*$/;        # ignore blank lines

        if (/^\s*(\w+)\s*=\s*(.*?)\s*$/) {
            $config->{$1} = $2;
        }
    }
    close CONF;

    return $config;
} 
############################################################
