#include <sys/types.h>
#include <stdio.h>
#include <string.h>
#include <sys/stat.h>
#define	TAR	"tar"
#define	ARC	"arc"
#define ZIP	"zip"
#define CONFIG	"/.config.bbs"
struct stat statbuf;
long atol();
main()
{
	FILE *instream;
	char	buffer[99], user[99], temp[99], bbs_soft[99], config_file[99];
	int	i, result;
	long	j;
	printf("\nInput the full path name for the users home directory: ");
	scanf("%s", user);
	printf("\n");
	strcpy(config_file, user);
	strcat(config_file, CONFIG);
	result = stat(config_file, &statbuf);
	if(!result)
	{
		printf("\nThere already is a configuration file, please delete it or rename it!\n");
		exit(1);
	}
	if((instream=fopen(config_file,"w")) == NULL)
	{
		printf("\nError opening configuration file for generation!\n");
		exit(1);
	}
	printf("\nInput the directory name where the bbs software can be found: ");
	scanf("%s", bbs_soft);
	printf("\n");
	strcpy(buffer,bbs_soft);
	strcat(buffer, "/welcome.bbs\n");
	fprintf(instream, "%s", buffer);
	strcpy(buffer,bbs_soft);
	strcat(buffer, "/bulletin.bbs\n");
	fprintf(instream, "%s", buffer);
	strcpy(buffer,bbs_soft);
	strcat(buffer, "/newuser.bbs\n");
	fprintf(instream, "%s", buffer);
	strcpy(buffer,bbs_soft);
	strcat(buffer,"/system.bbs\n");
	fprintf(instream, "%s", buffer);
	strcpy(buffer, bbs_soft);
	strcat(buffer, "/callers.bbs\n");
	fprintf(instream, "%s", buffer);
	strcpy(buffer, bbs_soft);
	strcat(buffer, "/lastcall.bbs\n");
	fprintf(instream, "%s", buffer);
	strcpy(buffer, bbs_soft);
	strcat(buffer, "/users.bbs");
	result = stat(buffer, &statbuf);
	if( result != 0 )
	{
		strcpy(temp, "> ");
		strcat(temp, buffer);
		(void)system(temp);
		strcpy(temp, "chown bbs ");
		strcat(temp, buffer);
		strcat(temp, "; chgrp root ");
		strcat(temp, buffer);
		strcat(temp, "; chmod 660 ");
		strcat(temp, buffer);
		(void)system(temp);
	}
	fprintf(instream, "%s\n", buffer);
	strcpy(buffer, bbs_soft);
	strcat(buffer, "/unixmsg.bbs\n");
	fprintf(instream, "%s", buffer);
	strcpy(buffer, bbs_soft);
	strcat(buffer, "/dlmsg.bbs\n");
	fprintf(instream, "%s", buffer);
	strcpy(buffer, bbs_soft);
	strcat(buffer, "/humor.bbs\n");
	fprintf(instream, "%s", buffer);
	strcpy(buffer, bbs_soft);
	strcat(buffer, "/help.bbs\n");
	fprintf(instream, "%s", buffer);
	strcpy(buffer, bbs_soft);
	strcat(buffer, "/helpfile.bbs\n");
	fprintf(instream, "%s", buffer);
	strcpy(buffer, bbs_soft);
	strcat(buffer, "/helpmsg.bbs\n");
	fprintf(instream, "%s", buffer);
	strcpy(buffer, bbs_soft);
	strcat(buffer, "/userpriv.bbs");
	result = stat(buffer, &statbuf);
	if( result != 0 )
	{
		strcpy(temp, "> ");
		strcat(temp, buffer);
		(void)system(temp);
		strcpy(temp, "chown bbs ");
		strcat(temp, buffer);
		strcat(temp, "; chgrp root ");
		strcat(temp, buffer);
		strcat(temp, "; chmod 660 ");
		strcat(temp, buffer);
		(void)system(temp);
	}
	fprintf(instream, "%s\n", buffer);
	strcpy(buffer, bbs_soft);
	strcat(buffer, "/mainpriv.bbs\n");
	fprintf(instream, "%s", buffer);
	strcpy(buffer, bbs_soft);
	strcat(buffer, "/filepriv.bbs\n");
	fprintf(instream, "%s", buffer);
	strcpy(buffer, bbs_soft);
	strcat(buffer, "/question.bbs\n");
	fprintf(instream, "%s", buffer);
	strcpy(buffer, bbs_soft);
	strcat(buffer, "/answer.bbs\n");
	fprintf(instream, "%s", buffer);
	strcpy(buffer, bbs_soft);
	strcat(buffer, "/features.bbs\n");
	fprintf(instream, "%s", buffer);
	strcpy(buffer, bbs_soft);
	strcat(buffer, "/listfile.bbs\n");
	fprintf(instream, "%s", buffer);
	strcpy(buffer, bbs_soft);
	strcat(buffer, "/today > /tmp/today\n");
	fprintf(instream, "%s", buffer);
	strcpy(buffer, bbs_soft);
	strcat(buffer, "/\n");
	fprintf(instream, "%s", buffer);
	strcpy(buffer, bbs_soft);
	strcat(buffer, "/areas.bbs\n");
	fprintf(instream, "%s", buffer);
	strcpy(buffer, bbs_soft);
	strcat(buffer, "/msgareas.bbs\n");
	fprintf(instream, "%s", buffer);
	strcpy(buffer, user);
	strcat(buffer, "/\n");
	fprintf(instream, "%s", buffer);
	printf("\nInput the full path name for checksum: ");
	scanf("%s", temp);
	strcpy(buffer, temp);
	strcat(buffer, " r \n");
	fprintf(instream, "%s", buffer);
	strcpy(buffer, temp);
	strcat(buffer, " s \n");
	fprintf(instream, "%s", buffer);
	printf("\nInput the full path name for crc: ");
	scanf("%s", temp);
	strcpy(buffer, temp);
	strcat(buffer, " r \n");
	fprintf(instream, "%s", buffer);
	strcpy(buffer, temp);
	strcat(buffer, " s \n");
	fprintf(instream, "%s", buffer);
	printf("\nInput the full path name for ymod: ");
	scanf("%s", temp);
	strcpy(buffer, temp);
	strcat(buffer, " r \n");
	fprintf(instream, "%s", buffer);
	strcpy(buffer, temp);
	strcat(buffer, " s \n");
	fprintf(instream, "%s", buffer);
	printf("\nInput the full path name for rb: ");
	scanf("%s", temp);
	strcpy(buffer, temp);
	strcat(buffer, "\n");
	fprintf(instream, "%s", buffer);
	printf("\nInput the full path name for sb: ");
	scanf("%s", temp);
	strcpy(buffer, temp);
	strcat(buffer, " -k \n");
	fprintf(instream, "%s", buffer);
	printf("\nInput the full path name for rz: ");
	scanf("%s", temp);
	strcpy(buffer, temp);
	strcat(buffer, "\n");
	fprintf(instream, "%s", buffer);
	printf("\nInput the full path name for sz: ");
	scanf("%s", temp);
	strcpy(buffer, temp);
	strcat(buffer, " \n");
	fprintf(instream, "%s", buffer);
	printf("\nInput the full path name for kermit: ");
	scanf("%s", temp);
	strcpy(buffer, temp);
	strcat(buffer, " -i -q -s \n");
	fprintf(instream, "%s", buffer);
	strcpy(buffer, temp);
	strcat(buffer, " -i -w -q -r\n");
	fprintf(instream, "%s", buffer);
	strcpy(buffer, "ls ");
	strcat(buffer, user);
	strcat(buffer, " > /tmp/flist\n");
	fprintf(instream, "%s", buffer);
	printf("\nInput the full path name for sealink: ");
	scanf("%s", temp);
	strcpy(buffer, temp);
	strcat(buffer, " -r \n");
	fprintf(instream, "%s", buffer);
	strcpy(buffer, temp);
	strcat(buffer, " -s \n");
	fprintf(instream, "%s", buffer);
	printf("\nInput the first name of the sysop: ");
	scanf("%s", temp);
	fix_name(temp);
	strcpy(buffer, temp);
	printf("\nInput the last name of the sysop: ");
	scanf("%s", temp);
	fix_name(temp);
	strcat(buffer, " ");
	strcat(buffer, temp);
	fprintf(instream, "%s\n", buffer);
	printf("\nInput the number of SECONDS you will allow for a login: ");
	scanf("%s", temp);
	j = atol(temp);
	if(j < 60L ) j=60L;
	if(j > 32767L) j=32767L;
	sprintf(buffer, "%ld", j);
	fprintf(instream, "%s\n", buffer);
	printf("\nInput the number of SECONDS you will allow for usage: ");
	scanf("%s", temp);
	j = atol(temp);
	if(j < 60L ) j=60L;
	if(j > 32767L) j=32767L;
	sprintf(buffer, "%ld", j);
	fprintf(instream, "%s\n", buffer);
	printf("\nInput the number of SECONDS you will allow for response: ");
	scanf("%s", temp);
	j = atol(temp);
	if(j < 60L ) j=60L;
	if(j > 32767L) j=32767L;
	sprintf(buffer, "%ld", j);
	fprintf(instream, "%s\n", buffer);
	printf("\nInput the privilege level for new users: ");
	scanf("%s", temp);
	j = atol(temp);
	if(j < 1L ) j=1L;
	if(j > 32767L) j=32767L;
	sprintf(buffer, "%ld", j);
	fprintf(instream, "%s\n", buffer);
	printf("\nInput the maximum privilege level allowed to users: ");
	scanf("%s", temp);
	j = atol(temp);
	if(j < 1L ) j=1L;
	if(j > 32767L) j=32767L;
	sprintf(buffer, "%ld", j);
	fprintf(instream, "%s\n", buffer);
	printf("\nInput the maximum number of seconds allowed to users: ");
	scanf("%s", temp);
	j = atol(temp);
	if(j < 1L ) j=1L;
	if(j > 32767L) j=32767L;
	sprintf(buffer, "%ld", j);
	fprintf(instream, "%s\n", buffer);
	printf("\nInput the daily number of KILOBYTES allowed to users: ");
	scanf("%s", temp);
	j = atol(temp);
	if(j < 1L ) j=1L;
	if(j > 32767L) j=32767L;
	sprintf(buffer, "%ld", j);
	fprintf(instream, "%s\n", buffer);
	printf("\nInput the full path name for tar: ");
	scanf("%s", temp);
	strcpy(buffer, temp);
	strcat(buffer, " -tvf \n");
	fprintf(instream, "%s", buffer);
	printf("\nInput the full path name for zcat( link to compress): ");
	scanf("%s", temp);
	strcpy(buffer, temp);
	strcat(buffer, " \n");
	fprintf(instream, "%s", buffer);
/*
 *	Starting with version 7.102, external arc and zip programs are
 *	no longer needed! To be compatible with the older .config.bbs files,
 *	we will just put something there for old time sake!
 *
 *	printf("\nInput the full path name for arc: ");
 *	scanf("%s", temp);
 *	strcpy(buffer, temp);
 *	strcat(buffer, " -v \n");
 *	fprintf(instream, "%s", buffer);
 *	printf("\nInput the full path name for unzip: ");
 *	scanf("%s", temp);
 *	strcpy(buffer, temp);
 *	strcat(buffer, " -v \n");
 *	fprintf(instream, "%s", buffer);
*/

	fprintf(instream, "/usr/bin/arc -v \n");   /*  fake arc  */
	fprintf(instream, "/usr/bin/zip -v \n");   /*  fake zip  */


	printf("\nInput the full path name of the dev driver for local monitoring: ");
	scanf("%s", buffer);
	fprintf(instream, "%s\n", buffer);
	printf("\nInput the full path name for the console device driver: ");
	scanf("%s", buffer);
	fprintf(instream, "%s\n", buffer);
	printf("\nInput the full path name for the SHELL and command switch");
	printf("\nyou wish to use for the U(nix) option in the main menu.");
	printf("\nExample: /bin/sh ");
	printf("\n?  ");
	scanf("%s", buffer);
	fprintf(instream, "%s -i\n", buffer);
logloop:
	printf("\nDo wish to have the software do a message scan when the user\n");
	printf("logs on? 1=yes, 0=no ..... ");
	scanf("%s", temp);
	i = atoi(temp);
	if( i > 1 || i < 0) goto logloop;
	sprintf(buffer, "%d", i);
	fprintf(instream, "%s\n", buffer);
	printf("\nInput the full path name to be used for the USENET reader.\n");
	printf("Example: /usr/bin/readnews : ");
	scanf("%s", buffer);
	fprintf(instream, "%s\n", buffer);
	printf("\nInput the full path name to be used for the USENET poster.\n");
	printf("Example: /usr/bin/postnews : ");
	scanf("%s", buffer);
	fprintf(instream, "%s\n", buffer);
	fclose(instream);
	printf("\n\n\nConfiguration file has been generated!\n\n\n");
}
fix_name(adr) char *adr ;
	{
	char *adrs, *str_ptr;
	char tempbf[30];
	adrs = adr ;
	while(*adrs)
		{
		*adrs = tolower(*adrs) ;
		adrs++ ;
		}
	while (1)
		{
		str_ptr = strrchr(adr, ' ');
		if(str_ptr == NULL) break;
		*str_ptr++ = '\0';
		strcpy(tempbf, adr);
		strcat(tempbf, str_ptr);
		strcpy(adr,tempbf);
		}
	*adr = toupper(*adr) ;
	}
