
/*************************************************************************/
/* WBColors.c v1.0     Stefan Lindahl    HASTEF Software,  Sweden       */
/*                                                                       */
/*    Stefan Lindahl                                                     */
/*    Drevkarlsgr. 8                                                     */
/*    S-222 52  LUND                                                     */
/*    SWEDEN                                                             */
/*                                                                       */
/* This program is placed in the Public Domain                           */
/*************************************************************************/

#include <exec/types.h>
#include <libraries/dosextens.h>
#include <workbench/startup.h>
#include <stdio.h>
#include <graphics/view.h>
#include <graphics/gfxbase.h>

#define BOLDON  "\2331m"
#define BOLDOFF "\2330m"

extern struct WBStartup *WBenchMsg;         /* For WorkBench startup */

char KEYWORD1[]="colors=";     /* Lazy thing to do, but so what? */
char KEYWORD2[]="COLORS="; 

extern struct DosBase *DosBase;
struct GfxBase *GfxBase;

/** main() ****************************************************************/
main(argc, argv)
int argc;
char **argv;
{
   int i,count;
   LONG color;
   BOOL   fromWB,An_Error=FALSE;
   struct FileLock *lock;
   struct FileInfoBlock FileIB;
   char            *filename,*comment,bull[40];
   UWORD cols[4];
   struct ViewPort *vp;

   GfxBase = (struct GfxBase *)OpenLibrary("graphics.library",0);
                           /* OpenLib always works  (I hope...) */

   fromWB = (argc==0);

   if (!fromWB)                 /* Passed filename via command line  */
   {
      filename = argv[0];
   }
   else if (WBenchMsg->sm_NumArgs > 0)
   {                              /* Passed filename via  WorkBench */
      filename = (char *) WBenchMsg->sm_ArgList->wa_Name;
   }
   else
      An_Error = TRUE;       /* Something's wrong! */

   An_Error |= !(lock = (struct FileLock *) Lock(filename,ACCESS_READ) );
   if (!An_Error) Examine(lock,&FileIB);        /* Get file lock */

   printf("** Set WorkBench Colors **\nA Program by %sStefan Lindahl%s.\n",
                              BOLDON,BOLDOFF);
   printf("%sHASTEF%s Software, Sweden.\nPlaced in the Public Domain.\n",
                              BOLDON,BOLDOFF);

   while (!An_Error) /* Not used for a loop, just for a graceful exit with */
   {                                                          /* continue. */
      comment = FileIB.fib_Comment;

                        /* Does string start with 'COLORS=' or 'colors=' ? */
      if (An_Error = !(!strncmp(comment,KEYWORD1,strlen(KEYWORD1)) ||
          !strncmp(comment,KEYWORD2,strlen(KEYWORD2)) ) ) break;

      comment += strlen(KEYWORD1);

      for(i=0;i<4;i++)        /* Convert hex characters to colorvalues */
      {
         color=0L;

         count = stch_i(comment,&color);  /* Lattice hexascii to integer */
         if (An_Error = !count) break;
         if (An_Error = (color > 0xFFF)) break;
         cols[i] = (UWORD)color;

         comment += count;

         if (*(comment++) != '|' && i<3) { An_Error = TRUE; break; }
      }
      if (An_Error) break;

      vp = GfxBase->ActiView->ViewPort;   /* Get first viewport (=top)    */
                                          /* NOTE: Not neccessary the WB. */
      LoadRGB4(vp,cols,4);          /* Set the colors */

      break;
   }

   if (lock) UnLock(lock);
   if (GfxBase) CloseLibrary(GfxBase);      /* Release */

   if (An_Error) printf("\n%sError! Check the comment field!%s\n",
                              BOLDON,BOLDOFF);
   else printf("\n\n");

   printf("\nPress %sRETURN%s to continue!\233\060\040\160",/*Remove Cursor*/
                              BOLDON,BOLDOFF);
   gets(bull);
   printf("\233\040\160");   /* Cursor on again */
}
      /* Bye, Bye       SL     November 28, 1987 */
