#include <exec/types.h>
#include <dos/dostags.h>
#include <stdio.h>

#include <proto/dos.h>

int execute(char *cmd)
{
    long rc;

    while (*cmd == ' ') cmd++;
    if (strncmp(cmd, "exec", 4) == 0 && (cmd[4] == ' ' || cmd[4] == '\t')) cmd += 4;
    if ((rc = SystemTags(cmd, SYS_UserShell, TRUE, TAG_END)) == -1)
    {
	fprintf(stderr, "Failed to execute command %s\n", cmd);
	return 20;
    }
    return rc;
}

void main(int argc, char **argv)
{
  int command;
  char *command_string;
  char *program_name = argv[0];
  struct RDArgs *args;
  long opts[1];
  static char options[] = "";

  /* Throw out AmigaDOS args so that Input() is clean */
  if (args = ReadArgs(options, opts, NULL)) FreeArgs(args);

  command = 0;
  /* Simplistic argument parsing */
  argv++;
  argc--;
  while (argc > 0 && argv[0][0] == '-')
    {
      switch (argv[0][1])
	{
	case 'c':
	  if (argc == 1) goto usage;
	  command = 1;
	  command_string = argv[1];
	  argv++;
	  argc--;
	  break;
	case 'i': case'v':
	  /* ignored for now */
	  break;
	default: goto usage;
	}
      argc--;
      argv++;
    }
  if (argc != 0) goto usage;

  if (command) exit(execute(command_string));
  else exit(Execute("", Input(), NULL) ? 0 : 1);

 usage:
  fprintf(stderr, "%s [-i] [-c command]\n", program_name);
  exit(1);
}


/*
 * Local variables:
 * compile-command: "lc -L -v sh.c"
 * end:
 */
