# TGI perl support for TGI version 1.0 and 1.1

package TGI;

$VERSION = "1.00";
$SERVER = undef;
$PROTOCOL = undef;
$DIGITS = undef;
$QUERY = undef;
$ANI = undef;
$DNID = undef;
$CID = undef;
$PORT = "0";
$TOKEN = " ";
$PAIRS = "&";

$| = 1;

$SERVER = $ENV{'SERVER_SOFTWARE'} if $ENV{'SERVER_SOFTWARE'};
$PROTOCOL = $ENV{'SERVER_VERSION'} if $ENV{'SERVER_VERSION'};
$TOKEN = $ENV{'SERVER_TOKEN'} if $ENV{'SERVER_TOKEN'};
$PAIRS = $ENV{'SERVER_TOKEN'} if $ENV{'SERVER_TOKEN'};
$PORT = $ENV{'PORT_NUMBER'} if $ENV{'PORT_NUMBER'};
$ANI = $ENV{'PORT_ANI'} if $ENV{'PORT_ANI'};
$DNID = $ENV{'PORT_DNID'} if $ENV{'PORT_DNID'};
$DIGITS = $ENV{'PORT_DIGITS'} if $ENV{'PORT_DIGITS'};

if($ENV{'PORT_QUERY'})
{
	my($query_string) = $ENV{'PORT_QUERY'};
	my(@pairs) = split($PAIRS, $query_string);
	foreach(@pairs)
	{
		my($keyword, $value) = split('=',$_, 2);
		$QUERY{lc($keyword)} = $value;
	}
}	

if($ENV{'PORT_CID'})
{
	my($cid_string) = $ENV{'PORT_CID'};
	my(@pairs) = split($PAIRS, $cid_string);
	foreach(@pairs)
	{
		my($keyword, $value) = split('=', $_, 2);
		$CID{lc($keyword)} = $value;
	}
}		

if ($ENV{'SERVER_RUNTIME'})
{
	chdir $ENV{'SERVER_RUNTIME'};
	$TempFile::TMPDIRECTORY = "$ENV{'SERVER_RUNTIME'}/temp";
};

1;

sub set
{
	my($self) = @_;
	my($variable) = shift;
	my($value) = shift;
	
	print "SET$TOKEN$PORT$TOKEN$variable$TOKEN$value\n";
};

sub put
{
	my($self) = @_;
	my($variable) = shift;
	my($value) = shift;

	print "PUT$TOKEN$variable$TOKEN$value\n";
};

sub down
{
	print "DOWN\n";
}

sub idle
{
	print "IDLE$TOKEN$PORT\n";
}

sub busy
{
	print "BUSY$TOKEN$PORT\n";
}

sub compile
{
	print "COMPILE\n";
}

sub exit
{
	my($exitcode) = shift;
	my($pid) = getpid();
	print "EXIT$TOKEN$pid$TOKEN$exitcode\n";
}

