/* 
 * PGTKM --  Author: Ray R. Larson  - version 0.8, Sept. 1993
 *
 *	PGTKM is a replacement for the Postgres monitor program that uses
 *      John Ousterhout's Tcl and TK libraries to provide a more complete
 *      application building tool for Postgres under the X window system.
 *
 *      Version Info:
 *             v. 0.1 (Nov. 1991) - first cut - based on the wish program.
 *             v. 0.2 (April 1992) - WDB maps added
 *             v. 0.3 (May 1992) - version 2.0 of Tk
 *             v. 0.4 (Aug 1992) - Inversion Large Object support
 *                                 WDB maps and text docs stored as POSTGRES
 *                                 large objects.
 *             v. 0.5 (Feb 1993) - Check name used for invocation and
 *                                 source all interface tcl code from the
 *                                 database.
 *             v. 0.6 (Mar 1993) - split functions into additional modules.
 *             v. 0.7 (Apr 1993) - Added pgquery commands - removed old junk
 *             v. 0.8 (Sep 1993) - Further modularized and using AppInit 
 *                                 from tcl 7.0 and tk 3.3
 *             v. 0.9 (Feb 1994) - converted to tcl 7.3 and tk 3.6
 *
 * Copyright (c) 1993 The Regents of the University of California.
 * All rights reserved.
 *
 * Permission is hereby granted, without written agreement and without
 * license or royalty fees, to use, copy, modify, and distribute this
 * software and its documentation for any purpose, provided that the
 * above copyright notice and the following two paragraphs appear in
 * all copies of this software.
 * 
 * IN NO EVENT SHALL THE UNIVERSITY OF CALIFORNIA BE LIABLE TO ANY PARTY FOR
 * DIRECT, INDIRECT, SPECIAL, INCIDENTAL, OR CONSEQUENTIAL DAMAGES ARISING OUT
 * OF THE USE OF THIS SOFTWARE AND ITS DOCUMENTATION, EVEN IF THE UNIVERSITY OF
 * CALIFORNIA HAS BEEN ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 *
 * THE UNIVERSITY OF CALIFORNIA SPECIFICALLY DISCLAIMS ANY WARRANTIES,
 * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY
 * AND FITNESS FOR A PARTICULAR PURPOSE.  THE SOFTWARE PROVIDED HEREUNDER IS
 * ON AN "AS IS" BASIS, AND THE UNIVERSITY OF CALIFORNIA HAS NO OBLIGATION TO
 * PROVIDE MAINTENANCE, SUPPORT, UPDATES, ENHANCEMENTS, OR MODIFICATIONS.
 */

#include "tk.h"

/*
 * The following variable is a special hack that allows applications
 * to be linked using the procedure "main" from the Tk library.  The
 * variable generates a reference to "main", which causes main to
 * be brought in from the library (and all of Tk and Tcl with it).
 */

extern int main();
int *tclDummyMainPtr = (int *) main;


/* Variables declared in tk main() */

static Tcl_Interp *interp; /* Interpreter for application. */
static Tk_Window mainWindow; /* Main window */
static char *geometry;
extern char *dbname;
static char *fileName;

/* the following are set when the program is to have a special */
/* name that will invoke a particular database with stored TCL */
#define DBAPPNAME "lassen"
#define APPNAMESTART 1



/*
 *----------------------------------------------------------------------
 *
 * Tcl_AppInit --
 *
 *	This procedure performs application-specific initialization.
 *      In the case of PGTKM this may involve running a startup
 *      script or accessing the startup files from a database. 
 *      Command line args for initializing POSTGRES are parsed
 *      and acted upon here also (via DB_ParseArgs).
 *
 * Results:
 *	Returns a standard Tcl completion code, and leaves an error
 *	message in interp->result if an error occurs.
 *
 * Side effects:
 *	Depends on the startup script.
 *
 *----------------------------------------------------------------------
 */

int
Tcl_AppInit(interp)
    Tcl_Interp *interp;		/* Interpreter for application. */
{
    Tk_Window mainwin;
    char *commandname;
    
    mainwin = Tk_MainWindow(interp);

    if (geometry == NULL) 
      geometry = "-0+0";


    /* Load the application specific commands */
    LoadPGTKMCmds(interp);

    /*
     * Call DB_ParseArgs to handle the POSTGRES specific command line
     * options, such as the name of the database, etc.
     *
     */
    DB_ParseArgs(interp);

    if (Tcl_VarEval(interp,"set commandname [file tail $argv0] ", 
		      (char *)NULL) != TCL_OK) {
      fprintf(stderr, "couldn't extract command name...\n");
      exit (1);
      }

    commandname = Tcl_GetVar(interp, "commandname", TCL_GLOBAL_ONLY);

    if (strcmp(commandname, DBAPPNAME) == 0) {
      /* if the app-name is the right one, load the libraries from */
      /* the database                                              */
      PQsetdb (dbname);
     /*  if (Tcl_VarEval(interp,"proc source {name} { PQsource $name }",
		      (char *)NULL) != TCL_OK) { */

      if (Tcl_VarEval(interp,"PQsource /PGTclBoot",
		      (char *)NULL) != TCL_OK) {
	char *msg;
	msg = Tcl_GetVar(interp, "errorInfo", TCL_GLOBAL_ONLY);
	if (msg == NULL) {
	  msg = interp->result;
	}
	fprintf(stderr, "%s\n", msg);
	return TCL_ERROR;
      }
 /*      Tcl_SetVar(interp, "tk_version", "7.0", TCL_GLOBAL_ONLY); */
      
 /*      fileName = "/PGTclBoot"; */
    }
    else {
      /* otherwise use the usual tcl/tk libraries from the usual places */
      if (Tcl_Init(interp) == TCL_ERROR) {
	return TCL_ERROR;
      }
      if (Tk_Init(interp) == TCL_ERROR) {
	return TCL_ERROR;
      }
      /*
       * Specify a user-specific startup file to invoke if the application
       * is run interactively.  Typically the startup file is "~/.apprc"
       * where "app" is the name of the application.  If this line is deleted
       * then no user-specific startup file will be run under any conditions.
       */

      tcl_RcFileName = "~/.PGTKMrc";

    }

    return TCL_OK;
}



