/*
 *------------------------------------------------------------------
 *
 * $Source: /afs/net.mit.edu/dapps/project/techinfodev/src/srv_ti/RCS/main.c,v $
 * $Revision: 1.3 $
 * $Date: 92/08/25 17:09:21 $
 * $State: Exp $
 * $Author: ark $
 * $Locker: ark $
 *
 * $Log:	main.c,v $
 * Revision 1.3  92/08/25  17:09:21  ark
 * Saber & WAIS cleanup
 * 
 * 
 * Revision 1.2  92/08/24  15:45:58  ark
 * WAIS searching added to client & server (J command)
 * 
 * Revision 1.1  92/07/22  11:09:13  ark
 * Saber loads quietly; ANSI use standardized; command line options; no behavioral changes
 * 
 * Revision 1.0  92/07/10  12:32:21  ark
 * Initial revision
 * 
 * Revision 1.1  91/07/15  10:40:19  thorne
 * Initial revision
 * 
 *------------------------------------------------------------------
 */

#ifndef lint
#ifndef SABER
static char *rcsid_foo_c = "$Header: /afs/net.mit.edu/dapps/project/techinfodev/src/srv_ti/RCS/main.c,v 1.3 92/08/25 17:09:21 ark Exp Locker: ark $";
#endif  SABER
#endif  lint
 /* entry point for TechInfo browser*/
/*
  Copyright (C) 1989 by the Massachusetts Institute of Technology

   Export of this software from the United States of America is assumed
   to require a specific license from the United States Government.
   It is the responsibility of any person or organization contemplating
   export to obtain such a license before exporting.

WITHIN THAT CONSTRAINT, permission to use, copy, modify, and
distribute this software and its documentation for any purpose and
without fee is hereby granted, provided that the above copyright
notice appear in all copies and that both that copyright notice and
this permission notice appear in supporting documentation, and that
the name of M.I.T. not be used in advertising or publicity pertaining
to distribution of the software without specific, written prior
permission.  M.I.T. makes no representations about the suitability of
this software for any purpose.  It is provided "as is" without express
or implied warranty.

  */
#include <sys/types.h>  /* for mac? */

#include <ctype.h>
#include <stdio.h>
#include <sys/wait.h>
#include <sys/file.h>
#include <strings.h>
#include <signal.h>

#include "pdb.h"
#include "disp.h"

#ifdef CLIENT
#include "nlist_c.h" /* */
#include "network.h"
#else
#include "web.h" 
#include "nlist.h"
#endif

extern char * ttyio_getinput();
extern int sock;
int home_sock;
int pid = 0; /* pid of gif viewer */
void do_quit();
int      reconnected;  /* reconnected to server */
int save_cmd = FALSE;
char *pips_server_host,*pips_server_port;
int debug = 0;



log_session()
{
	static long	start_time = 0L, end_time = 0L;
	long		connect_time;
	FILE		*fp;
	char		hname[65];
	char		*getlogin();

	if (!start_time)
		time(&start_time);
	else {
		time(&end_time);
		connect_time = end_time - start_time;
	}
	fp = fopen(LOG_FILE, "a");
	if (!fp) {
		perror(LOG_FILE);
		return;
	}
	gethostname(hname, 65);

	if (!end_time)
		fprintf(fp, "%5d  %-8s %.24s\n",
			getpid(), hname,
			ctime(&start_time));
	else
		fprintf(fp, "%5d  %-8s %.24s, active %4.1f minutes\n",
			getpid(), hname, ctime(&end_time),
			(float) connect_time / 60.0);
	fclose(fp);
}
void
lost_contact()
{
  int rc;
  signal(SIGINT, SIG_IGN);
  signal(SIGPIPE, SIG_IGN);
  signal(SIGHUP, SIG_IGN);
  signal(SIGWINCH, SIG_IGN);
  reconnected = TRUE;		

  /* Only the sweb_init call itself was commented out previously; 
     if we're not going to reestablish the connection, we might as 
     well quit.  Made it quit here 8/25/92  ark */

  /*
    rc = sweb_init(pips_server_host,pips_server_port); 
  if (rc < 0)			
    {
      printf("Lost contact with the server");
      sleep(10);
      exit(0);
    }
  */

  printf("Lost connection with the server, exiting.\n");
  exit(1);
  /* signal(SIGINT, do_quit);
     signal(SIGPIPE, lost_contact); 
     signal(SIGHUP, do_quit);
     signal(SIGWINCH, disp_resize); */
}
void
do_quit()
{
#ifndef CLIENT
	log_session(); 
#endif
	
	disp_shutdown();
	system("/bin/stty lnext '^V'"); /* need to make this an ioctl call 
				    , it is so that ^V will work as down */
	system("clear"); 
#ifdef CLIENT
	write(sock,"q\n",2);
	close(sock);
#endif
	if (pid != 0)
	  kill(pid,SIGHUP);
	exit(0);
}
void
do_close()
{
        write(sock,"q\n",2);
        close(sock);
        return;
      }


