/**********************************************************************
 * Stock Answer -> TechInfo program
 * by Bruce Lewis
 *
 * $Author: brlewis $
 * $Source: /mit/techinfodev/src/util/RCS/ti2scm.c,v $
 * $Header: /mit/techinfodev/src/util/RCS/ti2scm.c,v 1.2 1997/08/19 21:02:57 brlewis Exp $
 *
 * Copyright 1993 by the Massachusetts Institute of Technology.
 *
 * For copying and distribution information, please see the file
 * <mit-copyright.h>.
 **********************************************************************/
static char rcsid[] = "$Header: /mit/techinfodev/src/util/RCS/ti2scm.c,v 1.2 1997/08/19 21:02:57 brlewis Exp $";

#include <unistd.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <ti/ti.h>
#include <ti/node.h>
#include <ctype.h>
#include <com_err.h>

#define ABORT { com_err("ti2scm", code, "(%s)", ti_context); exit(1); }
#define USAGEFMT "Usage: %s [-r] [-q] [-server H] [-port P] -nodeid id\n"
int quietf=0, recursef = 0;

void
writestring(scheme_fd, str, len)
     int scheme_fd;
     char *str;
     int len;
{
  int i;

  if (len == -1) len = strlen(str);
  write(scheme_fd, " \"", 2);
  for(i=0; i<len; i++)
    {
      if (str[i] == '\\' || str[i] == '"')
	write(scheme_fd, "\\", 1);
      write(scheme_fd, &str[i], 1);
    }
  write(scheme_fd, "\"", 1);
  return;
}

long
ti2scm(tp, nd, depth)
     TI *tp;
     ti_node *nd;
     int depth;
{
  long code;
  ti_menu *child_menu;
  ti_node **grandchildren;
  int grandchild_count, i, j;

  /* Output misc info */
  printf("(%ld %d ", ti_id(nd), ti_date(nd));
  fflush(stdout);
  writestring(1, ti_topic(nd), -1);
  writestring(1, ti_title(nd), -1);
  writestring(1, ti_source(nd), -1);
  writestring(1, ti_locker(nd), -1);
  writestring(1, ti_file(nd), -1);

  printf(" (");
  if (recursef && ti_ismenu(nd))
    {
      /* Get grandchildren */
      if (0 != (code = ti_otl(tp, &child_menu, TI_BELOW, ti_id(nd), 1)))
	return(code);
      code = ti_rdmenu(child_menu, &grandchildren, &grandchild_count);
      ti_freemenu(child_menu);
      if (code) return(code);

      for(i=1; i< grandchild_count; i++)
	{
	  /* Indent */
	  printf("\n");
	  for(j=0; j <= depth; j++) printf("  ");

	  /* Recurse */
	  if (0 != (code=ti2scm(tp, grandchildren[i], depth+1)))
	    {
	      ti_freenodes(grandchildren);
	      return(code);
	    }
	}
      ti_freenodes(grandchildren);
    }
  printf("))");

  /* success */
  return 0L;
}


main(argc, argv)
     int argc;
     char *argv[];
{
  long code;
  char *server=NULL, *port=NULL;
  TI *tp;
  ti_node *nd;
  int arg;
  long nodeid;

  /* parse arguments */
  for(arg=1; arg<argc; arg++)
    {
      if (argv[arg][0] == '-')
	{
	  if (!strcmp(argv[arg], "-r"))
	    {
	      recursef = 1;
	      continue;
	    }
	  if (!strcmp(argv[arg], "-q"))
	    {
	      quietf = 1;
	      continue;
	    }
	  if (!strcmp(argv[arg], "-server"))
	    {
	      server = argv[++arg];
	      continue;
	    }
	  if (!strcmp(argv[arg], "-port"))
	    {
	      port = argv[++arg];
	      continue;
	    }
	  if (!strcmp(argv[arg], "-nodeid"))
	    {
	      nodeid = strtoul(argv[++arg], (char **)0, 10);
	      continue;
	    }
	  fprintf(stderr, USAGEFMT, argv[0]);
	  exit(1);
	}
      else
	{
	  fprintf(stderr, USAGEFMT, argv[0]);
	  exit(1);
	}
    }

  /* connect to the TechInfo server */
  if (0 != (code=ti_open(server, port, &tp))) ABORT;

  /* output the node given */
  if (code=ti_newnode(&nd)) ABORT;
  if (0 != (ti_nodeinfo(tp, nodeid, nd))) ABORT;
  if (0 != (code=ti2scm(tp, nd, 0))) ABORT;
  printf("\n");

  /* done */
  ti_close(tp);
  exit(0);
}
