#!/usr/bin/perl

# This routine will convert a tab delimited text file into an nPulse configfile
# and infofile.txt set of files.

$Version = '1.0';
$Usage = <<EOU;
Usage: $0 [-vh] [-c configfile] [-i infofile] [-k key1,key2,..] file
  file      Name of tab-delimited input text file
  -v        Tell us about it
  -h        This message
  -c file   Filename of configfile [/usr/local/npulse/etc/configfile]
  -i file   Filename of infofile.txt [/usr/local/npulse/data/infofile.txt]
  -k keys   Column names of the input file. [default: in order shown]
            Valid values are (case sensitive)
              VALUE         DESCRIPTION
              ip            numeric ip address
              portlist      list of ports to monitor
              os            Operating System
              CustomerID_1  Customer identification
              Location_2    Equipment location
              Host_3        Host location or type
              Phone_4       Telephone number
              Pager_5       Pager number
              Email_6       Email address
              Comments_7    Comments
              Alias_8       Device alias
              Icon_9        GIF icon for display
              GroupID_10    Group identification
              CustomID_11   Custom identification
EOU
@def_k = qw(ip portlist os CustomerID_1 Location_2 Host_3 Phone_4 Pager_5 Email_6 Comments_7 Alias_8 Icon_9 GroupID_10 CustomID_11);
$help = "Use $0 -h for help";

use Getopt::Std;
getopts('vhc:i:k:');
$file = shift;

print "$0 version $Version\n" if $opt_v;

if ($opt_h) { print $Usage; exit; }

$opt_c ||= '/usr/local/npulse/etc/configfile';
$opt_i ||= '/usr/local/npulse/data/infofile.txt';
$opt_k ||= join(',',@def_k);

@keys = split (/\s*,\s*/,$opt_k);
foreach $k (@keys) { die "Invalid keylist specification. $help\n" unless grep(/^$k$/,@def_k); }
die "'ip' must be one of the keys. $help\n" unless grep(/^ip$/,@keys);

die "Invalid file specification ($file). $help\n" unless ($file and open(F, "<$file"));

rename $opt_c, $opt_c.".bak" if -f $opt_c;
rename $opt_i, $opt_i.".bak" if -f $opt_i;

open(C, ">$opt_c") or die "Unable to write to configfile. $help\n";
open(I, ">$opt_i") or die "Unable to write to infofile.txt $help\n";

print "Processing keys: ",join(",",@keys),"\n" if $opt_v;
print "Processing $file ... " if $opt_v;
push(@def_k,'icon');
while (<F>) {
  chomp; undef @items; undef %Set;
  @items = split /\t/;
  next unless $#items >= 0;
  next if $items[0] =~ /^#/;
  for ($i=0;$i<=$#items;$i++) {
    $Set{$keys[$i]} = $items[$i];
  }
  $numlines++;
  die "Invalid ip on line $numlines\n" unless $Set{ip} =~ /^[0-9\.]+$/;
  print C $Set{ip}," ",$Set{portlist},"\n";
  $Set{icon} = $Set{'Icon_9'};
  foreach $k (@def_k) {
    next if $k eq 'portlist';
    print I $k,' => "',&Encode($Set{$k}),'"',($k eq $def_k[$#def_k]?"\n":", ");
  }
}
close(F); close(I); close(C);

print "$numlines line",(($numlines>1)?"s":"")," written to $opt_c and $opt_i\n" if $opt_v;
exit;

sub Encode {
  my $text = shift || return;
  $text =~ s/([^a-zA-Z0-9_\. -])/uc sprintf("%%%02x",ord($1))/eg;
  return $text;
}
