# include "basic.h"
# include "scheduler.h"

int task_periods (int argc, char **argv) {

  int N, Poll, Exec, Timeout;
  float PollValue, ExecValue, TimeoutValue;
  char *taskname;
  Task *task;

  Poll = FALSE;
  if (N = get_argument (argc, argv, "-poll")) {
    Poll = TRUE;
    remove_argument (N, &argc, argv);
    PollValue = atof(argv[N]);
    remove_argument (N, &argc, argv);
  }

  Exec = FALSE;
  if (N = get_argument (argc, argv, "-exec")) {
    Exec = TRUE;
    remove_argument (N, &argc, argv);
    ExecValue = atof(argv[N]);
    remove_argument (N, &argc, argv);
  }

  Timeout = FALSE;
  if (N = get_argument (argc, argv, "-timeout")) {
    Timeout = TRUE;
    remove_argument (N, &argc, argv);
    TimeoutValue = atof(argv[N]);
    remove_argument (N, &argc, argv);
  }

  if (argc != 1) {
    fprintf (stderr, "USAGE: periods [-poll value] [-exec value] [-timeout value]\n");
    fprintf (stderr, "  define time periods for this task\n");
    return (FALSE);
  }

  taskname = GetCurrentTask ();
  if (taskname == NULL) {
    fprintf (stderr, "ERROR: not defining or running a task\n");
    return (FALSE);
  }
  task = FindTask (taskname);
  if (task == NULL) {
    fprintf (stderr, "ERROR: current task not found??\n");
    return (FALSE);
  }

  if (Poll) task[0].poll_period = PollValue;
  if (Exec) task[0].exec_period = ExecValue;
  if (Timeout) task[0].timeout_period = TimeoutValue;

  return (TRUE);
}


/** 
    careful with this: the command is supposed to be realized 
    for the Job, not the Task (at execution)
    the code is right, but who calls it when needs to be clarified.
 **/

