/*
 * Copyright 1990 by Baylor College of Medicine ALL RIGHTS RESERVED. 
 *
 * This program is subject to a license agreement between 
 * Baylor College of Medicine and MIT. Any use inconsistent with
 * said license and any use by persons other than the faculty, 
 * students and staff at MIT or any use on a computer not operated 
 * as part of the Athena Computing Environment (ACE) is expressly 
 * prohibited.
 */
#include <stdio.h>
#include <string.h>
#include <X11/StringDefs.h>
#include <X11/Intrinsic.h>
#include <X11/cursorfont.h>
#include <X11/Shell.h>
#include <srvlib.h>

#include "VnsP.h"

typedef struct
{
	String db;                  /* database name */
	String server;              /* server name or host name */
	int port;                   /* port number */
	String site_printer_file;   /* site printer file path */
	String default_font;        /* default font name */
	Boolean help;               /* help flag */
} ApplicationData, *ApplicationDataPtr;

static XtResource resources[] = {
	{"defaultFont",
		"DefaultFont",
		XtRString,
		sizeof(String),
		XtOffset(ApplicationDataPtr,default_font),
		XtRString,
		NULL},
	{"database",
		"Database",
		XtRString,
		sizeof(String),
		XtOffset(ApplicationDataPtr,db),
		XtRString,
		NULL},
	{"server",
		"Server",
		XtRString,
		sizeof(String),
		XtOffset(ApplicationDataPtr,server),
		XtRString,
		NULL},
	{"port",
		"Port",
		XtRInt,
		sizeof(int),
		XtOffset(ApplicationDataPtr,port),
		XtRImmediate,
		(caddr_t) 6721},
	{"sitePrinterFile",
		"SitePrinterFile",
		XtRString,
		sizeof(String),
		XtOffset(ApplicationDataPtr,site_printer_file),
		XtRString,
		NULL},
	{"help",
		"Help",
		XtRBoolean,
		sizeof(Boolean),
		XtOffset(ApplicationDataPtr,help),
		XtRImmediate,
		(caddr_t) FALSE},
};

static XrmOptionDescRec options[] = {
	{"-database",  "*database",  XrmoptionSepArg,  NULL},
	{"-server",    "*server",    XrmoptionSepArg,  NULL},
	{"-port",      "*port",      XrmoptionSepArg,  NULL},
	{"-help",      "*help",      XrmoptionNoArg,   "True"},
};

static
void
Usage(argc,argv)
	int argc;
	char *argv[];
{
	int i;
	static int errs = False;

	for (i = 1; i < argc; i++)
	{
		if (!errs++)
		{
			fprintf(stderr,"vns: command line option unknown:\n");
		}
		fprintf(stderr,"option: %s\n",argv[i]);
	}

	fprintf(stderr,"vns understands all standard X command-line arguments\n");
	fprintf(stderr,"and the following additional arguments:\n");
	fprintf(stderr,"Option           Parameter\n");
	fprintf(stderr,"-server          server name\n");
	fprintf(stderr,"-database        database name\n");
	fprintf(stderr,"-port            port number\n");
	fprintf(stderr,"-help            no value necessary\n");
}

vns_default_top_level(vns,argc,argv)
	VnsContext *vns;
	int argc;
	char *argv[];
{
	XtAppContext app_context;
	char title[40];

	sprintf(title,"Virtual Notebook System (vns%d.%d.%d)",
		VNS_MAJOR_VERSION,VNS_MINOR_VERSION,VNS_RELEASE);

	/* Initialize X */
	vns->toplevel = XtVaAppInitialize(&app_context,"Mvns",
		options,XtNumber(options),&argc,argv,
		(char **)NULL,
		XtNtitle,title,
		XtNgeometry,vns->session->panel_geom,
		XtNallowShellResize,True,
		NULL);
	if (vns->toplevel == NULL)
	{
		fprintf(stderr,"Error: cannot communicate with X server.\n");
		exit(-1);
	}
	vns->dpy = XtDisplay(vns->toplevel);

	/* for any left over arguments, only warn the user and display usage */
	if (argc > 1)
	{
		Usage(argc,argv);
		fprintf(stderr,"continuing execution of the VNS...\n");
	}
}

