/*
 * from simtel20 and CICA format to pcboard
 */

#if 0
1234567890123456789012345678901 [Simtel]
4DOSANN.ZIP   B    6864  910819  4DOS runs on DOS 5.0 and Norton DOS info
1234567890123456789012345678901 [CICA]
diskindx.txt	901031	Cumulative Index of the WRK Disks (below)
diskroot.zip	920610	Windows Resource Kit 4/3/92
---------->
9600TEXT.ZIP    16576  09-26-91  A pair of US Robotics text files about HST
                               | modems.
#endif

#include <ctype.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys\stat.h>

#define CICA

struct data {
	char filename[15];
	long filesize;
	int year;
	int month;
	int day;
	char description[900];
};

int pastheader = 0;
struct data line;
char path[128];

void
readline(void) {
	char buf[1000];
	char workbuf[1000];
	static char datebuf[5] = "12";
	char *r;
	
	if (NULL == gets(buf))
		exit(0);
	
	/* wait for header */
	if (! pastheader) {
#ifdef CICA		
		if (0 == strlen(buf))
			++pastheader;
#else		
		if (strstr(buf, "================"))
			++pastheader;
#endif
		return;
	}
	
	line.filename[0] = 0;
		
	if (strlen(buf) == 0) {
		fprintf(stderr, "zero length line\n");
		return;
	}
	
	if (strlen(buf) < 31) {
		fprintf(stderr, "warning: line too short:\n%s\n", buf);
	}
	
	strcpy(workbuf, buf);
	
	r = strtok(buf, "\t ");
	if (! r) {
		fprintf(stderr, "unknown line:\n%s\n", buf);
		return;
	}
	strcpy(line.filename, r);
	
	r += strlen(r) + 1;		/* skip name */
	while (isspace(*r))		/* skip white */
		++r;

#ifndef CICA	
	++r;					/* skip file type */
	
	while (isspace(*r))		/* skip white */
		++r;
	
	r = strtok(r, "\t ");
	if (! r) {
		fprintf(stderr, "unknown line:\n%s\n", buf);
		line.filename[0] = 0;
		return;
	}
	line.filesize = atol(r);
	r += strlen(r) + 1;
	
	while (isspace(*r))		/* skip white */
		++r;
#endif	
	
	datebuf[0] = *r;
	++r;
	datebuf[1] = *r;
	++r;
	line.year = atoi(datebuf);
	
	datebuf[0] = *r;
	++r;
	datebuf[1] = *r;
	++r;
	line.month = atoi(datebuf);
	datebuf[0] = *r;
	++r;
	datebuf[1] = *r;
	++r;
	line.day = atoi(datebuf);
	
	while (isspace(*r))		/* skip white */
		++r;
	
	if (*r)	
		strcpy(line.description, r);
	else
		line.description[0] = 0;
}

void
writeline(void) {
	char *r;
	char *p;
	FILE *foo;
	char buf[128];
	struct stat stat_buf;

	if (! line.filename[0])
		return;

#if 0	
9600TEXT.ZIP    16576  09-26-91  A pair of US Robotics text files about HST
                               | modems.
#endif

#ifdef CICA
	sprintf(buf, "%s\\%s", path, line.filename);
	if (NULL == (foo = fopen(buf, "r"))) {
		fprintf(stderr, "failed opening '%s'\n", buf);
		exit(1);
	}

	stat(buf, &stat_buf);

	fclose(foo);
		
	line.filesize = stat_buf.st_size;
#endif
	
	printf("%-13s%8ld  %02d-%02d-%02d  ",
		line.filename,
		line.filesize,
		line.month,
		line.day,
		line.year);
	
	/* write wrapping description */

	r = line.description;
	while (1) {
		if (strlen(r) <= 45) {		
			printf("%s\n", r);
			return;
		}

		p = r + 45;
		while (! isspace(*p))
			--p;
		
#if 0	
9600TEXT.ZIP    16576  09-26-91  A pair of US Robotics text files about HST
                               | modems.
#endif
		*p = 0;
		printf("%s\n%31s| ", r, " ");
		r = p + 1;
	}
}

void _Cdecl
main(int argc, char *argv[]) {
	
#ifdef CICA	
	fprintf(stderr,
		"usage: 2pcb <directory path> < 00_index.txt > files.bbs\n");
	strcpy(path, argv[1]);
#endif
	
	while (1) {
		readline();
		writeline();
	}
}
