/**********************************************************************
 * TechInfo demolition program
 * by Bruce Lewis
 *
 * $Author: brlewis $
 * $Source: /afs/net.mit.edu/dev/project/techinfodev/src/util/RCS/tidemolish.c,v $
 * $Header: /afs/net.mit.edu/dev/project/techinfodev/src/util/RCS/tidemolish.c,v 1.1 93/07/23 15:21:31 brlewis Exp $
 *
 * Copyright 1993 by the Massachusetts Institute of Technology.
 *
 * For copying and distribution information, please see the file
 * <mit-copyright.h>.
 **********************************************************************/
#include <mit-copyright.h>

static char rcsid_newprov_c[] = "$Header: /afs/net.mit.edu/dev/project/techinfodev/src/util/RCS/tidemolish.c,v 1.1 93/07/23 15:21:31 brlewis Exp $";

#define USAGEFMT "Usage: %s [-quiet] [-server H] [-port P] nodeid ...\n"

#define ABORT { com_err(argv[0], code, "(%s)", ti_context); exit(1); }
#define Debug(x) if (debugf) { printf x; }
int quietf=0, debugf = 0;

#include <stdio.h>
#include <string.h>
#include <ti/ti.h>

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

  /* parse arguments */
  for(arg=1; arg<argc; arg++)
    {
      if (argv[arg][0] == '-')
	{
	  if (!strcmp(argv[arg], "-debug"))
	    {
	      debugf = 1;
	      continue;
	    }
	  if (!strcmp(argv[arg], "-quiet"))
	    {
	      quietf = 1;
	      continue;
	    }
	  if (!strcmp(argv[arg], "-server"))
	    {
	      server = argv[++arg];
	      Debug(("Server: %s\n", server));
	      continue;
	    }
	  if (!strcmp(argv[arg], "-port"))
	    {
	      port = argv[++arg];
	      Debug(("Port: %s\n", port));
	      continue;
	    }
	  fprintf(stderr, USAGEFMT, argv[0]);
	  exit(1);
	}

      /* check that single numeric arg is provided */
      if (node_found || !(condemned_id=atol(argv[arg])))
	{
	  fprintf(stderr, USAGEFMT, argv[0]);
	  exit(1);
	}
      node_found=1;
    }

  /* make sure one node was specified */
  if (!node_found)
    {
      fprintf(stderr, USAGEFMT, argv[0]);
      exit(1);
    }

  /* connect to the TechInfo server */
  if (code=ti_open(server, port, &tp)) ABORT;
  Debug(("Connected to %s %s\n", ti_host(tp), ti_port(tp)));

  /* authenticate */
  if (code=ti_auth(tp, TI_KERBEROS, "")) ABORT;
  Debug(("Primary source: %s\n", ti_source1(tp)));

  /* find node to demolish */
  if (code=ti_newnode(&nd)) ABORT;
  if (code = ti_nodeinfo(tp, condemned_id, nd)) ABORT;

  /* verify */
  if (!quietf)
    {
      printf("Node %ld, \"%s\", is owned by %s.\n",
	     condemned_id, ti_title(nd), ti_source(nd));
      if (!say_yes("Demolish?", 0))
	{
	  printf("No action taken.\n");
	  ti_close(tp);
	  exit(0);
	}
    }

  /* demolish */
  if (code = ti_rmr(tp, -1, nd)) ABORT;

  /* done */
  if (!quietf) printf("Demolished %s.\n", ti_title(nd));
  ti_close(tp);
  exit(0);
}

/**********************************************************************
 * say_yes(question, d)
 *    char *question
 *    int d;  [default 1 or 0]
 *
 *   + prints question
 *   + gets yes or no answer
 *
 * Returns:
 *   + 1 if yes
 *   + 0 if no
 **********************************************************************/

say_yes(question, d)

     char *question;
     int d;
{
  char answer[512];

  printf("%s (y/n, default: %c) ", question, d ? 'y' : 'n');
  switch(*(fgets(answer, 15, stdin))) {
  case 'y':
  case 'Y':
    return(1);
  case 'n':
  case 'N':
    return(0);
  default:
    return(d);
  }
}