int	Debug = 0;
int numservers;

/*
 * The hope is that initialize is re-callable.
 */

initialize()
{
	int rc;

        SERVER = FALSE;
	if (Debug)
	printf("\n\nStartup size: %.2fK\n", (float) ((long) sbrk(0)) / 1024.0);

	/*
	 * Should free any already allocated memory!  (in the case of a
	 * restart)
         */
#ifdef CLIENT
    
	rc = nlist_est_conn(pips_server_host,pips_server_port);
	home_sock = sock; /* set our original server socket */
	if (rc < 0) /* couldn't connect */
	  {
	    printf("\nCould not connect to server. Call %s for help.\n",HELP_PHONE);
	    exit(1);
	  }
	if (check_version())
	  printf("This client is out of date.\n");
        numservers = load_servers();
	write(sock,"w:2:0:1\n", 8);
	nlist_recv();
#else 
	NODE	*root_node;

	root_node = web_load();
	if (!root_node)
		return 0;
	nlist_reset();
	nlist_add(root_node, 0);
	nlist_expand(1); 
#endif


	if (Debug) {
		printf("Current size: %.2fK\n", (float) ((long) sbrk(0)) / 1024.0);
		putstr("Hit return to begin ->"), flush(), getchar();
	} else
		putstr(" Done."), flush();

	disp_init();
	signal(SIGINT, do_quit);
	signal(SIGPIPE, lost_contact); 
	signal(SIGHUP, do_quit);
	signal(SIGWINCH, disp_resize);
	system("/bin/stty lnext '^I'"); 
/* need to make this an ioctl call , it is so that ^V will work as down */

	/*
	 * should free and reinitialize screen also, in case window
	 * dimensions have changed, in fact, we should also catch
	 * SIGWINCH to automatically resize when the window changes.
	 */

	 flush();

	 return 1;
}



main(argc, argv)
int	argc;
char    *argv[];
{
	extern char	*getinput();		/* from ttyio.c */
        extern int menu;
	char		cmd_line[128];
	int    arg;

	printf("%s is initializing ...",THENAME),flush();
	  pips_server_port = PORT;  /* default server port */
	  pips_server_host = PIPS_SERVER; /* default server hostname */
	  strcpy (cmd_line, "main");  /* default command on startup */
	  
	  for (arg = 1; arg < argc; arg++) {
	    if ( !strcmp(argv[arg], "-debug"))
	      Debug++;
	    else if (arg + 1 < argc && !strcmp(argv[arg],"-c")) {
	      arg++;
	      cmd_adv(); 
	      strcpy(cmd_line, argv[arg]);
	    }
	    else if (arg + 1 < argc && !strcmp(argv[arg], "-port")) {
	      arg++;
	      pips_server_port = argv[arg];
	    }
	    else if (arg + 1 < argc && !strcmp(argv[arg], "-server")) {
	      arg++;
	      pips_server_host = argv[arg];
	    }
	  }

        menu = MENU;

#ifndef CLIENT
	log_startup("",LOG_MACHINE);
	/* log_session(); */
#endif
#ifdef GETFILES
	log_startup("hydrd",LOG_MACHINE);
	/* log_session(); */ 
#endif
	if (!initialize())
		puts("Fatal initialization error -- aborting."), exit(1);
/* got here debugging */

        save_cmd = TRUE;
	parse_cmd(cmd_line);	/* Pips Intro */
	for (;;) {

		/*
		 * Don't do display output if pending input.  This makes
		 * for interruptable re-display, which makes things much
		 * faster in the case of say 2 seconds of auto-repeat on
		 * the down arrow.
		 */

	/*	if (!ttyio_charp())  */   /* uncomment if above func wanted */
			disp_update();
		save_cmd = TRUE;
		parse_cmd( ttyio_getinput(cmd_line) );

	}


	/*NOTREACHED*/

	/*
	 * the function do_quit() contains the only program exit point
	 */
}


