/*************************************************************************
 ***                        SETPREFS.C                   (JJB TEMPLAR) ***
 *** Date begun: 25/6/89.                                              ***
 *** Last modified: 25/6/89.                                           ***
 ***                12/7/89 - fixed WB startup (to CurrentDir)         ***
 ***                13/7/89 - big optimize pass.                       ***
 *************************************************************************/
/*** Program to set prefs from a system-config file.                   ***
 *** No longer opens a WB console, since all of a sudden the console   ***
 *** windows have become borderless and funnily colored.               ***
 *************************************************************************/

#include <exec/types.h>
#include <workbench/startup.h>
#include <intuition/intuition.h>

#include <proto/exec.h>
#include <proto/intuition.h>
#include <proto/dos.h>

#include <string.h>

struct IntuitionBase    *IntuitionBase;

struct Preferences  mypref,newpref;
char    *filename,*def = "devs:system-configuration";
int     noWBcons = 1;

extern struct WBStartup *WBenchMsg;
extern char             *_ProgramName;
BPTR    output;

#define USAGE       8
#define USAGENAME   4
char    *usage[USAGE] = {
"[32mSetPrefs2.0: ",
__DATE__,
" - [42m[33m JJB TEMPLAR [40m - load prefs from file.[0m\n",
"[32mUSAGE:[0m ",
NULL,
" [fname]\n",
"    where fname = name of a system-configuration file.\n",
"[32mNOTE:[0m only the pointer, colors, and key speed info are loaded.\n"
};

void    printf(char *);
void    dolibs();
void    doopts(char *);
void    cleanup(int);

void    printf(cp) /*====================================================*/
register char   *cp;
{
    if (output) Write(output,cp,strlen(cp));
}

void    dolibs() /*======================================================*/
{
    if (!(IntuitionBase = (struct IntuitionBase *)OpenLibrary("intuition.library",0L))) {
        printf("ERROR: failed to open intuition.library!\n");
        cleanup(0);
    }
}

void    doopts(param) /*=================================================*/
register char *param;
{
register struct WBArg   *wa;
register int            j;
    if (!*param) {  /* Param itself is not NULL. It points to NULL. */
        if (WBenchMsg->sm_NumArgs > 1) {
            wa = WBenchMsg->sm_ArgList;
            wa++;
            CurrentDir(wa->wa_Lock);
            filename = wa->wa_Name;
        }
        else filename = def;
        output = NULL;
    }
    else {
        output = Output();
        param += strlen(_ProgramName);
        while (*param && ((*param == ' ') || (*param == '\n'))) param++;
        if (!*param) filename = def;
        else {
            if (*param == '?') {
                usage[USAGENAME] = _ProgramName;
                for (j = 0; j < USAGE; j++) printf(usage[j]);
                cleanup(1);
            }
            if (*param == '"') {
                param++;
                filename = param;
                while (*param && (*param != '\n') && (*param != '"')) param++;
                *param = 0;     /* Don't care if no second quote */
            }
            else {
                filename = param;
                while (*param && (*param != '\n') && (*param != '"') && (*param != ' ')) param++;
                *param = 0;
            }
        }
    }
}

void    _main(param) /*==================================================*/
register char   *param;
{
register int    j;
register BPTR   file;
    dolibs();
    doopts(param);

    if (!(file = Open(filename,MODE_OLDFILE))) {
        printf("ERROR: failed to open file!\n");
        cleanup(1);
    }

    GetPrefs(&newpref,sizeof(struct Preferences));   /* Old preferences */

    if (Read(file,(char *)&mypref,sizeof(mypref)) != sizeof(mypref)) {
        printf("ERROR: Reading file!\n");
        goto END;
    }

  /* Now newpref = old prefs, and mypref = prefs from file */
    newpref.KeyRptSpeed = mypref.KeyRptSpeed;
    newpref.KeyRptDelay = mypref.KeyRptDelay;
    newpref.DoubleClick = mypref.DoubleClick;
    for (j = 0; j < POINTERSIZE; j++)
        newpref.PointerMatrix[j] = mypref.PointerMatrix[j];
    newpref.XOffset = mypref.XOffset;   newpref.YOffset = mypref.YOffset;
    newpref.color17 = mypref.color17;   newpref.color18 = mypref.color18;
    newpref.color19 = mypref.color19;
    newpref.PointerTicks = mypref.PointerTicks;
    newpref.color0 = mypref.color0;     newpref.color1 = mypref.color1;
    newpref.color2 = mypref.color2;     newpref.color3 = mypref.color3;

  /* Now newpref = old prefs, with pointer and colors from mypref */
    SetPrefs(&newpref,sizeof(struct Preferences),1);
END:
    Close(file);
    cleanup(1);
}

void    cleanup(bra) /*==================================================*/
int     bra;
{
    switch (bra) {
        case (1): CloseLibrary(IntuitionBase);
        case (0): break;
    }
    XCEXIT(0);      /* Safe to bypass exit() ? */
}
