#!/usr/bin/perl

use strict;
use Carp;

use lib ("/opt/rt3/lib", "/opt/rt3/local/lib");
use Text::CSV_XS;
my $csv = Text::CSV_XS->new();

use RT;
use RTx::AssetTracker;
use RTx::AssetTracker::Assets;
use RTx::AssetTracker::Asset;
use RTx::AssetTracker::Types;
use RTx::AssetTracker::Type;

RT::LoadConfig;
RTx::AssetTracker::LoadConfig;
RT::Init;

my %TypeMap = ( 7 => 'Network', 2 => 'Windows', 6 => 'Unix', 8 => 'Printers', 9 => 'Misc',
               10 => 'Telecom', 11 => 'Storage', 12 => 'Mainframe' );

my %StatusMap = ( none => 'production', Prod => 'production', Dev => 'development', Qual => 'qa',
               Pilo => 'pilot', DR => 'dr', Ret => 'retired', Test => 'test' );

my %CFMap;

my $cfs = RT::CustomFields->new($RT::SystemUser);
$cfs->UnLimit;
$cfs->LimitToLookupType('RTx::AssetTracker::Type-RTx::AssetTracker::Asset');
while (my $cf = $cfs->Next) {
       $CFMap{$cf->Name} = $cf->id;
}

my $data = shift;
open(FILE, "<$data") or die "Could not open $data: $!";
my $headers = <FILE>;
chomp $headers;
my $status = $csv->parse($headers);
my @headers = $csv->fields();

use Data::Dumper; 

while(<FILE>) {

       chomp;
       my %data = build_hash($_);
       #print Dumper(\%data); next;

       my $asset = RTx::AssetTracker::Asset->new($RT::SystemUser);
       my $type = $TypeMap{ $data{fkey_device_type_id} }; delete $data{fkey_device_type_id};
       my $name = $data{device_name} || 'no name';        delete $data{device_name};
       print "Processing $name...\n";
       my $desc = $data{description};                     delete $data{description};
       my $status = $StatusMap{ $data{prod_dev} };        delete $data{prod_dev};
       $status = $status || 'production';
       my $location = $data{location};
       #my $model = $data{model};
       my @ips = split(/\s+/, $data{IPs}); delete $data{IPs};

       my @CFargs;
       foreach my $key (keys %data) {
               next unless $data{$key};
               next unless $CFMap{$key};
               push @CFargs, "CustomField-" . $CFMap{$key}, $data{$key};
       }

       my ($id, $trans, $err) = $asset->Create(
               _RecordTransaction => 1,
               Type => $type, Name => $name,
               Description => $desc, Status => $status,
               @CFargs );
               #Model => $model, Location => $location,
               #);

       foreach my $ip (@ips) {
               my ($interface, $addr, $mac, $tcp, $udp) = split(/\|/, $ip);
               $interface = $interface || '';
               $addr = $addr || '';
               $mac = $mac || '';
               $mac =~ s/[^\w\.]//g;
               my @tcp = split(',',$tcp);
               my @udp = split(',',$udp);
               my ($rv, $msg) = $asset->AddIP(IP => $addr, Interface => $interface, MAC => $mac, TCPPorts => \@tcp, UDPPorts => \@udp, SilentPorts => 1);
               print "IP $addr not added: $msg\n" unless $rv;
       }

       unless ($id) {
               print STDERR "$status Asset $name not created: $err\n";
       }

}

sub build_hash {

       my $line = shift;
       my %hash;
       my $status = $csv->parse($line) or die "Not parsed: $_";
       my @data = $csv->fields();
       my @heads = @headers;
       while (@data) {
               my $data = shift @data;
               my $key = shift @heads;
               $hash{$key} = $data;
       }

       return %hash;

}
