/*
 * Copyright (c) 1992 MCNC, CONCERT Network
 * All rights reserved.
 *
 * Permission to use, copy, modify, and distribute this software and
 * its documentation for any purpose and without fee is hereby granted,
 * provided that the above copyright notice appears in all copies and
 * that both the copyright notice and this permission notice appear in
 * supporting documentation.  MCNC makes no representations about the
 * suitability of this software for any purpose. It is provided "as is"
 * without express or implied warranty.
 *
 */

/************************************************************************
 * db_access
 *
 * Retrieve tickets from the postgres database.
 *
 * Given a database number for a ticket entry, returns the fields
 * in a file in a form suitable for the tt script to read. 
 * Will return the fields suitable for the X window application
 *
 * db_access -db dbname -o filename number
 *		dbname - name of the database
 *		filename - file returning the ticket field values
 *		number - database number of the ticket
 *
 *       Author:         Tom Sandoski
 *       Released:       07/13/92
 *
 ************************************************************************/

#include <stdio.h>
#include <strings.h>
#include <signal.h>
#include "libpq-fe.h"

void	db_access();

main(argc, argv)
int	argc;
char	*argv[];
{
	char		*dbname;
	char		*outfile;
	char		*number;
	int		chdir(), setjmp(), fprintf();
	char		*getenv();
	extern		exitpg();
	extern		resetmmgr();
	int		i;

	char		buf[80];
	char		*pbuf = buf;
	char		*result;
	char		*temp;
	PortalBuffer	*p;
	FILE		*fd;

	for (i=1; i<argc; i++) {
	    if (!strcmp(argv[i], "-db")) {
		i++;
		if (i >= argc) {
		    fprintf(stderr,
		    "dbaccess: '-db' must be followed by a database name\n");
		    exit(1);
		}
		dbname = argv[i];
	    }
	    if (!strcmp(argv[i], "-o")) {
		i++;
		if (i >= argc) {
		    fprintf(stderr,
		    "dbaccess: '-o' must be followed by a file name\n");
		    exit(1);
		}
		outfile = argv[i];
	    }
            number = argv[i];
	}


/* Specify the database to access */
	PQsetdb(dbname);

/* Start a transaction sequence */
	PQexec("begin");

/* Retrieve the ticket into a portal */
        sprintf(pbuf, "retrieve portal dbportal (ticket.all) where ticket.number=\"%s\"", number);
	result=PQexec(pbuf);
	if (strcmp(result, "CRETRIEVE")) {
		fprintf(stderr, "Retrieve error, result=%s \n", result);
		exit(1);
		}
	result=PQexec("fetch all in dbportal");
	if (strcmp(result, "Pdbportal")) {
		fprintf(stderr, "Fetch error, result=%s \n", result);
		exit(1);
		}
	p=PQparray("dbportal");

/* Get the individual field values from the ticket */
/* and set up a file for passing them back to the tt script */
        if ((fd = fopen (outfile, "w")) == NULL) {
                fprintf (stderr, "can not open file %s for write\r\n", outfile);
		exit(1);
	}
	fprintf(fd, "set d_number \"%s\"\n", PQgetvalue(p,0,0));
	fprintf(fd, "set d_status \"%s\"\n", PQgetvalue(p,0,1));
	fprintf(fd, "set d_scope \"%s\"\n", PQgetvalue(p,0,2));
	fprintf(fd, "set d_site \"%s\"\n", PQgetvalue(p,0,3));
	fprintf(fd, "set d_line \"%s\"\n", PQgetvalue(p,0,4));
	fprintf(fd, "set d_source \"%s\"\n", PQgetvalue(p,0,5));
	fprintf(fd, "set d_type \"%s\"\n", PQgetvalue(p,0,6));
	fprintf(fd, "set d_priority \"%s\"\n", PQgetvalue(p,0,7));
	fprintf(fd, "set d_clcode \"%s\"\n", PQgetvalue(p,0,8));
	fprintf(fd, "set d_owner \"%s\"\n", PQgetvalue(p,0,9));
	fprintf(fd, "set d_restime \"%s\"\n", PQgetvalue(p,0,12));
	fprintf(fd, "set d_start \"%s\"\n", PQgetvalue(p,0,13));
	fprintf(fd, "set d_end \"%s\"\n", PQgetvalue(p,0,14));
	fprintf(fd, "set d_alarm \"%s\"\n", PQgetvalue(p,0,16));
	fprintf(fd, "set d_contact \"%s\"\n", PQgetvalue(p,0,17));
	fprintf(fd, "set d_pline \"%s\"\n", PQgetvalue(p,0,18));
	fprintf(fd, "set d_aline \"%s\"\n", PQgetvalue(p,0,19));
	fprintf(fd, "set d_addinfo \"%s\"\n", PQgetvalue(p,0,20));

	sprintf(pbuf, "");
	result = PQgetvalue(p,0,10);
	temp = strtok(result, " ");
	for (i=1; i < 5; i++) {
		temp = strtok(NULL, " "); 
		strcat(pbuf, temp);
		strcat(pbuf, " ");
	}
	fprintf(fd, "set d_opened \"%s\"\n", pbuf);

	sprintf(pbuf, "");
	result = PQgetvalue(p,0,11);
	if (strcmp(result, "epoch")) {
		temp = strtok(result, " ");
        	for (i=1; i < 5; i++) {
                	temp = strtok(NULL, " ");
                	strcat(pbuf, temp);
                	strcat(pbuf, " ");
        	}
	}
	fprintf(fd, "set d_closed \"%s\"\n", pbuf);

	sprintf(pbuf, "");
	result = PQgetvalue(p,0,15);
	if (strcmp(result, "epoch")) {
		temp = strtok(result, " ");
        	for (i=1; i < 5; i++) {
                	temp = strtok(NULL, " ");
                	strcat(pbuf, temp);
                	strcat(pbuf, " ");
        	}
	}
	fprintf(fd, "set d_update \"%s\"\n", pbuf);

/* End the transaction sequence */
	PQexec("end");

	exit(0);
}
