#!/usr/bin/perl
use strict;
use warnings;

use IO::Socket;
use POSIX qw(strftime);

# This script enables the user to test the changes to sip summary handling in bug 10817
# Set up the values in server to reflect the sipserver you are connecting to and
# the values in conf to reflect your test user and environment
# When run the script should connect to the server and sends a patron 
# info request for each valid value of summary, the requests and the servers
# responses are written to stdout
# see comments above each request for the item fields returned

my $server = {
    host => 'localhost',
    port => 6001,
};
my $conf = {
    login_user => 'SIPTEST',
    login_password => 'SIPK1',
    location => 'CPL',
    institution_id => 'CPL',
    patron_id => '23529001223638',
    terminal_password => 'sipuser',
    patron_password => 'TestPW',
};


my $sequence = 0;
my $login_message = [ "9300CN$conf->{login_user}|CO$conf->{login_password}|CP$conf->{location}|" ];
my $terminator = chr(0x0d);
my $date;
my @messages = ($login_message,
    # No individual items
    [ '63001', 'DATE',  "          AO$conf->{location}|AA$conf->{patron_id}|AC$conf->{terminal_password}|AD$conf->{patron_password}|BP1|BQ100|"],
    # hold items AS
    [ '63001', 'DATE',  "Y         AO$conf->{location}|AA$conf->{patron_id}|AC$conf->{terminal_password}|AD$conf->{patron_password}|BP1|BQ100|"],
    # overdue items AT
    [ '63001', 'DATE',  " Y        AO$conf->{location}|AA$conf->{patron_id}|AC$conf->{terminal_password}|AD$conf->{patron_password}|BP1|BQ100|"],
    # charged items AU
    [ '63001', 'DATE',  "  Y       AO$conf->{location}|AA$conf->{patron_id}|AC$conf->{terminal_password}|AD$conf->{patron_password}|BP1|BQ100|"],
    # fine items AV
    [ '63001', 'DATE',  "   Y      AO$conf->{location}|AA$conf->{patron_id}|AC$conf->{terminal_password}|AD$conf->{patron_password}|BP1|BQ100|"],
    # recall items BU
    [ '63001', 'DATE',  "    Y     AO$conf->{location}|AA$conf->{patron_id}|AC$conf->{terminal_password}|AD$conf->{patron_password}|BP1|BQ100|"],
    # unavailable holds CD
    [ '63001', 'DATE',  "     Y    AO$conf->{location}|AA$conf->{patron_id}|AC$conf->{terminal_password}|AD$conf->{patron_password}|BP1|BQ100|"],
    [ '9900302.00' ],
);

my $socket = IO::Socket::INET->new(
    PeerAddr => $server->{host},
	PeerPort => $server->{port},
	Proto    => 'tcp',
	Type     => SOCK_STREAM)
    or die "Could not connect : $@";
$socket->autoflush(1);
for my $m_arr (@messages) {
    my $req = q{};
    for my $chunk ( @{$m_arr} ) {
        if ($chunk eq 'DATE') {
            $req .= sipdatetime();
        }
        else {
            $req .= $chunk;
        }
    }

    $req .= add_checksum($req);
    print "Request:$req\n";
    $req .= $terminator;
    $socket->send($req);
    my $response;
    $socket->recv($response, 1024);
    $response=~s/\x0d/[0d]/g;
    $response=~s/\x0a/[0a]/g;
    print "Response:$response\n";
}


close $socket;

sub add_checksum {
    my $msg_buf = shift;
    my $seq = "AY$sequence" . 'AZ';
    ++$sequence;
    if ($sequence == 10) {
        $sequence = 0;
    }
    $msg_buf .= $seq;
    my $cksum = checksum($msg_buf);
    $seq .= sprintf '%04.4X', $cksum;

    return $seq;

}
sub checksum {
    my $pkt = shift;
    return (-unpack('%16U*', $pkt) & 0xFFFF);
}

sub sipdatetime {
    my $now_string = strftime '%Y%m%d    %H%M%S', localtime;
    return $now_string;
}

