/*------------------------------------------------------------------*
 * waiter.c
 *
 *------------------------------------------------------------------*/

#include <stdio.h>
#include <string.h>

#define EXISTANCE  0

main(argc, argv)
int argc;
char **argv;
{

     /* set variables */
	 char filname[80];
     char dos_command[80];
	 char repeat_cmd[1];

     /* verify enough arguements were entered on the command line */
     if(argc < 3)
     {
        printf("Usage: WAITER <file-to-detect> <DOS command> [R]\n\n");
        exit(1);
     }

	 strcpy(repeat_cmd,argv[3]);   /* use third arg as possible repeat */

     /* move command line variables to strings */
	 strcpy(filname,argv[1]);
     strcpy(dos_command,argv[2]);

     /* check keyboard buffer and exit if user indicates */
     if(kbhit())
        exit(0);

     /* display message on screen */
     printf("Waiting for:   To perform:\n");
     printf("------------   ----------------------\n");
	 printf("%-12s   %s\n", filname, dos_command);

     /* watch for filename until a key is pressed */
     while(!(kbhit()))
     {
        /* if the file exists, do the dos command */
		if(!(access(filname, EXISTANCE)))
        {
           system(dos_command);
           if( strcmpi(repeat_cmd, "R") != 0)
              exit(0);
        }
     }
     exit(0);
}

