/* ***************************************************************************

Programma ....... RunBack
Versione ........ 6.0 - 12/8/1989
Autori .......... Rob Peck, Greg Searle, Doug Keller, Luigi Callegari
Software ........ Lattice C V5.02 
Hardware ........ Amiga 500/2500, Kickstart & Workbench V1.2/1.3
Sintassi ........ RunBack [[-]delay [priorità]] comando [argomenti...]
Compilazione .... lc -cusf -rr -v -w -O
Linking ......... Blink TO RunBack FROM lib:cres.o+runback.o
                  LIBRARY LIB:lcsr.lib+lib:amiga.lib sc sd nd verbose

*************************************************************************** */

#include <exec/types.h>
#include <exec/memory.h>
#include <exec/tasks.h>
#include <libraries/dosextens.h>
#include <proto/dos.h>
#include <proto/exec.h>
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <fcntl.h>
#include <dos.h>

/****************************  Prototipi **************************/
void main (int , char **);
void Usage(void);
int IsInteger (char *);

/* *********************** Funzione principale ********************/
void main (int argc, char *argv[])
{   
   LONG   loaddelay = 0, fromparm = 1, priority = 0, oldpriority,
          priorityset = FALSE, SetTaskPri();
   UBYTE  commandstring[255], name[255];

   /*struct FileHandle *nilfh;*/
   BPTR nilfh;
   struct TaskCB     *task, *FindTask();

    if (argc < 2  ||  argv[1][0] == '?') Usage();

    /********* *  Verifica parametri.  * *********/

    if (IsInteger (argv[1])) 
	{               /* Load Delay. */
	fromparm  = 2;
	loaddelay = 50 * atol (argv[1]);
	if (loaddelay < 0) loaddelay = -loaddelay;
	if (argc < 3) Usage();           /* Ritardo, senza nome file! */
	strcpy(name, argv[2]);
    	if (IsInteger (argv[2]))
		{        /* Priorità task  */
		fromparm    = 3;
		priority    = atol (argv[2]);
		priorityset = TRUE;
		if (argc < 4) Usage();    /* Manca ancora nome file! */
		strcpy(name, argv[3]);
    		}
	}
    else
	{
	strcpy(name, argv[1]);
	}

    nilfh = Open ("NIL:" ,MODE_NEWFILE); /* Segue sempre */

   /********* *  Ricostruisce stringa parametri  * *********/

    strcpy (commandstring, "RUN ");

    while (fromparm < argc) {
       strcat (commandstring, " "); /* add a blank */

       strcat (commandstring, argv[fromparm++]);


    } /* while */

   /********* *  Lancia il programma  * *********/

/* La priorità viene fissata modificando temporaneamente quella del CLI
corrente. Il nuovo task assume questa stessa priorità. ***************/

#ifdef DEBUG
    printf("%s\n", commandstring);
    printf("Delay %ld Priority %ld\n", loaddelay, priority);
#else
    task = FindTask (NULL);
    if (priorityset) oldpriority = SetTaskPri( task, priority );
    Execute (commandstring, nilfh, nilfh);
    if (priorityset) SetTaskPri (task, oldpriority);
    printf("[33;3m%s[m in background, priorità= %ld\n", name, priority);
#endif

    (void)Delay ( loaddelay );

}  /* fine main() */

/* Ritorna TRUE se la stringa è intera */
int IsInteger (char *s)
{
     if (*s == '-') ++s;
     while (*s)
         if (*s < '0'  ||  *(s++) > '9') return (FALSE);
     return (TRUE);
}


void Usage()
{
     printf("Runback V6.0i\n");
     printf("Uso: RUNBACK [[-]ritardo [priorità]] comando [argomenti...]\n\n");
     exit( 0L );
}
