/*      SavePrefs.c by David W. Nipper
         placed in the Public Domain
              9 November 1987                  */

/*   Creates a file in the Devs: directory
     containing a copy of the current Pref-
     erences in use.  This program runs 
     only from the CLI.  These files are
     intended for use with the companion
     program SetPrefs.  Enter the  desired
     name for the current Preferences set
     and enter it after "SavePrefs".  You
     do not add the "Devs:" path to the
     name, that is done in the program for
     you.  No alternate paths are supported
     because that is where the SetPrefs
     program will look for the file, and
     that is where the system keeps such
     information, anyway.                      */



#include <workbench/startup.h>
#include <exec/types.h>
#include <exec/memory.h>
#include <intuition/intuition.h>
#include <graphics/sprite.h>
#include <libraries/dos.h>
#define REV 0L

extern void *OpenLibrary(), *AllocMem();
extern struct Preferences *GetPrefs();
extern struct FileHandle *Open();
extern char *strcat();
void *IntuitionBase;
struct Preferences *Pref;

main (argc,argv)
int argc;
char **argv;
{
        struct FileHandle *OutFil;
        char *FileName;
        long len;

        openstuff();
        if (argc != 2) {
          die("Must be one parameter exactly\n");
        }
        else {
          argv++;
          FileName = *argv;
        } 
        Pref = AllocMem((long)sizeof(*Pref),(long)MEMF_PUBLIC);
        if (!(Pref))
          die ("Couldn't Allocate Memory\n");
        Pref = GetPrefs(Pref,(long)sizeof(*Pref)); 
        FileName = strcat("Devs:",FileName);
        OutFil = Open(FileName,MODE_NEWFILE);
        len = Write(OutFil,Pref,(long)sizeof(*Pref));
        if (len == -1) {
          Close(OutFil);
          die("Couldn't write file\n");
        }
        Close(OutFil);
        closestuff();
}

openstuff ()
{
	if (!(IntuitionBase = OpenLibrary ("intuition.library", REV))) {
		die ("Intuition open failed\n");
        }
}

closestuff ()
{
	if (Pref) {
		FreeMem (Pref,(long)sizeof(*Pref));
        }
	if (IntuitionBase) {
		CloseLibrary (IntuitionBase);
        }
}

die (str)
char *str; 
{
	puts (str);
	closestuff();
	exit (100L);
}
