/* options.c
   process application options (resources) using the X resource manager. */


     /*---------------------------------------------------------------*/
     /* Xgopher        version 1.1     20 April 1991                  */
     /*                version 1.0     04 March 1991                  */
     /* X window system client for the University of Minnesota        */
     /*                                Internet Gopher System.        */
     /* Allan Tuchman, University of Illinois at Urbana-Champaign     */
     /*                Computing Services Office                      */
     /* Copyright 1992 by                                             */
     /*           the Board of Trustees of the University of Illinois */
     /* Permission is granted to freely copy and redistribute this    */
     /* software with the copyright notice intact.                    */
     /*---------------------------------------------------------------*/


#include <stdio.h>

#include <X11/Xlib.h>
#include <X11/StringDefs.h>
#include <X11/Intrinsic.h>

#include "conf.h"
#include "globals.h"
#include "options.h"
#include "listP.h"
#include "prefixP.h"

/* Note on boolean data types.
   Xlib.h	defines Bool, True, False (as int, 1, 0)
   Intrinsic.h	defines Boolean, TRUE, FALSE (as typedef char, 1, 0)

   To keep some modules of this program independent of X, the new
   type BOOLEAN is introduced (typedef int), and constants TRUE and
   FALSE are conditionally defined in conf.h to be 1 and 0.
   An implicit conversion from Boolean (resources) to BOOLEAN
   is assumed. */


/* makePrefix
   edit application resources of directory listing prefixes to conform
   to be blank padded and exactly PREFIX_LEN characters.
   NOTE: These strings are *NOT* NULL-terminated. */

static void
makePrefix(target, source)
char	*target, *source;
{
	int	i;

	for (i=0; i<PREFIX_LEN; i++) {
		if (*source == NULL) {
			*(target++) = ' ';
		} else {
			*(target++) = *(source++);
		}
	}
}


/* getApplicationResources
   get application resources */

gopherAppResources *
getApplicationResources(topLevel, argc, argv)
Widget	topLevel;
int	argc;
char	**argv;
{
	static gopherAppResources	appResources;

	/* get application resources */

	XtGetApplicationResources(topLevel, &appResources, resources,
			XtNumber(resources), NULL, 0);

	if (! appResources.defaultsInstalled) return NULL;

	itemStart     = appResources.itemStart;
	itemIncrement = appResources.itemIncrement;
	dirStart      = appResources.dirStart;
	dirIncrement  = appResources.dirIncrement;

	setHelpFile (appResources.helpFile);

	if (appResources.logFile != NULL  &&  *appResources.logFile != NULL)
		if ((logFP = fopen (appResources.logFile, "w")) == NULL) {
			fprintf (stderr, "cannot open log file \'%s\'\n",
						appResources.logFile);
		} else {
			setbuf(logFP, NULL);
		}
	else 
		logFP = NULL;

	makePrefix(prefixFile,   appResources.prefixFile);
	makePrefix(prefixDir,    appResources.prefixDir);
	makePrefix(prefixCSO,    appResources.prefixCSO);
	makePrefix(prefixIndex,  appResources.prefixIndex);
	makePrefix(prefixTelnet, appResources.prefixTelnet);
	makePrefix(prefixSound,  appResources.prefixSound);
	makePrefix(prefixUnknown,appResources.prefixUnknown);

#ifdef DEBUG
	LOG (logFP, "Application Resources\n");
	LOG (logFP, "\tdefaultsInstalled:\t%s\n",
			appResources.defaultsInstalled ? "True" : "False");
	LOG (logFP, "\trootServer:\t%s\n",
			appResources.rootServer);
	LOG (logFP, "\trootPort:\t%d\n",
			appResources.rootPort);
	LOG (logFP, "\tmainTitle:\t%s\n",
			appResources.mainTitle);
	LOG (logFP, "\tallowSave:\t%s\n",
			appResources.allowSave ? "True" : "False");
	LOG (logFP, "\tallowPrint:\t%s\n",
			appResources.allowPrint ? "True" : "False");
	LOG (logFP, "\tprintCommand:\t%s\n",
			appResources.printCommand);
	LOG (logFP, "\thelpFile:\t%s\n",
			appResources.helpFile);
	LOG (logFP, "\tdirectoryTime:\t%d\n",
			 appResources.directoryTime);
	LOG (logFP, "\thasSound:\t%s\n",
			appResources.hasSound ? "True" : "False");
	LOG (logFP, "\tsoundCommand:\t%s\n",
			appResources.soundCommand);
	LOG (logFP, "\tallowTelnet:\t%s\n",
			appResources.allowTelnet ? "True" : "False");
	LOG (logFP, "\ttelnetCommand:\t%s\n",
			appResources.telnetCommand);
	LOG (logFP, "\titemStart:\t%d\n",
			 appResources.itemStart);
	LOG (logFP, "\titemIncrement:\t%d\n",
			 appResources.itemIncrement);
	LOG (logFP, "\tdirStart:\t%d\n",
			 appResources.dirStart);
	LOG (logFP, "\tdirIncrement:\t%d\n",
			 appResources.dirIncrement);
	LOG (logFP, "\tdoubleClick:\t%s\n",
			appResources.doubleClick ? "True" : "False");
	LOG (logFP, "\tmarkRoot:\t%s\n",
			appResources.markRoot ? "True" : "False");
	LOG (logFP, "\ttempDirectory:\t%s\n",
			 appResources.tempDirectory);
	LOG (logFP, "\tlogFile:\t%s\n",
			 appResources.logFile==NULL ? 
					"<none>" : appResources.logFile);
	LOG (logFP, "\tprefixFile:\t%.*s\n",
			 PREFIX_LEN, appResources.prefixFile);
	LOG (logFP, "\tprefixDir:\t%.*s\n",
			 PREFIX_LEN, appResources.prefixDir);
	LOG (logFP, "\tprefixTelnet:\t%.*s\n",
			 PREFIX_LEN, appResources.prefixTelnet);
	LOG (logFP, "\tprefixCSO:\t%.*s\n",
			 PREFIX_LEN, appResources.prefixCSO);
	LOG (logFP, "\tprefixIndex:\t%.*s\n",
			 PREFIX_LEN, appResources.prefixIndex);
	LOG (logFP, "\tprefixSound:\t%.*s\n",
			 PREFIX_LEN, appResources.prefixSound);
	LOG (logFP, "\tprefixUnknown:\t%.*s\n",
			 PREFIX_LEN, appResources.prefixUnknown);
#endif /* DEBUG */

	return &appResources;
}
