# include "data.h"

int pop (int argc, char **argv) {
  
  int N;
  char *var;
  char *line;
  Queue *queue;

  var = (char *) NULL;
  if (N = get_argument (argc, argv, "-var")) {
    remove_argument (N, &argc, argv);
    var = strcreate (argv[N]);
    remove_argument (N, &argc, argv);
  }

  if (argc != 2) {
    fprintf (stderr, "USAGE: pop (queue) [-var variable]\n");
    return (FALSE);
  }

  /* will create a queue if none exists */
  queue = FindQueue (argv[1]);
  if (queue == NULL) {
    fprintf (stderr, "ERROR: queue %s not found\n", argv[1]);
    return (FALSE);
  }

  line = PopQueue (queue);
  if (line == NULL) {
    fprintf (stderr, "ERROR: queue %s is empty\n", argv[1]);
    return (FALSE);
  }

  if (var == (char *) NULL) {
    fprintf (stderr, "%s\n", line);
  } else {
    set_str_variable (argv[2], line);
  }

  free (line);
  return (TRUE);
}

