#include <stdio.h>
#include <rpc/rpc.h>            /* always need this */
#include <master.h>
#include <time.h>
#include <abs_error.h>
#include "my_error.h"

extern CLIENT *cl;

char * location(char *argv[])
{
char * eptr; /* error pointer */
static char retbuf[1024];
int count=0,cnt=0;
loc_res lr;
abs_loc_t * temp;
abs_loc_t alt;
int bar;
FILE *fd;
char buf[ABS_STRING_MAX];
int i,len;
int ret;
search_t search;

	memset(retbuf,0,1024);
/*
 *	empty the table
 */

	memset(&lr,0,sizeof(loc_res));
	ret = show_location_1(&lr,cl);
	if ( lr.code != ABS_NO_MATCH) {
		if ( lr.code ) {
			TEST_ERROR(ret,lr.code,eptr);
			exit(-1); /* something really weird happened */
		}
		temp = lr.tag_res_u.value;
		while (temp != NULL ) {
			memset(&alt,0,sizeof(abs_loc_t));
			alt.tag = (char *) malloc(strlen(temp->tag)+1);
			if (!alt.tag)
				exit(1);
			strcpy(alt.tag,temp->tag);
			ret = delete_location_1(&alt,&bar,cl);
			if (ret || bar) {
				if (bar == ABS_DBMS_INTEGRITY_VIOLATION) {
					cleanup_loc(alt.tag,eptr,cl);
					ret = delete_location_1(&alt,&bar,cl);
					if (ret || bar) {
						TEST_ERROR(ret,bar,eptr);
					}
				} else
				TEST_ERROR(ret,bar,eptr);
			}
			temp = temp->next;
			free(alt.tag);
		}
	}
/*
 * empty show
 */
	ret = show_location_1(&lr,cl);
	CHECK_RESULT("a",retbuf,debug,"location",ABS_NO_MATCH,1,ret,lr.code,eptr);
/*
 * add a tag w/o a description
 */

	memset(&alt,0,sizeof(abs_loc_t));
	alt.tag = (char *) malloc(strlen("bar")+1);
	if (!alt.tag)
		exit(2);
	strcpy(alt.tag,"bar");
	ret = add_location_1(&alt,&bar,cl);
	CHECK_RESULT("b",retbuf,debug,"location",ABS_INVALID_SPEC,1,ret,bar,eptr);
	free(alt.tag);
/*
 * add a more complicated tag
 */	
	memset(&alt,0,sizeof(abs_loc_t));
	alt.tag = (char *) malloc(strlen("foo")+1);		
	if (!alt.tag)
		exit(3);
	strcpy(alt.tag,"foo");
	alt.description = (char *) malloc( strlen("this is a test")+1);
	if (!alt.description)
		exit(4);
	strcpy(alt.description,"this is a test");
	ret = add_location_1(&alt,&bar,cl);
	CHECK_RESULT("c",retbuf,debug,"location",ABS_SUCCESS,1,ret,bar,eptr);
/*
 * add it again
 */
	ret = add_location_1(&alt,&bar,cl);
	CHECK_RESULT("d",retbuf,debug,"location",ABS_DBMS_INTEGRITY_VIOLATION,1,ret,bar,eptr);
/*
 * delete it
 */
	ret = delete_location_1(&alt,&bar,cl);
	CHECK_RESULT("e",retbuf,debug,"location",ABS_SUCCESS,1,ret,bar,eptr);
/*
 * add it back
 */
	ret = add_location_1(&alt,&bar,cl);
	CHECK_RESULT("f",retbuf,debug,"location",ABS_SUCCESS,1,ret,bar,eptr);
	free(alt.tag);
	free(alt.description);
/*
 * add a null tag entry
 */
	memset(&alt,0,sizeof(abs_loc_t));                
	ret = add_location_1(&alt,&bar,cl);
	CHECK_RESULT("g",retbuf,debug,"location",ABS_INVALID_SPEC,1,ret,bar,eptr);
/*
 * delete a null tag entry
 */
	ret = delete_location_1(&alt,&bar,cl);
	CHECK_RESULT("h",retbuf,debug,"location",ABS_INVALID_SPEC,1,ret,bar,eptr);
/*
 * delete a non existant entry
 */
	alt.tag=(char *)malloc(strlen("012345678")+1);
	if (!alt.tag)
		exit(5);
	strcpy(alt.tag,"012345678");
	alt.description = (char *)malloc(strlen("it is the time for")+1);
	if (!alt.description)
		exit(6);
	strcpy(alt.description,"it is the time for");
	ret = delete_location_1(&alt,&bar,cl);
	CHECK_RESULT("i",retbuf,debug,"location",ABS_NO_MATCH,1,ret,bar,eptr);
/*
 * add a MAX_TAG -1 entry
 */
	ret = add_location_1(&alt,&bar,cl);
	CHECK_RESULT("j",retbuf,debug,"location",ABS_SUCCESS,1,ret,bar,eptr);
	free(alt.tag);
	free(alt.description);

/*
 * add a MAX_TAG entry
 */
	alt.tag=(char *)malloc(strlen("0123456789")+1);
	if (!alt.tag)
		exit(7);
	strcpy(alt.tag,"0123456789");
	alt.description = (char *)malloc(strlen("it is the time for")+1);
	if (!alt.description)
		exit(8);
	strcpy(alt.description,"it is the time for");
	ret = add_location_1(&alt,&bar,cl);
	CHECK_RESULT("k",retbuf,debug,"location",ABS_SUCCESS,1,ret,bar,eptr);
	free(alt.tag);
	free(alt.description);

/*
 * add a MAX_TAG +1 entry
 */
	alt.tag=(char *)malloc(strlen("01234567890")+1);
	if (!alt.tag)
		exit(9);
	strcpy(alt.tag,"01234567890");
	alt.description = (char *)malloc(strlen("it is the time for")+1);
	if (!alt.description)
		exit(10);
	strcpy(alt.description,"it is the time for");
	ret = add_location_1(&alt,&bar,cl);
	CHECK_RESULT("l",retbuf,debug,"location",1,0,ret,bar,eptr);
	free(alt.tag);
	free(alt.description);

/* 
 * add tag of 1
 */
	alt.tag=(char *)malloc(strlen("0")+1);
	if (!alt.tag)
		exit(11);
	strcpy(alt.tag,"0");
	alt.description = (char *)malloc(strlen("it is the time for")+1);
	if (!alt.description)
		exit(12);
	strcpy(alt.description,"it is the time for");
	ret = add_location_1(&alt,&bar,cl);
	CHECK_RESULT("m",retbuf,debug,"location",ABS_SUCCESS,1,ret,bar,eptr);
/*
 * delete tag of 1
 */
	ret = delete_location_1(&alt,&bar,cl);
	CHECK_RESULT("n",retbuf,debug,"location",ABS_SUCCESS,1,ret,bar,eptr);
	free(alt.tag);
	free(alt.description);
/* 
 * time to start filling in the other fields
 * a mail_contact of 1
 */
	alt.tag=(char *)malloc(strlen("a")+1);
	if (!alt.tag)
		exit(13);
	strcpy(alt.tag,"a");
	alt.description = (char *)malloc(strlen("it is the time for")+1);
	if (!alt.description)
		exit(14);
	strcpy(alt.description,"it is the time for");
	alt.mail_contact=(char *)malloc(strlen("a")+1);
	if (!alt.mail_contact)
		exit(15);
	strcpy(alt.mail_contact,"a");
	ret = add_location_1(&alt,&bar,cl);
	CHECK_RESULT("o",retbuf,debug,"location",ABS_SUCCESS,1,ret,bar,eptr);
	free(alt.tag);
	free(alt.description);
	free(alt.mail_contact);
/*
 * mail contact of 128 -1
 */
	alt.tag=(char *)malloc(strlen("b")+1);
	if (!alt.tag)
		exit(16);
	strcpy(alt.tag,"b");
	alt.description = (char *)malloc(strlen("it is the time for")+1);
	if (!alt.description)
		exit(17);
	strcpy(alt.description,"it is the time for");
	alt.mail_contact=(char *)malloc(strlen("0123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456")+1);
	if (!alt.mail_contact)
		exit(18);
	strcpy(alt.mail_contact,"0123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456");
	ret = add_location_1(&alt,&bar,cl);
	CHECK_RESULT("p",retbuf,debug,"location",ABS_SUCCESS,1,ret,bar,eptr);
	free(alt.tag);
	free(alt.description);
	free(alt.mail_contact);
/* 
 * mail contact of 128
 */
	alt.tag=(char *)malloc(strlen("c")+1);
	if (!alt.tag)
		exit(19);
	strcpy(alt.tag,"c");
	alt.description = (char *)malloc(strlen("it is the time for")+1);
	if (!alt.description)
		exit(20);
	strcpy(alt.description,"it is the time for");
	alt.mail_contact=(char *)malloc(strlen("01234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567")+1);
	if (!alt.mail_contact)
		exit(21);
	strcpy(alt.mail_contact,"01234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567");
	ret = add_location_1(&alt,&bar,cl);
	CHECK_RESULT("q",retbuf,debug,"location",ABS_SUCCESS,1,ret,bar,eptr);
	free(alt.tag);
	free(alt.description);
	free(alt.mail_contact);
/* 
 * mail contact of 129
 */
	alt.tag=(char *)malloc(strlen("d")+1);
	if (!alt.tag)
		exit(22);
	strcpy(alt.tag,"d");
	alt.description = (char *)malloc(strlen("it is the time for")+1);
	if (!alt.description)
		exit(23);
	strcpy(alt.description,"it is the time for");
	alt.mail_contact=(char *)malloc(strlen("012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678")+1);
	if (!alt.mail_contact)
		exit(24);
	strcpy(alt.mail_contact,"012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678");
	ret = add_location_1(&alt,&bar,cl);
	CHECK_RESULT("r",retbuf,debug,"location",1,0,ret,bar,eptr);
	free(alt.tag);
	free(alt.description);
	free(alt.mail_contact);
/* 
 * service of length 1 
 */
	alt.tag=(char *)malloc(strlen("e")+1);
	if (!alt.tag)
		exit(25);
	strcpy(alt.tag,"e");
	alt.description = (char *)malloc(strlen("it is the time for")+1);
	if (!alt.description)
		exit(26);
	strcpy(alt.description,"it is the time for");
	alt.mail_contact=(char *)malloc(strlen("vrt@mit.edu")+1);
	if (!alt.mail_contact)
		exit(27);
	strcpy(alt.mail_contact,"vrt@mit.edu");
	alt.service=(char *)malloc(strlen("a")+1);
	if (!alt.service)
		exit(28);
	strcpy(alt.service,"a");	
	ret = add_location_1(&alt,&bar,cl);
	CHECK_RESULT("s",retbuf,debug,"location",ABS_SUCCESS,1,ret,bar,eptr);
	free(alt.tag);
	free(alt.description);
	free(alt.mail_contact);
	free(alt.service);
/*
 * service TAG_MAX -1
 */
	alt.tag=(char *)malloc(strlen("f")+1);
	if (!alt.tag)
		exit(29);
	strcpy(alt.tag,"f");
	alt.description = (char *)malloc(strlen("it is the time for")+1);
	if (!alt.description)
		exit(30);
	strcpy(alt.description,"it is the time for");
	alt.mail_contact=(char *)malloc(strlen("vrt@mit.edu")+1);
	if (!alt.mail_contact)
		exit(31);
	strcpy(alt.mail_contact,"vrt@mit.edu");
	alt.service=(char *)malloc(strlen("012345678")+1);
	if (!alt.service)
		exit(32);
	strcpy(alt.service,"012345678");
	ret = add_location_1(&alt,&bar,cl);
	CHECK_RESULT("t",retbuf,debug,"location",ABS_SUCCESS,1,ret,bar,eptr);
	free(alt.tag);
	free(alt.description);
	free(alt.mail_contact);
	free(alt.service);
/*
 * service TAG_MAX
 */
	alt.tag=(char *)malloc(strlen("g")+1);
	if (!alt.tag)
		exit(33);
	strcpy(alt.tag,"g");
	alt.description = (char *)malloc(strlen("it is the time for")+1);
	if (!alt.description)
		exit(34);
	strcpy(alt.description,"it is the time for");
	alt.mail_contact=(char *)malloc(strlen("vrt@mit.edu")+1);
	if (!alt.mail_contact)
		exit(35);
	strcpy(alt.mail_contact,"vrt@mit.edu");
	alt.service=(char *)malloc(strlen("0123456789")+1);
	if (!alt.service)
		exit(36);
	strcpy(alt.service,"0123456789");
	ret = add_location_1(&alt,&bar,cl);
	CHECK_RESULT("u",retbuf,debug,"location",ABS_SUCCESS,1,ret,bar,eptr);
	free(alt.tag);
	free(alt.description);
	free(alt.mail_contact);
	free(alt.service);
/* 
 * service TAG_MAX +1
 */
	alt.tag=(char *)malloc(strlen("h")+1);
	if (!alt.tag)
		exit(37);
	strcpy(alt.tag,"g");
	alt.description = (char *)malloc(strlen("it is the time for")+1);
	if (!alt.description)
		exit(38);
	strcpy(alt.description,"it is the time for");
	alt.mail_contact=(char *)malloc(strlen("vrt@mit.edu")+1);
	if (!alt.mail_contact)
		exit(39);
	strcpy(alt.mail_contact,"vrt@mit.edu");
	alt.service=(char *)malloc(strlen("01234567890")+1);
	if (!alt.service)
		exit(40);
	strcpy(alt.service,"01234567890");
	ret = add_location_1(&alt,&bar,cl);
	CHECK_RESULT("v",retbuf,debug,"location",ABS_INVALID_SPEC,1,ret,bar,eptr);
	free(alt.tag);
	free(alt.description);
	free(alt.mail_contact);
	free(alt.service);
/*
 * msg_contact len 1
 */
	alt.tag=(char *)malloc(strlen("h")+1);
	if (!alt.tag)
		exit(41);
	strcpy(alt.tag,"h");
	alt.description = (char *)malloc(strlen("it is the time for")+1);
	if (!alt.description)
		exit(42);
	strcpy(alt.description,"it is the time for");
	alt.mail_contact=(char *)malloc(strlen("vrt@mit.edu")+1);
	if (!alt.mail_contact)
		exit(43);
	strcpy(alt.mail_contact,"vrt@mit.edu");
	alt.service=(char *)malloc(strlen("maybe")+1);
	if (!alt.service)
		exit(44);
	strcpy(alt.service,"maybe");	
	alt.msg_contact = (char *) malloc(strlen("a")+1);
	if (!alt.msg_contact)
		exit(45);
	strcpy(alt.msg_contact,"a");
	ret = add_location_1(&alt,&bar,cl);
	CHECK_RESULT("w",retbuf,debug,"location",ABS_SUCCESS,1,ret,bar,eptr);
	free(alt.tag);
	free(alt.description);
	free(alt.mail_contact);
	free(alt.service);
	free(alt.msg_contact);
/*
 * mag_contact MAX -1
 */
	alt.tag=(char *)malloc(strlen("i")+1);
	if (!alt.tag)
		exit(46);
	strcpy(alt.tag,"i");
	alt.description = (char *)malloc(strlen("it is the time for")+1);
	if (!alt.description)
		exit(47);
	strcpy(alt.description,"it is the time for");
	alt.mail_contact=(char *)malloc(strlen("vrt@mit.edu")+1);
	if (!alt.mail_contact)
		exit(48);
	strcpy(alt.mail_contact,"vrt@mit.edu");
	alt.service=(char *)malloc(strlen("maybe")+1);
	if (!alt.service)
		exit(49);
	strcpy(alt.service,"maybe");	
	alt.msg_contact = (char *) malloc(strlen("0123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456")+1);
	if (!alt.msg_contact)
		exit(50);
	strcpy(alt.msg_contact,"0123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456");
	ret = add_location_1(&alt,&bar,cl);
	CHECK_RESULT("x",retbuf,debug,"location",ABS_SUCCESS,1,ret,bar,eptr);
	free(alt.tag);
	free(alt.description);
	free(alt.mail_contact);
	free(alt.service);
	free(alt.msg_contact);
/*
 * msg_contact MAX
 */
	alt.tag=(char *)malloc(strlen("j")+1);
	if (!alt.tag)
		exit(51);
	strcpy(alt.tag,"j");
	alt.description = (char *)malloc(strlen("it is the time for")+1);
	if (!alt.description)
		exit(52);
	strcpy(alt.description,"it is the time for");
	alt.mail_contact=(char *)malloc(strlen("vrt@mit.edu")+1);
	if (!alt.mail_contact)
		exit(53);
	strcpy(alt.mail_contact,"vrt@mit.edu");
	alt.service=(char *)malloc(strlen("maybe")+1);
	if (!alt.service)
		exit(54);
	strcpy(alt.service,"maybe");	
	alt.msg_contact = (char *) malloc(strlen("01234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567")+1);
	if (!alt.msg_contact)
		exit(55);
	strcpy(alt.msg_contact,"01234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567");
	ret = add_location_1(&alt,&bar,cl);
	CHECK_RESULT("y",retbuf,debug,"location",ABS_SUCCESS,1,ret,bar,eptr);
	free(alt.tag);
	free(alt.description);
	free(alt.mail_contact);
	free(alt.service);
	free(alt.msg_contact);
/*
 * msg_contact MAX +1
 */
	alt.tag=(char *)malloc(strlen("k")+1);
	if (!alt.tag)
		exit(56);
	strcpy(alt.tag,"k");
	alt.description = (char *)malloc(strlen("it is the time for")+1);
	if (!alt.description)
		exit(57);
	strcpy(alt.description,"it is the time for");
	alt.mail_contact=(char *)malloc(strlen("vrt@mit.edu")+1);
	if (!alt.mail_contact)
		exit(58);
	strcpy(alt.mail_contact,"vrt@mit.edu");
	alt.service=(char *)malloc(strlen("maybe")+1);
	if (!alt.service)
		exit(59);
	strcpy(alt.service,"maybe");	
	alt.msg_contact = (char *) malloc(strlen("012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678")+1);
	if (!alt.msg_contact)
		exit(60);
	strcpy(alt.msg_contact,"012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678");
	ret = add_location_1(&alt,&bar,cl);
	CHECK_RESULT("z",retbuf,debug,"location",1,0,ret,bar,eptr);
	free(alt.tag);
	free(alt.description);
	free(alt.mail_contact);
	free(alt.service);
	free(alt.msg_contact);
/*
 * let us just fill in some extra entries for fun
 */
	fd = fopen("/usr/dict/words","r");
	if (fd) {
		for ( i=0;i<10;i++) { /* arbitrary for the moment */
			memset(&alt,0,sizeof(abs_loc_t));
			(void) fscanf(fd,"%s",buf);
			len = strlen(buf);
			alt.tag=(char *)malloc(len+1);
			if (!alt.tag)
				exit(61);
			if (len >ABS_MAX_TAG)
				strncpy(alt.tag,buf,ABS_MAX_TAG);
			else
				strcpy(alt.tag,buf);
			(void) fscanf(fd,"%s",buf);
			len = strlen(buf);
			alt.description = (char *)malloc(len+1);
			if (!alt.description)
				exit(62);
			strcpy(alt.description,buf);
			(void) fscanf(fd,"%s",buf);
			len = strlen(buf);
			alt.mail_contact=(char *)malloc(len+1);
			if (!alt.mail_contact)
				exit(63);
			strcpy(alt.mail_contact,buf);
			(void) fscanf(fd,"%s",buf);
			len = strlen(buf);
			alt.service=(char *)malloc(len+1);
			if (!alt.service)
				exit(64);
			strcpy(alt.service,buf);	
			(void) fscanf(fd,"%s",buf);
			len = strlen(buf);
			alt.msg_contact = (char *) malloc(len+1);
			if (!alt.msg_contact)
				exit(65);
			strcpy(alt.msg_contact,buf);
			ret = add_location_1(&alt,&bar,cl);
			free(alt.tag);
			free(alt.description);
			free(alt.mail_contact);
			free(alt.service);
			free(alt.msg_contact);
		}
	}
	fclose(fd);
/*
 * check to make sure the data is there
 */
	memset(&lr,0,sizeof(loc_res));
	ret = show_location_1(&lr,cl);
	CHECK_RESULT("aa",retbuf,debug,"location",ABS_SUCCESS,1,ret,lr.code,eptr);
/*
 * add a last "valid" entry
 */
	MALLOCANDSET(alt.tag,char,"e40",cnt);
	MALLOCANDSET(alt.description,char,"E40 basement",cnt);
	MALLOCANDSET(alt.mail_contact,char,"vrt",cnt);
	MALLOCANDSET(alt.service,char,"zephyr",cnt);
	MALLOCANDSET(alt.msg_contact,char,"vrt",cnt);
	ret = add_location_1(&alt,&bar,cl);
	free(alt.tag);
	free(alt.description);
	free(alt.mail_contact);
	free(alt.service);
	free(alt.msg_contact);
	MALLOCANDSET(alt.tag,char,"w91",cnt);
	MALLOCANDSET(alt.description,char,"w91 on the other side of campus",cnt);
	MALLOCANDSET(alt.mail_contact,char,"vrt",cnt);
	MALLOCANDSET(alt.service,char,"zephyr",cnt);
	MALLOCANDSET(alt.msg_contact,char,"vrt",cnt);
	ret = add_location_1(&alt,&bar,cl);
	free(alt.tag);
	free(alt.description);
	free(alt.mail_contact);
	free(alt.service);
	free(alt.msg_contact);
	MALLOCANDSET(alt.tag,char,"w20",cnt);
	MALLOCANDSET(alt.description,char,"Student Center",cnt);
	MALLOCANDSET(alt.mail_contact,char,"vrt",cnt);
	MALLOCANDSET(alt.service,char,"zephyr",cnt);
	MALLOCANDSET(alt.msg_contact,char,"vrt",cnt);
	ret = add_location_1(&alt,&bar,cl);
	free(alt.tag);
	free(alt.description);
	free(alt.mail_contact);
	free(alt.service);
	free(alt.msg_contact);
	MALLOCANDSET(alt.tag,char,"b11",cnt);
	MALLOCANDSET(alt.description,char,"The heart of campus",cnt);
	MALLOCANDSET(alt.mail_contact,char,"vrt",cnt);
	MALLOCANDSET(alt.service,char,"zephyr",cnt);
	MALLOCANDSET(alt.msg_contact,char,"vrt",cnt);
	ret = add_location_1(&alt,&bar,cl);
	free(alt.tag);
	free(alt.description);
	free(alt.mail_contact);
	free(alt.service);
	free(alt.msg_contact);

RETURN("location",debug);
}	


