#!/usr/bin/perl -w 
#
# Zymonic Business Process and Information Management System
# Copyright Zednax Limited 2008 -
# For Authors and Changelog see the subversion history
use strict;

package main;

use Zymonic::Decryptor::Client;
use Zymonic::Config;
use Data::Dumper;
use Zymonic;
$Zymonic::system  = '';
$Zymonic::session = '';

BEGIN
{
    use Zymonic::Utils qw(death_handler);
    $main::SIG{__DIE__} = \&death_handler;
}

$Zymonic::system = $ARGV[0];
my $pings      = $ARGV[1] || 1;
my $sequential = $ARGV[2] || 0;
my $data_cap   = $ARGV[3] || 'N';
my $all        = $ARGV[4] || '';
my $no_key     = $ARGV[5] || '';

unless ($Zymonic::system)
{
    print
"Usage: decryptor_ping.pl [system] [pings to send (optional)] [sequential mode - 0 or 1 (optional)] [capture data to encrypt Y/N or existing id] [all] [no key]\n";
    exit(0);
}

# Capture DATA to encrypt
if ( $data_cap =~ /[Yy]/i )
{
    print "DATA TO ENCRYPT: ";
    my $indata = <STDIN>;
    $Zymonic::ZCONFIG{$Zymonic::system} =
      Zymonic::Config->new( system_name => $Zymonic::system, config_dir => "/etc/zymonic" );
    my $zdc = Zymonic::Decryptor::Client->new(
        config => $Zymonic::ZCONFIG{$Zymonic::system},
        db     => $Zymonic::ZCONFIG{$Zymonic::system}->{DB}
    );
    $data_cap = $zdc->encrypt_card_number( $indata, 1 )->{encrypted_ref};
}

my @childpids = ();
foreach my $ping ( 1 .. $pings )
{
    my $child;

    unless ($sequential)
    {
        $child = fork();
    }

    if ($child)
    {
        push( @childpids, $child );
    }
    else
    {
        $Zymonic::ZCONFIG{$Zymonic::system} =
          Zymonic::Config->new( system_name => $Zymonic::system, config_dir => "/etc/zymonic" );
        my $zdc = Zymonic::Decryptor::Client->new(
            config => $Zymonic::ZCONFIG{$Zymonic::system},
            db     => $Zymonic::ZCONFIG{$Zymonic::system}->{DB}
        );

        my @decryptors = $zdc->get_decryptors( 0, $no_key );

        @decryptors = ( $decryptors[0] ) unless $all;

        foreach my $decryptor (@decryptors)
        {

            print "DECRYPTOR: $decryptor->{hostname}:$decryptor->{port} " . "PING: " . $ping . "\n";
            my $response;
            eval {

                $response = $zdc->call_decryptor(
                    {
                        ping => 'ping sequence ' . $ping,
                        data => ( $data_cap =~ /^\d+$/ ) ? 'ENCRYPTED' . $data_cap . 'ENCRYPTED' : 'NO DATA'
                    },
                    '', 'nokey', undef,
                    $decryptor
                );
                1;
            }
              or do
            {
                my $exception = $@;
                if ($exception)
                {
                    death_handler( $exception, '', 'return_error' );
                    print "\n";
                }
            };
            print " "
              . ( ( $response->{content} || '' ) eq 'PING!' ? 'OK' : 'NOT OK' )
              . " - DATA : "
              . ( $response->{incoming_was}->{data} || '' ) . "\n";
        }

        $Zymonic::ZCONFIG{$Zymonic::system}->{DB}->close_db_connection;
        if ( defined $child )
        {
            exit(0);
        }
    }

}

foreach (@childpids) { waitpid( $_, 0 ); }
