/************************************************************************
**                                                                     **
** ShowPreferences                                                     **
**                                                                     **
** This utility allows you to view the system-configuation file of any **
** workbench disk without booting from this disk.                      **
** ShowPrefs can be started from CLI or workbench.                     **
**                                                                     **
** Author & date: Rainer Koppler, 24.2.1992                            **
** Compiler: Lattice-C v5.02                                           **
**                                                                     **
************************************************************************/

#include <exec/types.h>
#include <exec/memory.h>
#include <intuition/intuition.h>
#include <intuition/preferences.h>
#include <workbench/icon.h>
#include <workbench/startup.h>
#include <workbench/workbench.h>
#include <libraries/dosextens.h>

#define LIB_VERSION 33L  /* at least Kickstart 1.2 */
#define MAX_LEN 80       /* length of work string */

#define PREF_SIZE sizeof(struct Preferences)

/*---------------------------------------------------------------------**
** external variables and global declarations                          **
**---------------------------------------------------------------------*/

extern struct IntuitionBase *IntuitionBase;
extern struct IconBase *IconBase;
extern struct WBStartup *WBenchMsg;

static char workStr[MAX_LEN];
static char devStr[MAX_LEN/2];
static const char sysConfName[] = "system-configuration";
static char toolName[] = "UNIT";


/*---------------------------------------------------------------------**
** function: GetToolType                                               **
** returns the value of a specified tool type of a specified file.     **
**---------------------------------------------------------------------*/

char *GetToolType (char *fileName, char *ttName, char *ttValue)
{
 struct DiskObject *obj;
 char *ttVal = NULL;

 if(IconBase = (struct IconBase *)OpenLibrary("icon.library",LIB_VERSION)) {
   if(obj = GetDiskObject(fileName)) {
     if(ttVal = FindToolType(obj->do_ToolTypes,ttName)) {
       strcpy(ttValue,ttVal);
       ttVal = ttValue;
     }
     FreeDiskObject(obj);
   }
   CloseLibrary(IconBase);
 }
 else
   printf("Cannot open icon.library.\n");
 return(ttVal);
}


/*----------------------------------------------------------------------**
** function: GetPrefName                                                **
** searches a file looking like "<unitName>[devs/]system-configuration" **
** and returns the name of the first found file.                        **
**----------------------------------------------------------------------*/

char *GetPrefName(char *unitName)
{
 BOOL found = FALSE;
 struct FileLock *tst;

 /* attempt to access UNIT:system-configuration */
 if(unitName != NULL) strcpy(workStr,unitName);
 strcat(workStr,sysConfName);
 if((tst = (struct FileLock *)Lock(workStr,ACCESS_READ)) != NULL)
   found=TRUE;
 UnLock(tst);
 if(found) return(workStr);

 /* attempt to access UNIT:devs/system-configuration */
 workStr[0] = '\0';
 if(unitName != NULL)
   strcpy(workStr,unitName);
 strcat(workStr,"devs/");
 strcat(workStr,sysConfName);
 if((tst = (struct FileLock *)Lock(workStr,ACCESS_READ)) != NULL)
   found=TRUE;
 UnLock(tst);
 if(found) return(workStr);

 /* no matching file found */
 return(NULL);
}


/*----------------------------------------------------------------------**
** function: ShowPrefs                                                  **
** sets new preferences, opens a simple window and waits until the user **
** closes the window.                                                   **
**----------------------------------------------------------------------*/

struct NewWindow demoWindow = {
  185,82,                  /* LeftEdge,TopEdge   */
  270,92,                  /* Width,Height       */
  0,1,                     /* DetailPen,BlockPen */
  CLOSEWINDOW,             /* IDCMPFlags         */
  WINDOWDRAG+WINDOWCLOSE+
  WINDOWSIZING+WINDOWDEPTH,/* Window Flags       */
  NULL,                    /* Gadget             */
  NULL,                    /* Image              */
  "Close window to exit",  /* Title              */
  NULL,                    /* Screen             */
  NULL,                    /* Bitmap             */
  248,20,                  /* MinWidth,MinHeight */
  640,256,                 /* MaxWidth,MaxHeight */
  WBENCHSCREEN             /* Type               */
};


void ShowPrefs(char *prefName)
{
 struct Preferences *oldPrefs,*newPrefs,*setPrefs;
 struct FileHandle *pFile;
 struct Window *win;
 struct IntuiMessage *imsg;
 int class,code;
 
 if(IntuitionBase=(struct IntuitionBase *) OpenLibrary("intuition.library",LIB_VERSION)) {
   if(oldPrefs=(struct Preferences *)AllocMem(PREF_SIZE*3,MEMF_CLEAR|MEMF_FAST)) {
     newPrefs = oldPrefs+PREF_SIZE;
     setPrefs = newPrefs+PREF_SIZE;
     if(pFile = (struct FileHandle *)Open(prefName,MODE_OLDFILE)) {
       Read(pFile,(UBYTE *)newPrefs,PREF_SIZE);
       Close(pFile);
       GetPrefs(oldPrefs,PREF_SIZE);
       CopyMem(oldPrefs,setPrefs,PREF_SIZE);
       setPrefs->color17 = newPrefs->color17;
       setPrefs->color18 = newPrefs->color18;
       setPrefs->color19 = newPrefs->color19;
       CopyMem(newPrefs->PointerMatrix,setPrefs->PointerMatrix,sizeof(USHORT)*36);
       setPrefs->color0 = newPrefs->color0;
       setPrefs->color1 = newPrefs->color1;
       setPrefs->color2 = newPrefs->color2;
       setPrefs->color3 = newPrefs->color3;
       SetPrefs(setPrefs,PREF_SIZE,FALSE);
       if(win = (struct Window *)OpenWindow(&demoWindow)) {
         Wait(1L << win->UserPort->mp_SigBit);
         while(imsg = (struct IntuiMessage *)GetMsg(win->UserPort)) {
           class = imsg->Class;
           code = imsg->Code;
           ReplyMsg(imsg);
           if(class == CLOSEWINDOW) break;
         }
         CloseWindow(win);
       }
       SetPrefs(oldPrefs,PREF_SIZE,FALSE);
     }
     FreeMem(oldPrefs,PREF_SIZE*3);
   }
   CloseLibrary(IntuitionBase);
 }
}


/*--------------------------------------------------------------------*/

void main(int argc, char *argv[])
{
 char *confName = NULL, *unitName = NULL;

 if(argc == 0)
   /* start from workbench */
   unitName = GetToolType(WBenchMsg->sm_ArgList->wa_Name,toolName,devStr);
 else {
   /* start from CLI */
   if(--argc > 1) {
     printf("Too many parameters !\n");
     exit(FALSE);
   }
   if(argc == 1) {
     if(strcmp(argv[1],"?")==0) {
       printf("\n»» ShowPrefs «« by Rainer Koppler\n\n");
       printf("Usage: %s [<system-configuration file>]\n",argv[0]);
       exit(FALSE);
     }
     else
       confName = argv[1];
   }
 }
 if(confName == NULL)
   if((confName = GetPrefName(unitName))==NULL) {
     printf("No system-configuration file found.\n");
     exit(FALSE);
   }
 ShowPrefs(confName);
}
