/*
 * Refer to file UPDNODES.H for copyright and version information
 */

#include <stdio.h>
#include <stdlib.h>
#ifdef	VMS
#include <unixio.h>
#include <file.h>
#endif	/* VMS */
#include "updnodes.h"

char * delta_file_name = DEFDELTA;
char * base_file_name = DEFBASE;
char * out_file_name = DEFOUT;
int verbose = LOG_MAJOR;
int linemax = 240;

#ifndef	oldcc
static void open_files(void);
static void process_args(int argc, char ** argv);
static void usage(void);
#else
static void open_files();
static void process_args();
static void usage();
#endif	/* oldcc */

FILE * delta;
FILE * base;
FILE * out;

char major_tag[STD_STR_LEN];
int major_tag_length = 0;

int err_count = 0;

#ifndef	oldcc
int main(int argc, char ** argv)
#else
int main(argc, argv)
int argc;
char ** argv;
#endif	/* oldcc */
{
    process_args(argc, argv);
    open_files();

    log_line(LOG_UPDATE, "%s Version %s-%s started.\n", PROGRAM_NAME, VERSION, BASELEVEL);
    merge_net();
    log_line(LOG_UPDATE, "%s Version %s-%s ended.\n", PROGRAM_NAME, VERSION, BASELEVEL);
    if (err_count) {
	log_line(LOG_MAJOR, "Error(s) detected during run.\n");
	exit(SYSERROR);
    } else
	return(SYSGOOD);
}

#ifndef	oldcc
static void open_files(void)
#else
static void open_files()
#endif	/* oldcc */
{
#ifdef	VMS
    delta = fopen(delta_file_name, "r", "mbf=15", "rop=RAH");
#else
    delta = fopen(delta_file_name, "r");
#endif	/* VMS */
    if (!delta) {
	error("Unable to open update file \"%s\".\n", delta_file_name);
	exit(SYSERROR);
    }

#ifdef	VMS
    base = fopen(base_file_name, "r", "mbf=15", "rop=RAH");
#else
    base = fopen(base_file_name, "r");
#endif	/* VMS */
    if (!base) {
	fclose(delta);
	error("Unable to open base file \"%s\".\n", base_file_name);
	exit(SYSERROR);
    }

#ifdef	VMS
    out = fopen(out_file_name, "w", "mbf=31", "rop=WBH", "deq=1024", "fop=cbt, tef", "alq=4096");
#else
    out = fopen(out_file_name, "w");
#endif	/* VMS */
    if (!out) {
	fclose(delta);
	fclose(base);
	error("Unable to open output file \"%s\".\n", out_file_name);
	exit(SYSERROR);
    }
}

#ifndef	oldcc
static void process_args(int argc, char ** argv)
#else
static void process_args(argc, argv)
int argc;
char **argv;
#endif	/* oldcc */
{
    int i;
    int files_found;

    files_found = 0;
    for (i = 1; i < argc; i++) {
	if (argv[i][0] == '-') {
	    switch(argv[i][1]) {
		case 'v':
		    verbose = LOG_UPDATE;
		    if (argv[i][2] == 'a') {
			verbose = LOG_MUNDANE;
		    }
		    break;
		case 'l':
		    linemax = atoi(argv[i]+2);
		    if (linemax < 1) {
			error("Invalid line length \"%s\".\n", argv[i]+2);
			exit(SYSERROR);
		    }
		    break;
		case '?':
		    usage();
		default:
		    usage();
	    }
	} else {
	    if (files_found > 2) {
		error("Too many files specified.\n");
		exit(SYSERROR);
	    }
	    switch (files_found) {
		case 0:
		    delta_file_name = argv[i];
		    break;
		case 1:
		    base_file_name = argv[i];
		    break;
		case 2:
		    out_file_name = argv[i];
		    break;
	    }
	    files_found++;
	}
    }
}

#ifndef	oldcc
static void usage(void)
#else
static void usage()
#endif	/* oldcc */
{
    printf("This is %s Version %s-%s.\n\n", PROGRAM_NAME, VERSION, BASELEVEL);
    printf("Usage: UPDNODES [opt] [[[upd_file] in_file] out_file]\n");
    printf("\nWhere:\n");
    printf("Options can be:\n");
    printf("\t-v[a]\t- verbose messages. 'a' means log informational messages also.\n");
    printf("\t-l#\t- line length to be used for output files. Default: 240.\n");
    printf("\t-?\t- display this list.\n");
    printf("\nupd_file\tthe delta file to be applied. Default: %s.\n",
	DEFDELTA);
    printf("in_file\t\tthe input nodes file. Default: %s.\n", DEFBASE);
    printf("out_file\tthe output nodes file. Default: %s\n", DEFOUT);
    exit(SYSERROR);
}