vns_init(vns,argc,argv)
	VnsContext *vns;
	int argc;
	char *argv[];
{
	static ApplicationData data;
	XtAppContext app_context;
	VnsDatabase *db;
	char title[40];

	sprintf(title,"Virtual Notebook System (vns%d.%d.%d)",
		VNS_MAJOR_VERSION,VNS_MINOR_VERSION,VNS_RELEASE);

	/* Initialize X */
	vns->toplevel = XtVaAppInitialize(&app_context,"Vns",
		options,XtNumber(options),&argc,argv,
		(char **)NULL,
		XtNtitle,title,
		XtNgeometry,vns->session->panel_geom,
		XtNallowShellResize,True,
		NULL);
	if (vns->toplevel == NULL)
	{
		fprintf(stderr,"vns: error: cannot communicate with X server.\n");
		exit(-1);
	}
	vns->dpy = XtDisplay(vns->toplevel);

	/* obtain the application resources */
	XtGetApplicationResources(vns->toplevel,(XtPointer)&data,
		resources,XtNumber(resources),(ArgList)NULL,0);

	if (data.help == True)
	{
		Usage(0,NULL);
		exit(0);
	}
	if (!data.server)
	{
		fprintf(stderr,"vns: error: server is NULL\n");
		exit(-1);
	}
	if (!data.db)
	{
		fprintf(stderr,"vns: error: database is NULL\n");
		exit(-1);
	}

	if (!data.site_printer_file)
	{
		fprintf(stderr,"vns: warning: site printer file is NULL\n");
	}
	else
	{
		vns->site_printer_file = data.site_printer_file;
	}

	/* set the default font, if specified */
	if (data.default_font)
	{
		vns->default_font = data.default_font;
	}

	/* add database to list of databases */
	db = VnsAddDatabase(vns,data.server,data.port,data.db);

	VnsConnect(db);
	if (db->srv == NULL)
	{
		fprintf(stderr,"vns: error: cannot communicate with dbsrv\n");
		fprintf(stderr,"server: %s  ",data.server);
		fprintf(stderr,"port: %d  ",data.port);
		fprintf(stderr,"database: %s\n",data.db);
		exit(-1);
	}

	/* add the srvlib event handler as an input source for this connection */
	{
		void get_events();
		XtAppAddInput(XtWidgetToApplicationContext(vns->toplevel),
			srv_socket(db->srv),
			(XtPointer)XtInputReadMask,get_events,(XtPointer)db->srv);
	}

	/* add the callback lists */
	VnsCreateCallbackList(vns,CB_NOTEBOOK_CREATE);
	VnsCreateCallbackList(vns,CB_NOTEBOOK_MODIFY);
	VnsCreateCallbackList(vns,CB_NOTEBOOK_DELETE);
	VnsCreateCallbackList(vns,CB_PAGE_CREATE);
	VnsCreateCallbackList(vns,CB_PAGE_MODIFY);
	VnsCreateCallbackList(vns,CB_PAGE_DELETE);

	/* initialize the vns_comm library (server.c) */
	init_client_server(vns->dpy);

	/* Create all of the bitmaps that we will need */
	create_bmaps(vns->dpy);

	/* Set up colormap */
	VtSetPseudoColormap(vns->dpy);

	/* initialize the property manager */
	VnsBuildPropertyManager(vns);

	/* load notebooks */
	VnsLoadAllNotebooks(vns,db);

	/* Construct the top level command panel for VNS */
	VnsBuildCommandPanel(vns,vns->toplevel);

	/* Pop up main window */
	XtPopup(vns->toplevel,XtGrabNone);

	VnsOpenSessionPages(vns);
}
