/* Copyright 1996 Acorn Computers Ltd
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
/*main.c - Repeat *command*/

/*History

   27th Oct 1994 J R C Written

*/

/*From CLib*/
#include <kernel.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

/*From OSLib*/
#include "os.h"
#include "osfile.h"
#include "osfscontrol.h"
#include "osgbpb.h"
#include "wimp.h"
#include "wimpspriteop.h"

int main (void)

{  int context;
   struct {char *prog, *command, *directory; bool directories, applications,
         files; char *type, *tail; bool tasks, verbose;
         char argb [os_CLI_LIMIT + 1];} argl;
   osgbpb_INFO_STAMPED (os_FILE_NAME_LIMIT + 1) info;
   bool done_initialise = FALSE;
   bits file_type;
   os_error *error = NULL, message;
   char *tail;

   if ((error = xos_get_env (&tail, NULL, NULL)) != NULL)
      goto finish;

   if ((error = xos_read_args ("prog/a,command/a,directory/a,directories/s,"
         "applications/s,files/s,type/k,tail,tasks/s,verbose/s",
         tail, (char *) &argl, sizeof argl, NULL)) != NULL)
      goto finish;

   if (!argl.directories && !argl.applications && !argl.files && !argl.type)
      argl.files = argl.directories = TRUE;

   #if 0
   /*Canonicalise the name just to see it helps matters any.*/
   if ((error = xosfscontrol_canonicalise_path (argl.directory, directory,
         NULL, NULL, sizeof directory, NULL)) != NULL)
      goto finish;
   #endif /*it doesn't. also breaks under RO200. JRC 9th Jan 1995*/

   if (argl.type)
      if ((error = xosfscontrol_file_type_from_string (argl.type,
            &file_type)) != NULL)
         goto finish;

   if (argl.tasks)
   {  if ((error = xwimp_initialise (wimp_VERSION_RO2, "Repeat",
            NULL, NULL, NULL)) != NULL)
         goto finish;
      done_initialise = TRUE;
   }

   context = 0;
   while (context != osgbpb_NO_MORE)
   {  int count;
      if ((error = xosgbpb_dir_entries_info_stamped (argl.directory,
            (osgbpb_info_stamped_list *) &info, 1, context,
            sizeof info, "*", &count, &context)) != NULL)
         goto finish;
      if (!count) continue;
      if
      (  (  info.obj_type == osfile_IS_FILE &&
            (  argl.files ||
               (argl.type && info.file_type == file_type)
              /*^^^^^^^^^ avoid uninitialised variable. JRC 9th Jan 1995*/
         )  ) ||
         (  info.obj_type == osfile_IS_DIR &&
            (  argl.directories ||
               (argl.applications && info.name [0] == '!')
      )  )  )
      {  char cmd [os_CLI_LIMIT + 1];

         if (!argl.tail)
            sprintf (cmd, "%s %s.%s", argl.command, argl.directory,
                  info.name);
         else
            sprintf (cmd, "%s %s.%s %s", argl.command, argl.directory,
                  info.name, argl.tail);

         if (argl.tasks)
         {  if (argl.verbose)
            {  strcpy (message.errmess, cmd);
               (void) xwimp_report_error_by_category (&message,
                     wimp_ERROR_BOX_SHORT_TITLE | wimp_ERROR_BOX_NO_BEEP |
                     wimp_ERROR_BOX_LEAVE_OPEN |
                     wimp_ERROR_BOX_CATEGORY_INFO <<
                     wimp_ERROR_BOX_CATEGORY_SHIFT, "Repeat", "information",
                     wimpspriteop_AREA, "...", NULL);
               (void) xwimp_report_error (NULL, wimp_ERROR_BOX_CLOSE, NULL,
                     NULL);
            }

            if ((error = xwimp_start_task (cmd, NULL)) != NULL)
               goto finish;
         }
         else
         {  if (argl.verbose)
               fprintf (stderr, "Repeat: %s\n", cmd);

            if (system (cmd) != 0)
            {  error = (os_error *) _kernel_last_oserror ();
               goto finish;
   }  }  }  }

finish:
   if (done_initialise)
   {  os_error *error1 = xwimp_close_down (NULL);
      if (error == NULL) error = error1;
   }

   if (error != NULL)
   {  if (argl.tasks)
         (void) xwimp_report_error (error, NONE, "Repeat", NULL);
      else
         fprintf (stderr, "Repeat: %s\n", error->errmess);
      return 1;
   }
   else
      return 0;
}