/* Program chooses randomly one of a number 
** of IFF files, and copies it to T: for display
** by the Workbench Preferences program.
**
** Author:  A. Wyatt
**
** Date: Oct 1997
**
** Input:   source directory string
**          short integer (number of files)
**
** Output:  chooses randomly one of the files
**          and copies it to "T:backdrop.iff"
**
** Other requirements: Calls the Execute function
**    need: Run
**          Delete
**          Copy
**
** If you want to change the maximum number of files,
** change the parameter 'MAX_FILES' and the mask MAX_MASK
**
** The mask is used to limit the range of random numbers
** so that it doesn't take too long to get one within 
** the range required. Leave the mask as (2^n -1).
*/

#include <stdio.h>
#include <dos.h>

#define  AND &&
#define  OR ||
#define  EQ ==
#define  NE !=

#define  MAX_FILES   1024
#define  MAX_MASK    MAX_FILES - 1

         int  main (int, char *[]);

extern   Execute (char *, FILE *, FILE *);
extern   time (int *);
extern   void exit(int);
extern   int srand (int);
extern   int rand (void);
extern   int dfind (struct FileInfoBlock *, const char *, int);

__aligned struct   FileInfoBlock fib;       /* for file lookup */

int main (int argc, char *argv[])
{
   int   number_files;              /* number files in directory */
   int   seed;                      /* for random number generator */
   int   rand_number;               /* result of random number generator */
   char  fname[40];                 /* file name */
   char  cmd_string [64];           /* string to pass to Execute */
   int   result;                    /* result of Execute command */
   struct   FileInfoBlock *fib_ptr = &fib; /* pointer to File Info Block */
   
   if (argc != 3)                   /* should be as in 'Usage' below */
   {
      printf ("Usage: RandBackdrop <directory> <number files>\n");
      exit (10);
   }

   sscanf (argv[2], "%d", &number_files);  /* get number of files */

   if ((number_files < 2) OR (number_files > MAX_FILES)) 
   {
      printf ("RandBackdrop: No files should be 2 - %d\n", MAX_FILES);
      exit (10);
   }

   seed = time (NULL);              /* = 'Randomise' */
   srand (seed);           /* start at a random place */

   /* now make up a random number [1 - number_files] */

   do
   {
   rand_number = rand () & MAX_MASK;  /* get a number */
   }  while (rand_number >= number_files);

   sprintf (fname, "%s/pix%03d.iff", argv [1], rand_number + 1);
   
   result = dfind (fib_ptr, fname, 0);     /* look up file in directory */

   if (result EQ 0)
   {
      /* look for T:Backdrop.iff and delete if already there */

      result = dfind (fib_ptr, "T:Backdrop.iff", 0);  
      if (result EQ 0) 
      {
 printf ("Randbackdrop: File T:Backdrop.iff exists - delete and re-run\n");
         exit (10);
      }      
      sprintf (cmd_string, "Copy %s T:Backdrop.iff\n", fname);

      Execute (cmd_string, NULL, stdout);
   
      return (0);      /* success */
   }

   printf ("RandBackdrop: Can't find file '%s'\n", fname);
   return (10);
}

