/*
 * Copyright (c) 1988, 1990 Regents of the University of California.
 * All rights reserved.
 *
 * Redistribution and use in source and binary forms are permitted provided
 * that: (1) source distributions retain this entire copyright notice and
 * comment, and (2) distributions including binaries display the following
 * acknowledgement:  ``This product includes software developed by the
 * University of California, Berkeley and its contributors'' in the
 * documentation or other materials provided with the distribution and in
 * all advertising materials mentioning features or use of this software.
 * Neither the name of the University nor the names of its contributors may
 * be used to endorse or promote products derived from this software without
 * specific prior written permission.
 * THIS SOFTWARE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR IMPLIED
 * WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED WARRANTIES OF
 * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
 */

#ifndef lint
char copyright[] =
"@(#) Copyright (c) 1988, 1990 Regents of the University of California.\n\
 All rights reserved.\n";
#endif /* not lint */

#ifndef lint
static char sccsid[] = "@(#)main.c	5.1 (Berkeley) 9/14/90";
#endif /* not lint */

#include <sys/types.h>
#include <string.h>

#include "ring.h"
#include "externs.h"
#include "defines.h"
#ifdef AMI_TCP
#include  <bsdsocket.h>
#else
#include  <ss/socket.h>
#endif

/*
 * Initialize variables.
 */
void
tninit()
{
    init_terminal();

    init_network();
    
    init_telnet();

    init_sys();

}

int	autologin;
#ifdef __SASC
	int Enable_Abort;
#endif
#ifdef AMI_TCP
	struct Library *SocketBase = 0;
#else
	struct Library *SockBase = 0;
#endif
struct Library *OpenLibrary();
FILE *log_file = 0;
long bytesin,
	 bytesout;

void closesockets(void)
{
#ifdef AMI_TCP	
	if (SocketBase)
		{
		CloseLibrary(SocketBase);
		SocketBase = 0;
		}
#else
	if (SockBase)
		{
		cleanup_sockets();
		CloseLibrary(SockBase);
		SockBase = 0;
		}
#endif
	if (log_file)
		fclose(log_file);
}

/*
 * main.  Parse arguments, invoke the protocol or command parser.
 */

main(argc, argv)
	int argc;
	char *argv[];
{
	extern char *optarg;
	extern int optind;
	int ch;
	char *user, *strrchr();
	char fname[128];
	extern int Enable_Abort;

	Enable_Abort = 0;

#ifdef AMI_TCP
	SocketBase = OpenLibrary("bsdsocket.library",0L);
	if (! SocketBase)
		exit(0);
	SetErrnoPtr(&errno,sizeof(errno));
#else
	SockBase = OpenLibrary("socket.library",0L);
	if (! SockBase)
		exit(0);
	setup_sockets(3,&errno);
#endif

	atexit(closesockets);

	tninit();		/* Clear out things */

	TerminalSaveState();

	if (prompt = strrchr(argv[0], '/'))
		++prompt;
	else
		prompt = argv[0];

	user = NULL;
	autologin = 0;
	while ((ch = getopt(argc, argv, "ade:l:n:")) != EOF) {
		switch(ch) {
		case 'a':
			autologin = 1;
			break;
		case 'd':
			debug = 1;
			break;
		case 'e':
			set_escape_char(optarg);
			break;
		case 'L':
			strcpy(fname,optarg);
			log_file = fopen(fname,"a");
			break;
		case 'n':
				SetNetTrace(optarg);
			break;
		case '?':
		default:
			usage();
			/* NOTREACHED */
		}
	}
	argc -= optind;
	argv += optind;

	if (argc) {
		char *args[7], **argp = args;

		if (argc > 2)
			usage();
		*argp++ = prompt;
		if (user) {
			*argp++ = "-l";
			*argp++ = user;
		}
		*argp++ = argv[0];		/* host */
		if (argc > 1)
			*argp++ = argv[1];	/* port */
		*argp = 0;

		if (setjmp(toplevel) != 0)
			Exit(0);
		if (tn(argp - args, args) == 1)
			return (0);
		else
			return (1);
	}
	(void)setjmp(toplevel);
	for (;;) {
			command(1, 0, 0);
	}
}

usage()
{
	fprintf(stderr, "usage: %s [-a] [ [-l user] host-name [port] ]\n",
	    prompt);
	exit(1);
}
