/* 
 * $RCSfile: init.c,v $
 *
 * x-kernel v3.3
 *
 * Original Copyright (c) 1993,1991,1990,1996  Arizona Board of Regents
 * Modifications Copyright (c) 1993 Massachusetts Board of Regents
 *
 * $Log: init.c,v $
 * Revision 1.2  1996/01/30 20:52:53  slm
 * Updated copyright and version.
 *
 * Revision 1.1  1995/07/29  02:42:45  slm
 * Initial revision
 *
 * Revision 1.4.1.1.1.1  1994/11/12  19:13:36  hkaram
 * New branch
 *
 * Revision 1.4.1.1  1994/03/14  22:26:38  menze
 * Removed unused variables
 *
 * Revision 1.4  1994/02/05  00:35:10  menze
 *   [ 1994/01/28          menze ]
 *   Rom initialization moved to pi/ook/initrom.c
 *
 * Revision 1.3  1993/12/16  02:14:21  menze
 * site.h no longer exists
 * protocol table initialization now lives in the prottbl module
 * errBuf is now global
 *
 * Revision 1.2  1993/11/13  00:45:44  menze
 * Original version from UMass
 */

#include <ctype.h>
#include <signal.h>
#include "x_stdio.h"
#include "platform.h"
#include "process.h"
#include "upi.h"
#include "xk_debug.h"
#include "event_i.h"
#include "event.h"
#include "compose.h"
#include "prottbl.h"
#include "x_libc.h"
#include "x_util.h"
#include "netmask.h"
#include "rom.h"

int globalArgc;
char **globalArgv;

int	traceinit;



static void
pgraphStub(ev, arg)
    Event	ev;
    VOID 	*arg;
{
    build_pgraph();
}

/*
 * Main process blocks on this after starting system. Signalling it
 * causes the xkernel to exit abruptly, but at a known point.
 */
static Semaphore wakeMain;

/*
 *	Main signal handler.  It's job is to wake up everyone
 *	so that they can unblock and catch the signal themselves.
 */
void master_signal_handler()
{
  fprintf(stderr, "master signal handler pid %d\n", getpid());
  process_unblock_children();
  exit(0);
}

int
main(argc,argv)
int	argc;
char	*argv[];
{
    extern void clock_ih(), InitProtocols();
    
    setbuf(stdout, 0);
    globalArgc = argc;
    globalArgv = argv;
    
    process_init();			
    
    xTraceInit();
    initRom();
    map_init();
    /* msgInit(); */
    evInit();
    init_clock(clock_ih, (long)(EVENT_INTERVAL/1000));
    netMaskInit();
    semInit(&wakeMain, 0);
    signal(SIGUSR1, master_signal_handler);

    prottbl_init();
    upiInit();
    build_pgraph_dev();
    evDetach( evSchedule( pgraphStub, 0, 0 ) );

    xTrace0(processcreation, TR_EVENTS, "Reached end of init code");
    semWait(&wakeMain);
    xTrace0(processcreation, TR_EVENTS, "Exiting..");
    return 0;
}

