/*
 *  boot.c
 */

#include <stdio.h>
#include <stdlib.h>
#include <string.h>

#include <hourglass.h>
#include <os.h>
#include <osmodule.h>
#include <wimp.h>
#include <wimpreadsysinfo.h>

int main(int argc, char *argv[])
{
  FILE *file, *log;
  int error = 0, tasks, len;
  char command[200], s1[300], s2[200];
  os_error *err;
  wimp_t task = 0;

  if (argc < 3)
  {
    fprintf(stderr, "\007Syntax: boot <script> <log>\n");
    return 1;
  }

  file = fopen(argv[1], "r");
  if (!file)
  {
    fprintf(stderr, "boot: unable to open '%s' for input\n", argv[1]);
    return 1;
  }

  log = fopen(argv[2], "w");
  if (!file)
  {
    fprintf(stderr, "boot: unable to open '%s' for output\n", argv[2]);
    return 1;
  }

  xhourglass_on();

  if ((err = xwimpreadsysinfo_task_count(&tasks)) != NULL)
    fprintf(log, "Wimp_ReadSysInfo error %i: %s\n", err->errnum, err->errmess);

  if (tasks > 0)
    if ((err = xwimp_initialise(wimp_VERSION_RO2, "Boot", NULL, NULL, &task)) != NULL)
      fprintf(log, "Wimp_Initialise error %i: %s\n", err->errnum, err->errmess);

  fseek(file, SEEK_END, 0);
  len = ftell(file);
  rewind(file);

  while (fscanf(file, "%s", command) != EOF)
  {
    if (strcmp(command, "sysvar") == 0)
    {
      fscanf(file, "%s ", s1);
      fgets(s2, 300, file);
      s2[strlen(s2)-1] = 0;
      fprintf(log, "sysvar %s %s\n", s1, s2);
      if ((err = xos_set_var_val(s1, s2, (int) strlen(s2), 0,
                       os_VARTYPE_LITERAL_STRING, NULL, NULL)) != NULL)
      {
        fprintf(log, " error %i: %s\n", err->errnum, err->errmess);
        error = 1;
      }
    }
    else if (strcmp(command, "module") == 0)
    {
      fscanf(file, " ");
      fgets(s1, 300, file);
      s1[strlen(s1)-1] = 0;
//      fscanf(file, "%s", s1);
      fprintf(log, "module %s\n", s1);
      if ((err = xosmodule_load(s1)) != NULL)
      {
        fprintf(log, " error %i: %s\n", err->errnum, err->errmess);
        error = 1;
      }
    }
    else if (strcmp(command, "oscli") == 0)
    {
      fscanf(file, " ");
      fgets(s1, 300, file);
      s1[strlen(s1)-1] = 0;
      fprintf(log, "oscli %s\n", s1);
      system(s1);     /* good reason for this */
/*      if ((err = xos_cli(s1)) != NULL)
      {
        fprintf(log, " error %i: %s\n", err->errnum, err->errmess);
        error = 1;
      }*/
    }
    else if (strcmp(command, "start") == 0)
    {
      fscanf(file, " ");
      fgets(s1, 300, file);
      s1[strlen(s1)-1] = 0;
      fprintf(log, "start %s\n", s1);
      if ((err = xwimp_start_task(s1, NULL)) != NULL)
      {
        fprintf(log, " error 0x%x: %s\n", err->errnum, err->errmess);
        error = 1;
      }
    }
    else if (strcmp(command, "echo") == 0)
    {
      fscanf(file, " ");
      fgets(s1, 300, file);
      s1[strlen(s1)-1] = 0;
      fprintf(log, "echo %s\n", s1);
      printf("%s\n", s1);
    }
    else
    {
      fgets(s1, 300, file);
      fprintf(log, "unknown command %s\n", command);
      error = 1;
    }
    xhourglass_percentage(ftell(file) * 100 / len);
  }

  fclose(log);
  fclose(file);

  if (task > 0)
  {
    if (error)
    {
      sprintf(s1, "Filer_Run %s", argv[2]);
      xwimp_start_task(s1, NULL);
    }
    if ((err = xwimp_close_down(task)) != NULL)
      fprintf(log, "Wimp_CloseDown error 0x%x: %s\n", err->errnum, err->errmess);
  }
  else if (error)
  {
    fprintf(stderr, "\007Errors occurred, log file '%s'\007\n", argv[2]);
  }

  xhourglass_smash();

  return 0;
}
