/*
 * 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 set for the shellform application
 *
 * db_access -db dbname -o filename number
 *		dbname - name of the database
 *		filename - file returning the ticket field values
 *		number - ticket number
 *
 *       Author:         Tom Sandoski
 *       Released:       04/30/92
 *
 ************************************************************************/

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

main(argc, argv)
int	argc;
char	*argv[];
{
	char		*dbname;
	char		*outfile;
	char		*number;
	int		fprintf();
	int		i, n;

	char		buf[80];
	char		*pbuf = buf;
	char		*ptoken;
	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, "d_number=\"%s\"\n", PQgetvalue(p,0,0));
	fprintf(fd, "d_status=\"%s\"\n", PQgetvalue(p,0,1));
	fprintf(fd, "d_scope=\"%s\"\n", PQgetvalue(p,0,2));
	fprintf(fd, "d_site=\"%s\"\n", PQgetvalue(p,0,3));
	fprintf(fd, "d_line=\"%s\"\n", PQgetvalue(p,0,4));
	fprintf(fd, "d_source=\"%s\"\n", PQgetvalue(p,0,5));
	fprintf(fd, "d_type=\"%s\"\n", PQgetvalue(p,0,6));
	fprintf(fd, "d_priority=\"%s\"\n", PQgetvalue(p,0,7));
	fprintf(fd, "d_clcode=\"%s\"\n", PQgetvalue(p,0,8));
	fprintf(fd, "d_owner=\"%s\"\n", PQgetvalue(p,0,9));
	fprintf(fd, "d_restime=\"%s\"\n", PQgetvalue(p,0,12));
	fprintf(fd, "d_start=\"%s\"\n", PQgetvalue(p,0,13));
	fprintf(fd, "d_end=\"%s\"\n", PQgetvalue(p,0,14));
	fprintf(fd, "d_alarm=\"%s\"\n", PQgetvalue(p,0,16));
	fprintf(fd, "d_contact=\"%s\"\n", PQgetvalue(p,0,17));
	fprintf(fd, "d_addinfo=\"%s\"\n", PQgetvalue(p,0,20));

/* Need to break out individual lines for shellforms. Limit of 4     */
/* lines and 63 characters per line since shellforms will break if   */
/* the entry exceeds the field width.                                */
/* NOTE - the Xwindow ticket manager can handle any length, so there */
/*        may actually be this kind of too long entry                */

	i = 1;
	strcpy(pbuf, "\"");
	result = PQgetvalue(p,0,18);
	ptoken = strtok(result, " \n");
	while (i<5) {
		if ((ptoken == 0) || ((strlen(pbuf) + strlen(ptoken)) > 63)) {
			strcat(pbuf, "\"");
			fprintf(fd, "d_pline%.1d=%s\n", i, pbuf);
			i++;
			strcpy(pbuf, "\"");
			continue;
		}
		else {
			strcat(pbuf, ptoken);
			strcat(pbuf, " ");
		}
		ptoken = strtok(NULL, " \n");
	}


	i = 1;
	strcpy(pbuf, "\"");
	result = PQgetvalue(p,0,19);
	ptoken = strtok(result, " \n");
	while (i<5) {
		if ((ptoken == 0) || ((strlen(pbuf) + strlen(ptoken)) > 63)) {
			strcat(pbuf, "\"");
			fprintf(fd, "d_aline%.1d=%s\n", i, pbuf);
			i++;
			strcpy(pbuf, "\"");
			continue;
		}
		else {
			strcat(pbuf, ptoken);
			strcat(pbuf, " ");
		}
		ptoken = strtok(NULL, " \n");
	}

        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, "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, "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, "d_update=\"%s\"\n", pbuf);



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

	exit(0);
}
