#!/usr/bin/perl
# useraction.pl - Parses the values from userform.pl and adds to db
#
# Copyright 2000, 2001 OST Corporation - Released under the GPL V2.0
# See the file COPYING included with this file for license info
# If you did not receive a file named COPYING with this program,
# you can find a copy of the GPL V2.0 by e-mailing rsb@ostel.com,
# or visiting http://www.fsf.org/copyleft/gpl.html#SEC1 

use CGI ':standard';
use File::LockDir;
use SDBM_File;
use DB_File;  # yeah, we need this

print header;
print start_html('User Info');

if (param()) {
    print 
	"<B>Thank you for your interest in Dialogic and Linux!</B>",
	p,
	"A Dialogic representative will be contacting you as soon as possible.",
	p,
	"<B>The agent will be provided with the following information:</B>",
        p,
	"<I>Your name is:</I> ",em(param('name')),
	p,
	"<I>Your Daytime Phone Number is:</I> ",em(param('dayphone')),
	p,
	"<I>Your e-mail address:</I> ",em(param('email')),
	p,
	"<I>The SDK's you have used:</I> ",em(join(", ",param('sdksused'))),
	p,
	"<I>Your question is:</I> ",em(param('question')),
	hr;
    
    # Add the ticket to the database
    my($return);
    my($name)=param('name');
    my($dayphone)=param('dayphone');
    my($email)=param('email');
    my($sdksused)=param('sdksused');
    my($question)=param('question');
    # not even bothering with session ids for the demo, very kludgy
    my($sid) = customerid(); # using the customer id counter for ticket numbers, like I said, very kludgy
    $return = tie(%hash, 'SDBM_File', '/home/customer/dbases/tickets', O_RDWR, 0666);
    #print "tickets tie return $return.\n"; undef($return);
    $hash{$sid} = join(":", $name, $dayphone, $email, $sdksused, $question);
    untie %hash;
    print p;

    # Assign an agent to the ticket
    my ($agentname, $activeticket);
    $agentname = "Demonstrator";
    $activeticket = $sid;
    $return = tie(%hash, 'SDBM_File', '/home/customer/dbases/agents', O_RDWR, 0666);
    #print "agents tie return $return.\n"; undef($return);
    $hash{$agentname} = $activeticket;
    untie %hash;
    
    print p;

    # Run the bayonne fifo command
    #$return=`bayctrl start 2 join`; 

    # Provide a link to more information
    print "<a href=\"/userpage/moreinfo.shtml\">Click here for information about the demo.</a>";

print p;    
}

print end_html;

##############################################################
# customerid
#
# one way to generate unique customer ids for ivr apps.
# randomization may be faster, but the count is semi-useful info and 
# gauranteed unique unless your countfile gets blown away.
#
sub customerid{
    return count_file_increment('cid',1);
}

##############################################################
# count_file_increment
#
# crank up the count of ivrapp countfile $_[0] by $_[1]
#
sub count_file_increment {
    my($count);
    my($increment) = $_[1];
    my($countfile) = '/home/customer/counters/' . $_[0];
    my($countlock) = $countfile . 'lck';

    unless (nflock($countlock, 2)) {
        print "couldn't lock $countlock in 2 seconds.\n"; }
    unless (open(CFILE, "<$countfile")) {
	print "couldn't open <$countfile and I had the lock!.\n"; }
    $count = <CFILE>; 
    if (!$count) {
	print "couldn't read from $countfile.\n"; }
    $count++;
    unless (open(CFILE, ">$countfile")) {
	print "couldn't open >$countfile and I had the lock!.\n"; }
    unless (print CFILE $count) {
	print "couldn't write to $countfile"; }
    nunflock($countlock);    
    close(CFILE);
    return $count;
}




