#/usr/local/bin/perl
#
# -*- Perl -*-
#
# This is part of X-RUST 1.0 Beta 4
#
# Copyright (C) 1996 Craig Ruefenacht <ruefenac@cs.utah.edu> and the
#                    University of Utah, Department of Computer Scinece
#
#                    Shawn T. Amundson <amundson@cs.umn.edu> and the
#                    University of Minnesota Computer Science Department
#
# Abstract: Emulate real functions used by Rust...
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2 of the License, or
# any later version.
# 
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
# 
# You should have received a copy of the GNU General Public License
# along with the source distribution of RUST, in the license
# directory. If you don't have a copy of it, write to: 
#
# Free Software Foundation, Inc.
# 675 Mass Ave
# Cambridge, MA 02139, USA.
#

#
# $Id: xrust_emulate.pl,v 1.1.1.1 2000/05/03 19:54:39 wright Exp $
#

use FileHandle;
use IPC::Open2;
use strict;

use vars qw(%BIN);

$| = 1;

my $pid = open2( \*Reader, \*Writer, $BIN{'xrust_server'} );

#&text_me;

sub text_me {

    my @a;

    my @comment1 = ( "hello, there\n", "This is a test\n" );
    my @comment2 = ( "This is the second\n", "test i'm doing...\n" );


    @a = &do_comment("amundson",1,12,0,@comment1);
    foreach (@a) {
	print "R: $_\n";
    }
    @a = &do_comment("amundson",1,12,0,@comment2);
    foreach (@a) {
	print "R: $_\n";
    }


    if (0) {
	@a = do_assign("amundson",12,"amesbury",0);
	foreach (@a) {
	    print "R: $_\n";
	}
	@a = do_assign("amundson",12,"",0);
	foreach (@a) {
	    print "R: $_\n";
	}
    }
    

    foreach (0..-1) {
	my @array = &get_ticket_log("amundson","",11,0);
	foreach (@array) {
	    print "$_\n";
	}
    }
    
    if (0) {
	my @h_a = &get_headers("","","","",0,"","","","","",0,0);
	print "$#h_a";

	&get_headers("amundson","","","",0,"","","","","",0,"");
    }

    if (0) {
	@a = do_action("amundson",0,12,'status',"",'open',0);
	foreach (@a) {
	    print "R: $_\n";
	}
	@a = do_action("amundson",0,12,'status',"",'resolved',0);
	foreach (@a) {
	    print "R: $_\n";
	}
    }


}

sub get_headers {
    #
    # Abstract: Emulate Rust_Query::get_headers
    #

    my $caller = shift;
    my $opt_realuser = shift;
    my $user = shift;
    my $owner = shift;
    my $unowned = shift;
    my $type = shift;
    my $status = shift;
    my $priority = shift;
    my $category = shift;
    my $subject = shift;
    my $sort = shift;
    my $queue = shift;

    if (0) {
	print "USER: $user\n";
	print "OWNER: $owner\n";
	print "TYPE: $type\n";
	print "STATUS: $status\n";
	print "PRIO: $priority\n";
	print "CAT: $category\n";
	print "SUB: $subject\n";
	print "SORT: $sort\n";
	print "QUEUE: $queue\n";
    }

    print Writer "get_headers\n";

    print Writer "$user\n";
    print Writer "$owner\n";
    print Writer "$unowned\n";
    print Writer "$type\n";
    print Writer "$status\n";
    print Writer "$priority\n";
    print Writer "$category\n";
    print Writer "$subject\n";
    print Writer "$sort\n";
    print Writer "$queue\n";

    my @h_a;

    my $error = <Reader>;
    chop($error);
    $h_a[0][0] = $error;

    # print "Error: $error\n";
    
    my $n = <Reader>;
    chop($n);
    $h_a[0][1] = $n;

    my $i;
    my $lll;
    foreach $i (1..$n) {
	foreach $a (0..13) {
	    $lll = <Reader>;
	    chop($lll);
	    $h_a[$i][$a] = $lll;
	}
    }

    #print "RETURNED: $#h_a\n";

    return @h_a;
}

sub get_ticket_log {
    my $caller = shift;
    my $opt_realuser = shift;
    my $opt_show = shift;
    my $queue_index = shift;
 
    print Writer "get_ticket_log\n";
    
    print Writer "$opt_show\n";
    print Writer "$queue_index\n";

    my $num = <Reader>;
    chop($num);

    #print "Number: $num\n";
    my $kl;
    my @array;
    foreach (0..$num) {
	$kl = <Reader>;
	chop($kl);
	push(@array,$kl);
    }
    #print "ERR: $array[0]\n";

    return @array;
}

sub do_action {

    my $caller = shift;
    my $type = shift;
    my $opt_num = shift;
    my $opt_set = shift;
    my $opt_append = shift;
    my $opt_value = shift;
    my $queue_index = shift;

    print Writer "do_action\n";
    
    print Writer "$type\n";
    print Writer "$opt_num\n";
    print Writer "$opt_set\n";
    print Writer "$opt_append\n";
    print Writer "$opt_value\n";
    print Writer "$queue_index\n";

    my $error = <Reader>;
    chop($error);
    my $string = <Reader>;
    chop($string);

    return ($error,$string);

}

sub do_assign {

    my $caller = shift;
    my $opt_num = shift;
    my $opt_assign = shift;
    my $queue_index = shift;

    print Writer "do_assign\n";
    
    print Writer "$opt_num\n";
    print Writer "$opt_assign\n";
    print Writer "$queue_index\n";

    my $error = <Reader>;
    chop($error);
    my $string = <Reader>;
    chop($string);

    return ($error,$string);

    
}

sub do_comment {

    my $caller = shift;
    my $type = shift;
    my $opt_num = shift;
    my $queue_index = shift;
    my @comments = @_;

    print Writer "do_comment\n";
    
    print Writer "$type\n";
    print Writer "$opt_num\n";
    print Writer "$queue_index\n";
    print Writer "$#comments\n";

    foreach (@comments) {
	print Writer $_;
    }

    my $error = <Reader>;
    chop($error);
    my $string = <Reader>;
    chop($string);

    return ($error, $string);
}

sub do_merge {

    my $caller = shift;
    my $opt_num1 = shift;
    my $opt_num2 = shift;
    my $queue_index = shift;

    print Writer "do_merge\n";
    
    print Writer "$caller\n";
    print Writer "$opt_num1\n";
    print Writer "$opt_num2\n";
    print Writer "$queue_index\n";

    my $error = <Reader>;
    chop($error);
    my $string = <Reader>;
    chop($string);

    return ($error, $string);
}

1;

