#!/usr/bin/perl -I.

#
# This is a simple "watch" program using hf-event.pl
# You can easly add/change any of the events you want watched.
# You can esaily change the functions called
# You can esily change what info is passed to JOB/SEND functions
#

use Net::FTP;
use strict;

require "hf-event.pl";

# These are just some dumb functions to show what's possible

sub job_create
{
    my ($tag, $id, $pages) = @_;
    print "*JOB_CREATE* JOBID $id TAG '$tag' PAGES $pages)\n";
}

sub job_send
{
    my ($tag, $dials) = @_;
    print "*JOB_SEND* TAG '$tag' DIALS $dials \n";
}

sub job_finish
{
    my ($tag, $dials, $commid, $npages, $status) = @_;
    print "*JOB_FINISH* TAG '$tag' DIALS $dials COMMID $commid NPAGES $npages STATUS '$status'\n";
}

sub job_dead
{
    my ($tag, $state, $status) = @_;
    print "*JOB_DEAD* TAG $tag STATE $state STATUS '$status'\n";
}

sub modem
{
        print "*MODEM*: " . join (', ', @_) . "\n";
}

sub job
{
        print "*JOB*: " . join (', ', @_) . "\n";
}

sub send
{
        print "*SEND*: " . join (', ', @_) . "\n";
}


#
# This is the main loop, which we want to "loop" in.
# If, for any reason our connection is broken, we want
# to just loop, open another one, start again
#
while (1)
{

        my $ftp = Net::FTP->new('localhost', Passive => 0, Debug => 0, Port => 4559, Timeout => 2**30)
                 or die "Cannot connect to server: $@";


        $ftp->login('monitor');
	$ftp->site('LOCKWAIT', 10);

        watch($ftp,
#                        JOB_CREATE => [\&job_create, 'JOB:JOBINFO', 'JOB:TOTPAGES'],
#                        SEND_BEGIN => [\&job_send, 'JOB:JOBINFO', 'JOB:NDIALS'],
#                        SEND_END => [\&job_finish, 'JOB:JOBINFO', 'JOB:NDIALS', 'JOB:COMMID', 'JOB:NPAGES', 'JOB:STATUS'],
#                        JOB_DEAD => [\&job_dead, 'JOB:JOBINFO', 'JOB:STATE', 'JOB:STATUS'],
#                        MODEM => \&modem,
                        JOB => [\&job, 'ID', 'JOB:JOBINFO', 'JOB:STATE', 'JOB:STATUS'],
#                        JOB_ACTIVE => [\&job, 'ID', 'JOB:JOBINFO', 'JOB:STATE', 'JOB:STATUS'],
#                        JOB_DEAD => [\&job, 'ID', 'JOB:JOBINFO', 'JOB:STATE', 'JOB:STATUS'],
#                        SEND_BEGIN => [\&job, 'ID', 'JOB:JOBINFO', 'JOB:STATE', 'JOB:STATUS'],
#                        SEND_END => [\&job, 'ID', 'JOB:JOBINFO', 'JOB:STATE', 'JOB:STATUS'],
        );

}
