/************************************************************************/
/*	Routing Infomation Generator	Release 1.0	1995/7/10	*/
/*									*/
/*		module 	: main routine				       	*/
/*		file	: rigen_main.c				        */
/*									*/
/*   Copyright (c) 1995-1997 by Systems Development Laboratory Hitachi,	*/
/*   Ltd. All rights reserved.						*/
/*----------------------------------------------------------------------*/
/*	UPDATE HISTORY							*/
/*									*/
/************************************************************************/
static char copyright[]=
  "@(#)Copyright (c) 1995-1997 by Systems Development Laboratory Hitachi,Ltd.\n All rights researved.\n";

static char version[]=  "Routing Information Generator  Version 1.0";

#ifdef VXWORKS

#include "stdioLib.h"
#include "rps_token.h"
#include "netDrv.h"
#include "hostLib.h"
#include "strLib.h"

#else

#include <stdio.h>
#include <signal.h>
#include "rps_token.h"

#endif

char *token_arg();

void usage();
void print_copyright();

char cmd_file[64];
char config_file[64];
char net_file[64];
char out_file[64];

int used_memory = 0;

#ifndef VXWORKS

main(argc,argv)
int argc;
char **argv;

#else

rps_task(file)
char *file;

#endif
{
    int cmd;
    FILE *netfp,*conffp,*cmdfp;
    int line;
    char tmparg[64],command[64];
    char arg[32],*tmp,*rt;
    int pid;
    int status;
    void rtgen_end();
    
#ifndef VXWORKS

    if(argc < 2) {
	usage();
	exit();
    }
	
    while(--argc) {
	argv++;
	switch(*(*argv+1)) {
	case 'i' : argv++;
	           strcpy(cmd_file, *argv);
	           --argc;
	           break;
	case 'o' : argv++;
	           strcpy(out_file, *argv);
	           --argc;
	           break;
        default  : break;
	}

    }

#else
/*
     netDevCreate (argv[1], argv[1], 0);

    strcpy(tmparg, argv[1]);
    strcat(tmparg, argv[2]);
*/
    strcpy(cmd_file,file);

#endif

    print_copyright();

    if((cmdfp = fopen(cmd_file, "r")) == NULL) {
	printf("command file open error\n");
	return -1;
    }

    time_init();

    line=1;
    used_memory = 0;
    while((cmd = get_cmd(cmdfp,command)) != EOF) {
	if(cmd != NOCOMMAND) {
	    if(!strcmp("start",command))  {
			break;
	    }
	    else {
		log_trace(stderr,"Not find start command in (%s) \n"
			  ,command,line);
		break;
	    }
	}
/*	else if(cmd == -1 || cmd == T_CMD_UNKNOWN) {
	    log_trace(stderr,"Unknown Command <%s> at line %d\n",command,line);
	}*/
	line++;
    }

    
    tmp=arg;
    if( get_arg(cmdfp,tmp) != EOF) {
        strcpy(config_file,token_arg(&tmp,ARG_SYMBOL));
        strcpy(net_file,token_arg(&tmp,ARG_SYMBOL));

        case_load(net_file);
        config_load(config_file);
    }
    
    all_interface_up();

    log_trace(stderr,"\n Routing table dump (%s) \n",out_file);
    rtable_dump(out_file);
    
    fclose(cmdfp);

#ifdef VXWORKS
    
    printf("used memory = %d byte \n",used_memory);

#endif
    
/*    time_term();*/
    sock_term();
#ifdef VXWORKS

/*    sys_timer_delete();*/

#endif

    log_trace(stderr,"\n Finished ..\n\n");

}


void usage()
{
  fprintf(stderr,"Usage: rigen -i [cmd_file] -o [outfile]\n");
  exit(1);
}

char *get_cmd_file()
{
    return cmd_file;
}


void print_copyright()
{
    log_trace(stderr,"\n %s",version);
    log_trace(stderr,
	    "\n All Rights Reserved, Copyright(c)1995, Hitachi, Ltd.\n");

}

void rtgen_end()
{
    printf("signal received\n");
    exit(0);
}
