;
;[1m[32m        ____     ___        ___     ___ ___ ___ ___ ____      ___ ___ ___[0m
;[1m[32m         /  /__//_         /__ /  //_ //__ /  //_    /  / / //_ //__//_  [0m
;[1m[32m        /  /  //__        ___//__//__/___//__//     /  /_/_//  //  \/__  [0m
;
;[1m[32m-----------------------------------------------------------------------------[0m
;   - FOR ABO'S CONTACT US AT:   BP:28 1170 WATERMAEL-BOITSFORT 1 BELGIUM!!...
;
;[1m[32m-----------------------------------------------------------------------------[0m
;                           - DON'T SEND ANY DISKS -
;
;[1m[32m-----------------------------------------------------------------------------[0m
;
;
;
;                      [33m THE LEGENDARY >> SUBSOFTWARE << IN 87/88/89/90/91!!...
;                      [1m[32m ------------------------------------------------------
;

/* PbConvert for use with NComm V1.92.  */
/* Public Domain by Torkel Lodberg 1991 */

#include <stdio.h>
#include <exec/types.h>
#include <exec/memory.h>
#include <intuition/intuition.h>
#include <proto/intuition.h>
#include <proto/graphics.h>
#include <proto/exec.h>
#include <fcntl.h>
#include <ctype.h>
#include <math.h>

#define OLD_FILETYPE_PHONE  5
#define NEW_FILETYPE_PHONE  6

static struct pb_Data2 {                 /* V1.8 file format    ***/
	UBYTE		name[41];
	UBYTE		phone[61];
	UBYTE		comment[41];
	UBYTE		pass_word[30];
	UBYTE		script_file[33];
	UBYTE		config_file[33];
	UBYTE		dummy;
	UBYTE   	char_set;	/*** 0-11		***/
	UBYTE		baud;		/*** 0-6 ( 300 - 19200)	***/
	UBYTE		data_length;	/*** 7 or 8		***/
	UBYTE		parity;		/*** None, odd, even	***/
	UBYTE		stop_bits;	/*** 1 - 2		***/
	UBYTE		duplex;		/*** Full - half	***/
};

static struct pb_Data {		/*** V1.92-> file format ***/
    UBYTE           name[41];
    UBYTE           phone[61];
    UBYTE           comment[41];
    UBYTE           pass_word[30];
    UBYTE           script_file[33];
    UBYTE           config_file[33];
    UBYTE           macro_file[33];
    UBYTE           char_set;	 /*** 0-12		***/
    UBYTE           baud;	 /*** 0-11 (300 - 115200)	***/
    UBYTE           data_length; /*** 7 or 8		***/
    UBYTE           parity;	 /*** None, odd, even	***/
    UBYTE           stop_bits;	 /*** 1 - 2		***/
    UBYTE           duplex;	 /*** Full - half	***/
    UBYTE           swap_del_bs; /*** Swap Del/BS       ***/
    UBYTE           protocol;    /*** Protocol (0-6)    ***/
    UBYTE           future[62];
};

static int fd = NULL;   /* File handles */
static int fe = NULL;

int wb_open;            /* Started from workbench? */

void cleanup(text)      /* Exit program cleanly */
char *text;
{
        if (wb_open) printf("\n");
        if (text) printf("%s\n",text);
        if (fd > 0) close(fd);
        if (fe > 0) close(fe);
        if (wb_open) Delay(100);
        exit(0);
}

void brk()              /* Called by every CTRL-C / D keypress */
{
        printf("*** BREAK\n");
        cleanup(NULL);
}

void main(argc, argv)   /* Main program */
int argc;
char *argv[];
{
	int	status, i;
	char	ch;
        struct  pb_Data data;
        struct  pb_Data2 data2;

        char    answer  [256];
        char    infile  [256];
        char    outfile [256];

        int     baudchange = NULL;
        int     datachange = NULL;
        int     paritychange = NULL;
        int     stopchange = NULL;
        int     protochange = NULL;

        int     oldformat = NULL, removeit = NULL;
        int     baud, data_length, parity, stop_bits, protocol;

        if (!argc) wb_open = TRUE;
           else wb_open = FALSE;

        if (onbreak(&brk)) cleanup(NULL);
        chkabort();

        if (argc < 3) {
                printf("PbConvert for use with NComm V1.92. May remove passwords\n");
                printf("and filenames from phonebooks, as well as converting the\n");
                printf("NComm V1.8 file format. Optional global serial changes.\n");
                printf("Public Domain by Torkel Lodberg 1991\n\n");

                if (argc) {
                   printf("   Usage: PbConvert <infile> <outfile> [-b] [-d] [-p] [-s] [-x] [-r] [-e]\n");
                   printf(" Options: [-b]nnnnnn where nnnnnn is the baudrate (300-115200)\n");
                   printf("          [-d]n where n is the data length (7-8)\n");
                   printf("          [-p]n where n is the parity (n)one, (o)dd, (e)ven\n");
                   printf("          [-s]n where n is the number of stop bits (1-2)\n");
                   printf("          [-x]n where n is the protocol (X/Y/B/G/Z/K/E)\n");
                   printf("          [-r]emove filenames and passwords\n");
                   printf("          [-e]xpect input file to be of old format (v1.8)\n\n");
                   cleanup(NULL);
                }
        }

        if (argc) {
           strcpy(infile, argv[1]);
           strcpy(outfile, argv[2]);

           for (i = 3; i < argc; i++) {
                   if (!strncmp(argv[i], "-b", 2)) {
                           baudchange = TRUE;
                           switch (atoi(&argv[i][2])) {
                                   case    300: baud = 0; break;
                                   case    600: baud = 1; break;
                                   case   1200: baud = 2; break;
                                   case   2400: baud = 3; break;
                                   case   4800: baud = 4; break;
                                   case   9600: baud = 5; break;
                                   case  19200: baud = 6; break;
                                   case  38400: baud = 7; break;
                                   case  57600: baud = 8; break;
                                   case  76800: baud = 9; break;
                                   case 115200: baud = 10; break;
                                   case  31250: baud = 11; break;

                                      default: cleanup("Invalid baud rate!");
                           }
                   }
                   if (!strncmp(argv[i], "-d", 2)) {
                           datachange = TRUE;
                           switch (atoi(&argv[i][2])) {
                                   case 8: data_length = 0; break;
                                   case 7: data_length = 1; break;
                                  default: cleanup("Invalid data length!");
                           }
                   }
                   if (!strncmp(argv[i], "-p", 2)) {
                           paritychange = TRUE;
                           switch (toupper(argv[i][2])) {
                                   case 'N': parity = 0; break;
                                   case 'O': parity = 1; break;
                                   case 'E': parity = 2; break;
                                    default: cleanup("Invalid parity!");
                           }
                   }
                   if (!strncmp(argv[i], "-s", 2)) {
                           stopchange = TRUE;
                           switch (atoi(&argv[i][2])) {
                                   case 1: stop_bits = 0; break;
                                   case 2: stop_bits = 1; break;
                                  default: cleanup("Invalid number of stop bits!");
                           }
                   }
                   if (!strncmp(argv[i], "-x", 2)) {
                           protochange = TRUE;
                           switch (toupper(argv[i][2])) {
                                   case 'X': protocol = 0; break;
                                   case 'Y': protocol = 1; break;
                                   case 'B': protocol = 2; break;
                                   case 'G': protocol = 3; break;
                                   case 'Z': protocol = 4; break;
                                   case 'K': protocol = 5; break;
                                   case 'E': protocol = 6; break;

                                  default: cleanup("Invalid protocol!");
                           }
                   }
                   if (!strncmp(argv[i], "-r", 2)) {
                           removeit = TRUE;
                   }
                   if (!strncmp(argv[i], "-e", 2)) {
                           oldformat = TRUE;
                   }
           }
        } else {
           strcpy(infile, "NComm:NComm.phone");
           strcpy(outfile, "RAM:NComm.phone");

           printf("Enter name of phonebook input file (Enter=NComm:NComm.phone): ");
           gets(answer);
           if (strlen(answer)) strcpy(infile, answer);

           printf("Enter name of phonebook output file (Enter=RAM:NComm.phone): ");
           gets(answer);
           if (strlen(answer)) strcpy(outfile, answer);

           oldformat = TRUE;
           printf("\nExpect old file format (Enter=Yes): ");
           gets(answer);
           if (toupper(answer[0]) == 'N') oldformat = FALSE;

           printf("\nNew baud rate (Enter=No Change): ");
           gets(answer);
           if (strlen(answer)) {
                baudchange = TRUE;
                switch (atoi(answer)) {
                   case    300: baud = 0; break;
                   case    600: baud = 1; break;
                   case   1200: baud = 2; break;
                   case   2400: baud = 3; break;
                   case   4800: baud = 4; break;
                   case   9600: baud = 5; break;
                   case  19200: baud = 6; break;
                   case  38400: baud = 7; break;
                   case  57600: baud = 8; break;
                   case  76800: baud = 9; break;
                   case 115200: baud = 10; break;
                   case  31250: baud = 11; break;

                   default: cleanup("Invalid baud rate!");
                }
           }

           printf("New data length (Enter=No Change): ");
           gets(answer);
           if (strlen(answer)) {
                datachange = TRUE;
                switch (atoi(answer)) {
                   case 8: data_length = 0; break;
                   case 7: data_length = 1; break;
                   default: cleanup("Invalid data length!");
                 }
           }

           printf("New parity (Enter=No Change): ");
           gets(answer);
           if (strlen(answer)) {
                paritychange = TRUE;
                switch (toupper(answer[0])) {
                   case 'N': parity = 0; break;
                   case 'O': parity = 1; break;
                   case 'E': parity = 2; break;
                   default: cleanup("Invalid parity!");
                }
           }

           printf("New number of stop bits (Enter=No Change): ");
           gets(answer);
           if (strlen(answer)) {
                stopchange = TRUE;
                switch (atoi(answer)) {
                   case 1: stop_bits = 0; break;
                   case 2: stop_bits = 1; break;
                   default: cleanup("Invalid number of stop bits!");
                }
           }

           printf("New protocol (Enter=No Change): ");
           gets(answer);
           if (strlen(answer)) {
                protochange = TRUE;
                switch (toupper(answer[0])) {
                   case 'X': protocol = 0; break;
                   case 'Y': protocol = 1; break;
                   case 'B': protocol = 2; break;
                   case 'G': protocol = 3; break;
                   case 'Z': protocol = 4; break;
                   case 'K': protocol = 5; break;
                   case 'E': protocol = 6; break;

                   default: cleanup("Invalid protocol!");
                }
           }

           removeit = FALSE;
           printf("\nRemove filenames and passwords (Enter=No): ");
           gets(answer);
           if (toupper(answer[0] == 'Y')) removeit = TRUE;
        }

	if((fd = open(infile, O_RDONLY, NULL)) == -1
	  ||read (fd, &ch, 1) != 1)
		if (argc) cleanup("Cannot open data-file");

        if (oldformat) {
        	if (ch != OLD_FILETYPE_PHONE)
        		cleanup("Error reading NComm.phone: Illegal file type");
                ch = NEW_FILETYPE_PHONE;
        } else {
        	if (ch < NEW_FILETYPE_PHONE)
        		cleanup("Error reading NComm.phone: Illegal file type");
        }

	if((fe = open(outfile, O_RDWR|O_CREAT|O_TRUNC, NULL)) == -1)
		cleanup("Cannot create file!");

	write (fe, &ch, 1);

        if (oldformat) {
        	while((status = read(fd, (char *) &data2, sizeof(struct pb_Data2))) == sizeof(struct pb_Data2)) {
                        if (removeit) {
                           memset(data2.pass_word, 0, sizeof(data2.pass_word));
                           memset(data2.script_file, 0, sizeof(data2.script_file));
                           memset(data2.config_file, 0, sizeof(data2.config_file));
                        }

                        if (baudchange) data2.baud = baud;
                        if (datachange) data2.data_length = data_length;
                        if (paritychange) data2.parity = parity;
                        if (stopchange) data2.stop_bits = stop_bits;

                        strcpy(data.name, data2.name);
                        strcpy(data.phone, data2.phone);
                        strcpy(data.comment, data2.comment);
                        strcpy(data.pass_word, data2.pass_word);
                        strcpy(data.script_file, data2.script_file);
                        strcpy(data.config_file, data2.config_file);
                        memset(data.macro_file, 0, sizeof(data.macro_file));

                        data.char_set = data2.char_set;
                        data.baud = data2.baud;
                        data.data_length = data2.data_length;
                        data.parity = data2.parity;
                        data.stop_bits = data2.stop_bits;
                        data.duplex = data2.duplex;
                        memset(data.future, 0, sizeof(data.future));

        		status = write(fe, (char *) &data, sizeof(struct pb_Data));
        		if(status != sizeof(struct pb_Data))
                                cleanup("Error while writing!");
                }
        } else {
        	while((status = read(fd, (char *) &data, sizeof(struct pb_Data))) == sizeof(struct pb_Data)) {
                        if (removeit) {
                           memset(data.pass_word, 0, sizeof(data.pass_word));
                           memset(data.script_file, 0, sizeof(data.script_file));
                           memset(data.config_file, 0, sizeof(data.config_file));
                           memset(data.macro_file, 0, sizeof(data.macro_file));
                        }

                        if (baudchange) data.baud = baud;
                        if (datachange) data.data_length = data_length;
                        if (paritychange) data.parity = parity;
                        if (stopchange) data.stop_bits = stop_bits;
                        if (protochange) data.protocol = protocol;

        		status = write(fe, (char *) &data, sizeof(struct pb_Data));
        		if(status != sizeof(struct pb_Data))
                                cleanup("Error while writing!");
                }
        }
	cleanup(NULL);
}
