/* PutPointer © 1989 AMIGA Plus
   v1.0 Friday 14-Jul-89 11:32:35
   By Aki Rimpilainen & Arnie Cachelin */

#include <exec/exec.h>
#include <intuition/intuition.h>
#include <intuition/preferences.h>
#include <workbench/startup.h>
#include <libraries/dos.h>
#include <functions.h>
#include <stdio.h>

struct IntuitionBase *IntuitionBase;
struct Preferences   *PrefBuff;
struct WBStartup     *WBenchMsg=NULL;

LONG   lock=NULL,savelock=NULL;

/* Including a different default pointer:
   You may include your own sprite structure here, but make sure its
   name is DefSpr[], the colors are in an array DefSpr_Col[],
   and the X any Y 'hotspot' offsets are in DefSpr_XOff and
   DefSpr_YOff. The GetPointer program will write these default
   sprite names if you don't specify a different structure name. */

#include "DefaultSprite.h"

main(argc,argv)
 int argc;
 char *argv[];
{
  struct FileHandle    *fh,*dh;
  struct WBArg         *arg;
  char   *name,*type;
  UBYTE  *SpriteData;
  short  i;


  if (argc==2)
   {
    puts("\n[33mPutPointer - © 1989 AMIGA [3mPlus[0m");
    type=argv[1];
    name="*";
    if (((*type!='r') && (*type!='R') &&
         (*type!='s') && (*type!='S')) || (strlen(type)>1))
     {
      name=argv[1];
      type="*";
     }
    }
  else if (argc==3)
   {
    puts("\n[33mPutPointer - © 1989 AMIGA [3mPlus[0m");
    type=argv[1];
    name=argv[2];
    if ((*type!='r') && (*type!='R') && (*type!='s') && (*type!='S')) Usage();
   }
  else if (argc==0)   /* We were called from the Workbench...*/
   {
    if (WBenchMsg->sm_NumArgs==1) /* This means PutPointer icon was used */
     {                            /* without selecting another pointer   */
      argc=1;
     }
    else                         /* else a pointer icon was used, or */
     {                           /* extended selection was used      */
      arg=(WBenchMsg->sm_ArgList)+1;
      if(lock=arg->wa_Lock) savelock=(long)CurrentDir(lock);
      name=arg->wa_Name;
      *type='*';
     }
   }
  else if (argc!=1) Usage();

  IntuitionBase=(struct IntuitionBase *)
    OpenLibrary("intuition.library",LIBRARY_VERSION);
  if (IntuitionBase==0) {Closing("Can't open intuition.");exit(0L); }

  PrefBuff=
    AllocMem((LONG)sizeof(struct Preferences),MEMF_CLEAR | MEMF_CHIP);
  if (PrefBuff==0) {Closing("Can't allocate memory.");exit(0L); }

  GetPrefs(PrefBuff,(LONG)sizeof(struct Preferences));
  SpriteData=(UBYTE *)PrefBuff->PointerMatrix;

  if (argc==1)              /* using default sprite */
   {
    if (!WBenchMsg)
    {
     puts("\n[33mPutPointer - © 1989 AMIGA [3mPlus[0m");
     puts("Using default pointer.");
    }
    CopyMemQuick(&DefSpr,SpriteData,72L);   /* Copy Pointer data, */
    PrefBuff->color17=DefSpr_Col[1];        /* Colors,            */
    PrefBuff->color18=DefSpr_Col[2];
    PrefBuff->color19=DefSpr_Col[3];
    PrefBuff->XOffset=DefSpr_XOff;          /* and X,Y hotspot    */
    PrefBuff->YOffset=DefSpr_YOff;          /* positions into    */
    SetPrefs(PrefBuff,(LONG)sizeof(struct Preferences));   /* Preferences */
   }
  else
   {
    if ((*type=='r') || (*type=='R'))
     {
      /* Read from file used to store Preferences settings */
      fh=Open("DEVS:system-configuration",MODE_OLDFILE);
        if (fh==0)
         {
          Closing("Can't open input file: DEVS:system-configuration");
          exit(0L);
         }
      Read(fh,PrefBuff,(LONG)sizeof(struct Preferences));
      Close(fh);
      SetPrefs(PrefBuff,(LONG)sizeof(struct Preferences));
      if (!WBenchMsg) puts("Restored system pointer.");
     }
    else
     {
      if (*name!='*')
       {
        if (!(fh=Open(name,MODE_OLDFILE))) /*  If we can't open the file, */
        {
         strcat(name,".p");                /*  add ".p" to the name and...*/
         if (!(fh=Open(name,MODE_OLDFILE)))/*  try again */
         {
          Closing("Can't open input file.");
          exit(0L);
         }
        }
        Read(fh,SpriteData,80L); /* reading sprite data */
        Close(fh);
        SetPrefs(PrefBuff,(LONG)sizeof(struct Preferences));
       }
      if ((*type=='s') || (*type=='S'))
       {
      /* Save to the file used to store Preferences settings */
        fh=Open("DEVS:system-configuration",MODE_NEWFILE);
        if (fh==0)
         {
          Closing("Can't open output file: DEVS:system-configuration");
          exit(0L);
         }
        Write(fh,PrefBuff,(LONG)sizeof(struct Preferences));
        Close(fh);
        if (!WBenchMsg) puts("Saved a default system pointer.");
       }
     }
   }
  Closing(0L);
  exit(0L);
}

Usage()  /* A little function to tell the user what's up then bail out */
{
  if (!WBenchMsg)
  {
    puts("\n[33mPutPointer © 1989 AMIGA [3mPlus[0m\n\n");
    puts("Usage: PutPointer [R|S] [filename]\n");
    puts("Options:\n\tR\tRestore system pointer.\n");
    puts("\tS\tSave as system pointer.\n\n");
  }
  Closing(0L);
  exit(0L);
}

Closing(mes)  /* Clean up after our program... */
 char *mes;   /* and give any error message     */
 {
  if ((!WBenchMsg)&&(mes)) puts(mes);
/*  if (WBenchMsg) ReplyMsg(WBenchMsg); */
  if (lock) CurrentDir(savelock);   /* restore current DIR */
  lock=NULL;
  if (PrefBuff) FreeMem(PrefBuff,(LONG)sizeof(struct Preferences));
  if (IntuitionBase) CloseLibrary(IntuitionBase);
 }

