/* PrintPal © 1989 AMIGA Plus v1.0
   By Aki Rimpilainen & Arnie Cachelin
   Wednesday 01-Nov-89 14:41:10  */


#define DEFAULTFILE "PrtData.prt"
#define F_OK        0L             /* checking existence of file */

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

#include "PrtIcon.h"     /* DiskObject structure, file created by a */
                         /* fixed version of Icon (A+#5) see icon.fix */
struct IntuitionBase *IntuitionBase;
struct Preferences   *PrefBuff;
struct WBStartup     *WBenchMsg=NULL;
struct IconBase      *IconBase;

LONG   lock=NULL,savelock=NULL;

main(argc,argv)
 int argc;
 char *argv[];
{
  struct WBArg       *arg;
  struct FileHandle  *fh;
  char   progname[52],name[52],opt[10];
  short  i,j;


  if ((argc>3)||(argc==2))
   {
    puts("\n[33mPrintPal - © 1989 AMIGA [3mPlus[0m");
    Usage();            /* display help if too many args */
   }
  if (argc==0)  /* That means we started from Workbench! */
   {
    arg=(WBenchMsg->sm_ArgList);
    strcpy(progname,arg->wa_Name);  /* Save program name for Icon */
    if (WBenchMsg->sm_NumArgs==1)   /* WB & no args --> saving data */
     {
      strcpy(name,DEFAULTFILE);
        /* access() is an Aztec specific function, so if you
           compile this with Lattice, either bag the following
           loop, of test the file using LOCK */
      for (j=1; access(name,F_OK)==0; j++)    /* add X to the file name */
       {                                      /* to find a usable one */
        strcpy(name,DEFAULTFILE);
        for (i=0; i<j; i++) strcat(name,"X");
       }
      strcpy(opt,"S");
     }
    else       /* WB & args --> setting preferences */
     {
      arg++;
      if(lock=arg->wa_Lock) savelock=(long)CurrentDir(lock);
      strcpy(name,arg->wa_Name);
      strcpy(opt,"L");
     }
   }
  else
   {
    puts("\n[33mPrintPal - © 1989 AMIGA [3mPlus[0m");
    strcpy(opt,argv[1]);       /* get the option  */
    strcpy(progname,argv[0]);       /* Get program name for icon's default tool  */
    if ( (strlen(opt)) >1 )       /* if opt longer than byte, exit */
     {
      strcpy(name,"Option too long : ");
      strcat(name,opt);            /* display what went wrong */
      Closing(name);
      exit(0L);
     }
    else strcpy(name,argv[2]);
   }

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

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

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

/* Get the printer info from preferences */
  GetPrefs(PrefBuff,((LONG)sizeof(struct Preferences)));

  if ((*opt=='S') || (*opt=='s'))    /* getting information */
   {
    if (!WBenchMsg)
     {
      AddExtension(name,".prt");
      printf("Saving Preferences printer settings as %s...\n",name);
     } /* saving data using dos.library commands rather than C std */
       /* this allows us to write the whole block w/ one command */
    fh=Open(name,MODE_NEWFILE);
    if (fh==0) { Closing("Can't open output file."); exit(0L); }
    Write(fh,&PrefBuff->PrinterType,106L);
    Close(fh);

    DOB.do_DefaultTool=progname;    /* put program name into data icon so it
                                       can find its application */
    PutDiskObject(name,&DOB);           /* save icon as name.info */

    if (!WBenchMsg) printf("%s Saved.\n",name);
   }

  else if ((*opt=='L') || (*opt=='l'))      /* setting information */
   {
    if (!(fh=Open(name,MODE_OLDFILE)))    /*  If we can't open the file, */
     {
      AddExtension(name,".prt");          /*  add ".prt" to the name and...*/
      if (!(fh=Open(name,MODE_OLDFILE)))  /*  try again */
        { Closing("Can't open input file."); exit(0L); }
     }
    Read(fh,&PrefBuff->PrinterType,106L); /* reading printer data */
    Close(fh);

    SetPrefs(PrefBuff,(LONG)sizeof(struct Preferences));
    printf("Preferences printer settings loaded from %s.\n",name);
   }
  else   /* exit if unknown option */
   {
    printf("Unknown option: %s\n",opt);
    Usage();
   }

  Closing(NULL);
  exit(0L);
}

int  AddExtension(base,ext)    /* Adds the extension (like .p) to a string */
     char    *base,*ext;       /* if it is not already there  */
    {
     char    *p,*e;
     int     a,b;
     e=ext;
     a=strlen(base);
     b=strlen(ext);
     if (a<b)          /* is extension is longer than string, */
      {                 /* add it and quit */
       strcat(base,ext);
       return(1);
      }
     else
      for(p=base + a - b; *p!=0; p++)
        if (*p!=*e++)
        {
         strcat(base,ext); /* Add extension as soon as strings don't match */
         return(1);
        }
     return(0);
    }

Usage()
{
  puts("Usage: PrintPal (L|S) [filename]\n");
  puts("Options:\n\tL\t[33mL[0moad printer preferences");
  puts("\tS\t[33mS[0mave printer preferences\n");
  Closing(NULL);
  exit(0L);
  return(0);
}

Closing(mes)
char *mes;
{
  if ((!WBenchMsg)&&(mes)) puts(mes);
  if (lock) CurrentDir(savelock);   /* restore current DIR */
  lock=NULL;
  if (PrefBuff) FreeMem(PrefBuff,(LONG)sizeof(struct Preferences));
  if (IconBase) CloseLibrary(IconBase);
  if (IntuitionBase) CloseLibrary(IntuitionBase);
  return(0);
}

