/****************************************************************************
*****************************************************************************

FILE NAME: main.c 

ABSTRACT: Main entry point for depth-first search algorithm.

AUTHOR: Shantanu Tarafdar

*****************************************************************************
****************************************************************************/

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

#include <HLS/generic.h>
#include <HLS/graphlib.h>

#ifndef NULL
#define NULL 0
#endif

/* Global variables */

/* Input arguments */
char *infile = NULL;
char *outfile = NULL;
Boolean display = true;

/* Function prototype */
List *graph_dfs(Graph *g);

/*****************************************************************************

FUNCTION: usage

AUTHOR: Shantanu Tarafdar     DATE: Sep 29, 1997

ABSTRACT: Prints the usage for this program.

RETURNS: Nothing

*****************************************************************************/

static void usage(char *prog_name)
{
  fprintf(stderr, "Usage: %s [-nodisp] input_file\n", 
	  prog_name);
  fprintf(stderr, "       -nodisp    Prevents the graphs from being displayed\n"
	          "       input_file The input graph file.\n"
         );
  exit(-1);
}

/*****************************************************************************

FUNCTION: parse_args

AUTHOR: Shantanu Tarafdar     DATE: Sep 29, 1997

ABSTRACT: Parse the command line arguments. 

RETURNS: Nothing

*****************************************************************************/

static void parse_args(int argno, char *args[])
{
  int carg = 1;

  /* Sanity check */
  if ((argno < 2) || (args == NULL))
    return;

  /* While there are still unexamined arguments, do */
  while (carg < argno) {

    /* If this is the nodisplay option, set the flag */
    if (streq(args[carg], "-nodisp")) {
      carg++;
      display = false;
    }
    /* Else if this is not an option, it must be the input file name */
    else if (args[carg][0] != '-') {
      /* Get the input file name */
      infile = strdup(args[carg]);
      carg++;
    }
    else
      usage(args[0]);

  }

  /* Check that mandatory arguments have been supplied */
  if (!infile) {
    warn("You must supply an input graph file... Exiting.\n");
    exit(-1);
  }
    
}

/*****************************************************************************

FUNCTION: main

AUTHOR: Shantanu Tarafdar     DATE: Oct  2, 1996

ABSTRACT: Entry point. 

RETURNS: Nothing

*****************************************************************************/

int main (int argc, char *argv[])
{
  Graph *g;     /* Input graph */
  List *dfs_nl; /* DFS node list */
  Node *n;      /* Node on DFS node list */

  /* Read the command-line input arguments */
  parse_args(argc, argv);

  /* Read the graph */
  g = graph_read_from_file(infile);
  if (g == NULL) {
    warn("Failed while reading graph from \"%s\". Exiting!\n", infile);
    exit(1);
  }

  /* Display the graph */
  if (display) {
    msg("Displaying input graph...\n");
    graph_display(g);
    pause_til_keypress();
  }

  /* Execute the depth-first search */
  dfs_nl = graph_dfs(g);

  /* Print the ordered list of nodes */
  msg("\n");
  msg("Nodes of graph in depth-first order:\n");
  msg("====================================\n");
  l_fforeach(dfs_nl, n) {
    msg("\t%s\n", node_name(n));
  } l_endfor;
  msg("====================================\n");
  msg("\n");

  /* Prompt for completion */
  if (display) {
    pause_til_keypress();
  }

  /* Free up memory */
  l_free(dfs_nl);
  graph_free(g);

  exit(0);
}


