#!/usr/bin/perl -w
use IO::Socket;
use DBI;

my $identifier = 'isonaslogger';
my $password = 'apassword';
my $isonasServer = '10.9.1.100';

my $char = "";
my $hell = "hot";
my $frozen = "lame";
my $line = "";

# Greets

print "ISONAS Door Control Logger\n";
print "Build: 1.00.043 Andy Dixon - andy\@unified.cc\n\n";

my $sock = new IO::Socket::INET (
                                PeerAddr => $isonasServer,
                                PeerPort => '5321',
                                Proto => 'tcp',
                                );
die "Could not create socket: $!\n" unless $sock;

print "Sending Authentication String..";
print $sock "<LOGON><$identifier><$password>|";
print " Done\n";

until ($hell eq $frozen)
{
  sysread($sock, $char,1);
  $line = $line . $char;
  if ($char =~ m/\|$/) {
        if ($line =~ m/LOGON/) {
                        # Discard if retval is a login thing
                        $line = "";
                } else {
                        # check for (cr|lf)+pipe
                        chomp($line);
                        if ($line =~ m/\>\|$/) {
                                # Convert pointies to quotes 'n' commas
                                $line =~ s/\>\</\'\,\'/g;
                                $line =~ s/\</\'/g;
                                $line =~ s/\>/\'/g;

                                # Trim whitespace
                                $line =~ s/\ \'/\'/g;
                                $line =~ s/\'\ /\'/g;
                                $line =~ s/\ \'/\'/g;
                                $line =~ s/\'\ /\'/g;
                                $line =~ s/\ \'/\'/g;
                                $line =~ s/\'\ /\'/g;
                                $line =~ s/\ \'/\'/g;
                                $line =~ s/\'\ /\'/g;
                                # Remove Pipe
                                $line =~ s/\|//g;

                                print $line."\n";

                                $line="";
                        }else {
                        #discard (cr|lf)+pipe
                        $line="";
                        }
                }
        }
}

close($sock);
