/* Get the locale settings */

#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <libraries/locale.h>
#include <libraries/iffparse.h>
#include <libraries/dos.h>
//#include <proto/locale.h>
//#include <proto/iffparse.h>
//#include <proto/dos.h>
#include <prefs/locale.h>
#include <prefs/prefhdr.h>

#include <clib/dos_protos.h>
#include <clib/iffparse_protos.h>

#define ENGLISH 1
#define FRENCH 2
#define SPANISH 3

extern int language;
extern BOOL locale;

extern struct IntuitionBase *IntuitionBase;
extern struct GfxBase *GfxBase;
extern struct Library *UtilityBase;
extern struct Library *GadToolsBase;
extern struct Library *DiskfontBase;
extern struct Library *AslBase;
extern struct Library *IFFParseBase;
extern struct Library *DOSBase;

struct Event {
	BOOL used;
	int type;	/* Type of event */
	int hours;	/* Time of event */
	int minutes;
	int timecode;
	char message[256];
	int day;
	int month;
	int year;
	BOOL enabledate;
	int freq;
};

extern struct {
/* Prefs structure used for preferences file format */

   char header[10];  /* I.D. Header */
   int vers;         /* Version of preferences file */
   int x;            /* X dimension of window when saved */
   int y;            /* Y dimension of window when saved */
   int width;        /* Width of window when saved */
   int height;       /* Height of window when saved */
   int planguage;    /* Language selected (menu) when saved, low pri */
   int just;         /* Justification of text, 0=Left, 1=Centre, 2=Right */
   short date;       /* Display date?, 0 = No, 1=Yes */
   short wtf;	     /* Window to front ? */
   int time_col[8];
   int date_col[8];
   short autoadjust;       /* Use automatic adjustment on window ? */
   char backdrop[256];  /* Backdrop image */
   char pub[139];			/* Default public screen */
	char accent[256];		/* Language file for tRanslate */
	char tkey[100];
	char hkey[100];
	struct Event events[11];  /* For the alarm! */

}prefs;

short getlocale(void);

short getlocale(void) {
	int i=0, p=0;
	struct IFFHandle *iffhandle;
	struct ContextNode *cnode;
	struct StoredProperty *hdrsp, *sp;
	LONG ifferror;
	char prefs_languages[10][31];
	
	IFFParseBase=OpenLibrary("iffparse.library",0);
	if(!IFFParseBase)
		goto skipbit;
	
	if(iffhandle=AllocIFF()) {
		if(iffhandle->iff_Stream=(LONG)Open("env:sys/locale.prefs",MODE_OLDFILE)) {
			InitIFFasDOS(iffhandle);
			if(OpenIFF(iffhandle,IFFF_READ) == 0) {
				PropChunk(iffhandle,ID_PREF,ID_PRHD);
				PropChunk(iffhandle,ID_PREF,ID_LCLE);
				while(1) {
					ifferror=ParseIFF(iffhandle,IFFPARSE_STEP);
					if(ifferror==IFFERR_EOC) continue; else if(ifferror) break;
					if(cnode=CurrentChunk(iffhandle)) {
					   if(cnode->cn_ID == ID_PRHD || cnode->cn_ID == ID_FORM) continue;
					}
					
					hdrsp=FindProp(iffhandle,ID_PREF,ID_PRHD);
					if(sp=FindProp(iffhandle,ID_PREF,ID_LCLE)) {
						for(i=0; i<10; i++) {
							strcpy(prefs_languages[i],((struct LocalePrefs *)sp->sp_Data)->lp_PreferredLanguages[i]);
						}
					}
				}
				CloseIFF(iffhandle);
			}
			Close(iffhandle->iff_Stream);
		}
		FreeIFF(iffhandle);
	}

	CloseLibrary(IFFParseBase);

	/* Additional Processing */
	
	for(p=0; p<10; p++) {
		if(!strcmp(prefs_languages[p],"english")) {
			language=ENGLISH; 
			return(1);
		}
		if(!strcmp(prefs_languages[p],"français")) {
			language=FRENCH;
			return(1);
		}
		if(!strcmp(prefs_languages[p],"español")) {
			language=SPANISH; 
            return(1); 
		}
	}			
	
	/* If it gets this far then none of the preferred languages is
       supported.  In this case the built-in system is set */

	skipbit:

   language=prefs.planguage;
	locale=FALSE;

   /* If nothing is found in planguage (such as when a new pref file is
      created) then English is set to the prefs and system */

	if(!language) {
		language=ENGLISH; prefs.planguage=ENGLISH;
	}
	
	return(0);	/* Locale did not work */
}
					
						
				
		

	
	




