/*-=-=-=-=-=-=-=-=-=-=-=- RCCL/RCI Notice of Copyright -=-=-=-=-=-=-=-=-=-=-*
 *									    *
 *	Copyright (C) 1987, 1991 by John E. Lloyd.			    *
 *									    *
 *	Information on copyright permissions, as well as a complete	    *
 *	disclaimer of warranty, is given in the file COPYRIGHT.		    *
 *									    *
 *-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=*/

#include <stdio.h>

#include "../h/cdefs.h"
#include "../h/errorCodes.h"
#include "../h/robot_jls.h"

#define MAX_NAMELEN	63

static char robotName[MAX_NAMELEN+1] = {'\0'};
static int initDone = 0;

/* ----- Error Messages. Defined externally to save space ----- */

static char *noRCCLEnvMsg	= "%s -- environment variable RCCL not set\n";
static char *cantOpenFileMsg	= "%s -- can't open file %s\n";
static char *cantReadFileMsg	= "%s -- can't read file %s\n";

char *getDefaultRobot ()
{	
	char *fxnName = "defaultRobot()";

	char filename[256];
	char *rcclDir;

	if (!planningLevelCheck(fxnName))
	 { return (NULL);
	 }
	if (!initDone)
	 { FILE *fp;
	   if ((rcclDir = (char *)getenvRCCL()) == NULL)
	    { errorMsg (EEnviron, noRCCLEnvMsg, fxnName);
	      return (NULL);
	    }
	   snprintf (filename, 256, "%s/conf/defaultRobot", rcclDir);
	   if ((fp = fopen (filename, "r")) == NULL)
	    { errorMsg (EBadAccess, cantOpenFileMsg, fxnName, filename);
	      return (NULL);
	    }
	   if (fscanf (fp, "%63s", robotName) != 1)
	    { errorMsg (EBadObj, cantReadFileMsg, fxnName, filename);
	      fclose (fp);
	      return (NULL);
	    }
	   robotName[MAX_NAMELEN] = '\0';
	   fclose (fp);
	   initDone = 1;
	   requestFlagInit(&initDone);
	 }
	return (robotName);
}
