#ifndef lint
static char *RCSid = "$Header: /usr/brule/guest/empire/empire/emp2/client/login.c,v 1.1 1994/11/30 22:51:52 empire Exp $";
#endif /* not lint */

/*
 * login.c
 *
 * Log in to empire host
 *
 * Dave Pare, 1989
 */

#include "misc.h"
#include "proto.h"

#include <ctype.h>
#include <stdio.h>

int
login(s, uname, cname, cpass, kill_proc)
	int	s;
	s_char	*uname;
	s_char	*cname;
	s_char	*cpass;
	int	kill_proc;
{
	extern	s_char *getpass();
	s_char	buf[1024];
	s_char	country[64];
	s_char	*ptr;
	s_char	*p;

	if (!expect(s, C_INIT, buf))
		return 0;
	(void) sendcmd(s, USER, uname);
	if (!expect(s, C_CMDOK, buf))
		return 0;
	if (cname == 0) {
		cname = country;
		(void) printf("Country name? ");
		*cname = 0;
		if (gets(cname) == 0 || *cname == 0)
			return 0;
	}
	(void) sendcmd(s, COUN, cname);
	if (!expect(s, C_CMDOK, buf)) {
		(void) fprintf(stderr, "empire: no such country\n");
		return 0;
	}
	if (cpass == 0) {
		cpass = getpass("Your name? ");
		if (cpass == NULL || *cpass == '\0')
			return 0;
	}
	(void) printf("\n");
	(void) sendcmd(s, PASS, cpass);
	bzero(cpass, strlen(cpass));		/* for core dumps */
	if (!expect(s, C_CMDOK, buf)) {
		(void) fprintf(stderr, "Bad password\n");
		return 0;
	}
	if (kill_proc) {
		(void) sendcmd(s, KILL, (s_char *)0);
		(void) printf("\n\t-=O=-\n");
		(void) expect(s, C_EXIT, buf);
		fprintf(stderr, "%s\n", buf);
		return 0;
	}
	(void) sendcmd(s, PLAY, (s_char *)0);
	(void) printf("\n\t-=O=-\n");
	if (!expect(s, C_INIT, buf)) {
		fprintf(stderr, "%s\n", buf);
		return 0;
	}
	for (ptr = buf; !isspace(*ptr); ptr++)
		;
	ptr++;
	p = index(ptr, '\n');
	if (p != 0)
		*p = 0;
	if (atoi(ptr) != CLIENTPROTO) {
		printf("Empire client out of date; get new version!\n");
		printf("   this version: %d, current version: %d\n",
			CLIENTPROTO, atoi(ptr));
	}
	return 1;
}
