/*
 * wild.c - expands wildcard arguments and run command afterwards
 *
 * Bruno Costa - 15 Mar 91 - 19 Mar 91
 *
 * (Requires AmigaDOS 2.0)
 */

#include <exec/types.h>
#include <clib/dos_protos.h>
#include <pragmas/dos_pragmas.h>
#include "wildargs.h"

extern struct DosLibrary *DOSBase;

#define HUGECLUSTER 2000	/* any big number will do */
#define MAXCMD 1024
char cmd[MAXCMD];


void usage (void)
{
 PutStr ("\x1b[1mwild\x1b[0m v2.0 - \xa9 1991 by Bruno Costa\n");
 PutStr ("usage: wild [-eqfdso] [-<n>] <program> {<wildcards>} ...\n");
 PutStr ("\t-e = don't do anything, just echo\n"
         "\t-q = quote arguments\n"
         "\t-f = just use files from wildcard expansion\n"
         "\t-d = just use directories from wildcard expansion\n"
         "\t-s = don't map a lonely star ('*') to '#?'\n"
         "\t-o = don't assume that arguments starting with '-' are options\n"
         "\t<n>= call <program> with at most <n> arguments\n");
 exit (10);
}


void main (int argc, char *argv[])
{
 int arg, childarg, cluster = HUGECLUSTER;
 int copyopt = TRUE, quote = FALSE, echo = FALSE;
 int wflags = WILD_DIRS | WILD_FILES;

 if (argc < 2)
   usage ();

 for (arg = 1; arg < argc  &&  argv[arg][0] == '-'; arg++)
 {
   int j = 0;
   while (argv[arg][++j])
     switch (argv[arg][j])
     {
       case 'e':		/* just echo */
         echo = TRUE;
         break;

       case 'q':		/* quote args */
         quote = TRUE;
         break;

       case 'o':		/* don't copy options */
         copyopt = FALSE;
         break;

       case 'd':		/* just expand dirs */
         wflags = WILD_DIRS;
         break;

       case 'f':		/* just expand files */
         wflags = WILD_FILES;
         break;

       case 's':		/* leave lonely stars untouched */
         wflags |= WILD_KEEPSTAR;
         break;

       case '0':
         cluster = HUGECLUSTER;
         break;
       case '1':
       case '2':
       case '3':
       case '4':
       case '5':
       case '6':
       case '7':
       case '8':
       case '9':
         cluster = argv[arg][j] - '0';
         break;

       default:
         PutStr ("wild: unknown option\n");
         usage ();
         break;
     }
 }

 wildoptions (wflags);

 if (!wildargs (&argc, &argv))
 {
   PutStr ("wild: error expanding arguments.\n");
   exit (10);
 }

 if (arg == argc)
   usage ();

 childarg = arg;

 while (arg < argc)
 {
   int end, opt = childarg;

   strncpy (cmd, argv[opt++], MAXCMD);		/* copy command name */

   if (copyopt)
     while (argv[opt][0] == '-')
     {
       strncat (cmd, " ", MAXCMD);
       strncat (cmd, argv[opt++], MAXCMD);	/* copy options */
     }

   if (arg == childarg)     /* if it is the 1st time, skip name & options */
     arg = opt;

   for (end = arg + cluster; arg < end && arg < argc; arg++)
   {
     strncat (cmd, (quote) ? " \"" : " ", MAXCMD);
     strncat (cmd, argv[arg], MAXCMD);
     if (quote)
       strncat (cmd, "\"", MAXCMD);
   }
   strncat (cmd, "\n", MAXCMD);

   if (echo)
     PutStr (cmd);
   else if (System (cmd, NULL))
   {
     PutStr ("wild: failed executing ");
     PutStr (cmd);
     exit (10);
   }
 }

 exit (0);
}
