#include "packdef.h"
#include <stdio.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <signal.h>
#include <fcntl.h>
#include <sgtty.h>
#include <string.h>
#include <ctype.h>
#include <pwd.h>

static char     linkh[150], linkm[150], linkc[150];
static char     header[150], messages[150], crossref[150];
static char     newheader[150], newmsg[150], newxref[150];
static char     log[150], newlog[150];
static int      table[1001];
static int      goodmsg;
static char     foo[150];

main()
{
	struct passwd  *p;
	struct passwd  *getpwnam();
	char           *ptr;
	int             counter, result;
	strcpy(foo, "bbs");
	while (1) {
		setpwent();
		p = getpwnam(foo);
		if (p == NULL) {
			printf("Unable to locate user %s, please enter the proper name: ", foo);
			scanf("%s", foo);
			endpwent();
		} else
			break;
	}
	strcpy(foo, p->pw_shell);
	ptr = strrchr(foo, '/');
	if (ptr == NULL) {
		printf("ERROR in shell location!\n");
		exit(1);
	}
	ptr++;
	*ptr = '\0';
	strcpy(ORGPATH, foo);
	strcpy(MSGS, ORGPATH);
	strcat(MSGS, MSGS1);

	for (counter = 1; counter < 100; counter++) {

		result = change_msga(counter);
		if (result != 1)
			exit(0);
		goodmsg = 0;
		strcpy(linkh, m_pathname);
		strcat(linkh, LINKH);
		strcpy(log, m_pathname);
		strcat(log, MSGLOG);
		strcpy(newlog, m_pathname);
		strcat(newlog, NEWLOG);
		strcpy(linkm, m_pathname);
		strcat(linkm, LINKM);
		strcpy(linkc, m_pathname);
		strcat(linkc, LINKC);
		strcpy(header, m_pathname);
		strcat(header, HEADER);
		strcpy(messages, m_pathname);
		strcat(messages, MESSAGES);
		strcpy(crossref, m_pathname);
		strcat(crossref, CROSSREF);
		strcpy(newheader, m_pathname);
		strcat(newheader, NEWHEADER);
		strcpy(newmsg, m_pathname);
		strcat(newmsg, NEWMSG);
		strcpy(newxref, m_pathname);
		strcat(newxref, NEWXREF);
		rremove();
		hdrreadr();
		hdrreadw();
		hdrwrt();
		cmd_s();
		nlog();
		rrename();
	}

}


savemsg()
{				/* save a message */
	int             fd;


	if ((fd = msgopenw(2)) == ERROR) {	/* open i/o */
		return (ERROR);
	}
	msgwrt(fd);		/* write the message */
	msgclose(fd);

	strcpy(h_date, msg_date);
	hdrwrt();		/* update the header file */
}
/* end of function		 */

cmd_s()
{
	int             q_msg, msgn, fd, msgnmbr, rc;
	msgnmbr = 1;
	for (fd = 0; fd <= 1000; fd++)
		table[fd] = 0;
	if ((fd = msgopenr(0)) == ERROR) {	/* open input */
		return (ERROR);
	}
	q_msg = 1;
	hdrreadr();
	while ((rc = msgread(fd, q_msg++)) != ERROR) {	/* read until eof or
							 * error */
		msgn = q_msg - 1;
		if (rc == 0) {
			goto lpp;	/* not a valid msg */
		}
		reduce();
		hdrreadw();
		goodmsg = 1;
		printf("\n%s%d\n", "Saving new message number ", msgnmbr);
		table[msgn] = msgnmbr;
		msgnmbr++;
		savemsg();
lpp:
		hdrreadr();
	}
	if (!goodmsg) {
		strcpy(msg_delete, "5");
		reduce();
		hdrreadw();
		table[msgn] = msgnmbr;
		savemsg();
	}
	msgclose(fd);
}
reduce()
{
	int             len;
	len = strlen(msg_text);
	len--;
	while (msg_text[len] == 32) {
		msg_text[len] = '\0';
		len--;
	}
}
nlog()
{
	FILE           *input, *output;
	char            firstz[20], lastz[20], msgz[7];
	char            buffer[99], qwerty[10];
	int             i, newi;
	qwerty[0] = '\0';
	if ((input = fopen(log, "r")) == NULL) {
		printf("\nError opening message.log???\n");
		exit(1);
	}
	if ((output = fopen(newlog, "w")) == NULL) {
		printf("\nError opening Omessage.log??\n");
		exit(1);
	}
	while (fscanf(input, "%s%s%s", firstz, lastz, msgz) != EOF) {
		i = atoi(msgz);
		while (1) {
			if (table[i] || !i)
				break;
			--i;
		}
		newi = table[i];
		itoa(qwerty, newi);
		strcpy(buffer, firstz);
		strcat(buffer, " ");
		strcat(buffer, lastz);
		strcat(buffer, " ");
		strcat(buffer, qwerty);
		i = strlen(qwerty);
		i = 6 - i;
		while (i) {
			strcat(buffer, "_");
			--i;
		}
		fprintf(output, "%s\n", buffer);
	}
	fclose(input);
	fclose(output);
}
rrename()
{
	int             result;
	printf("\nRenaming files ...............");
	result = unlink(linkh);
	result = unlink(linkm);
	result = unlink(linkc);
	result = link(log, linkh);
	result = unlink(log);
	result = link(newlog, log);
	result = unlink(newlog);
	result = link(linkh, newlog);
	result = unlink(linkh);
	result = link(header, linkh);
	result = link(messages, linkm);
	result = link(crossref, linkc);
	result = unlink(header);
	result = unlink(messages);
	result = unlink(crossref);
	result = link(newheader, header);
	result = link(newmsg, messages);
	result = link(newxref, crossref);
	result = unlink(newheader);
	result = unlink(newmsg);
	result = unlink(newxref);
	result = link(linkh, newheader);
	result = link(linkm, newmsg);
	result = link(linkc, newxref);
	result = unlink(linkh);
	result = unlink(linkm);
	result = unlink(linkc);
	strcpy(msg_text, CHMOD);
	strcat(msg_text, m_pathname);
	strcat(msg_text, CHMOD1);
	result = system(msg_text);
	printf("DONE\n");
}
rremove()
{
	int             result;
	result = unlink(newheader);
	result = unlink(newmsg);
	result = unlink(newxref);
}
/* end of function		 */
