#include <descrip.h>
#include <stdio.h>
#include "bboard.h"

static struct { 			/* Used in all of the GETJNI() calls */
    char n_name[9];
    char l_name[9];
    char l_daemon[33];
    char ln_name[9];
    int node_type;
    int link_pid;
    int link_status;
    int link_qcnt;
    int line_status;
    int line_tot;
    int line_err;
    int line_tmo;
    int context;
    int mask;
} jinf;

static int init = 0;			/* Flag for use of rou_init_route() */

/* Descriptors for buffers */

static struct dsc$descriptor_s
    node_name	= { 8, DSC$K_DTYPE_T, DSC$K_CLASS_S, &jinf.n_name},
    link_daemon = {32, DSC$K_DTYPE_T, DSC$K_CLASS_S, &jinf.l_daemon},
    line_name	= { 8, DSC$K_DTYPE_T, DSC$K_CLASS_S, &jinf.ln_name};


/* Check if the hook already in use? */

int check_for_hook(char * hook)
{
    static struct dsc$descriptor_s
	link_name   = { 8, DSC$K_DTYPE_T, DSC$K_CLASS_S, &jinf.l_name};

    jinf.context = -1;			/* Unknown */
    jinf.mask = 0x0a;			/* Want LINKs and HOOKs */

    if (init++ == 0) {			/* First time through */
	status = rou_init_route();
#ifdef	DEBUG
	printf("rou_init_route() = %08X\n", status);
#endif	/* DEBUG */
    }

    strupr(strcpy(temp, hook)); 	/* Need it uppercase and temporary */

    /* Do it to it */

#ifdef	DEBUG
	printf("nodename t linkname linkpid  s linkdaem linename\n");
	printf("-------- - -------- -------- - -------- --------\n");
#endif	/* DEBUG */

    while (rou_getjni(&jinf.context, &jinf.mask, &node_name, &jinf.node_type,
	    &link_name, &jinf.link_pid, &jinf.link_status, &jinf.link_qcnt,
	    &link_daemon, &line_name, &jinf.line_status, &jinf.line_tot,
	    &jinf.line_err, &jinf.line_tmo) & 1) {

#ifdef	DEBUG
	jinf.n_name[8]='\0';
	jinf.l_name[8]='\0';
	jinf.l_daemon[8]='\0';
	jinf.ln_name[8]='\0';

	printf("%8s %1d %8s %08X %1d %8s %8s\n", jinf.n_name, jinf.node_type,
	    jinf.l_name, jinf.link_pid, jinf.link_status, jinf.l_daemon,
	    jinf.ln_name);
#endif	/* DEBUG */

	/* Did we find it? */

	if (!strncmp(temp, strupr(jinf.l_name), strlen(temp))) {
	    return (TRUE);
	}
    }
    return (FALSE);
}

/* What's the name of the host? */

void load_host_name(char * host)
{
    static struct dsc$descriptor_s link_name;

    jinf.context = -1;			/* Unknown */
    jinf.mask = 0x01;			/* Want HOST information */

    if (init++ == 0) {			/* First time through */
	status = rou_init_route();
#ifdef	DEBUG
	printf("rou_init_route() = %08X\n", status);
#endif	/* DEBUG */
    }

    strcpy(host, "--------");           /* Something to initialize */
    str_desc(&link_name, host); 	/* Build a descriptor */

    /* Get the information */

    rou_getjni(&jinf.context, &jinf.mask, &node_name, &jinf.node_type,
	    &link_name, &jinf.link_pid, &jinf.link_status, &jinf.link_qcnt,
	    &link_daemon, &line_name, &jinf.line_status, &jinf.line_tot,
	    &jinf.line_err, &jinf.line_tmo);

    host[8] = '\0';                     /* Terminate the string */
    strip(host);			/* Remove trailing blanks */
}

/* Is everything ready for us to go away? */

char * check_links_down(void)
{
    static struct dsc$descriptor_s
	link_name   = { 8, DSC$K_DTYPE_T, DSC$K_CLASS_S, &jinf.l_name};

    jinf.context = -1;			/* Unknown */
    jinf.mask = 0x02;			/* Want LINKs */

    if (init++ == 0) {			/* First time through */
	status = rou_init_route();
#ifdef	DEBUG
	printf("rou_init_route() = %08X\n", status);
#endif	/* DEBUG */
    }

    /* Start searching */

    while (rou_getjni(&jinf.context, &jinf.mask, &node_name, &jinf.node_type,
	    &link_name, &jinf.link_pid, &jinf.link_status, &jinf.link_qcnt,
	    &link_daemon, &line_name, &jinf.line_status, &jinf.line_tot,
	    &jinf.line_err, &jinf.line_tmo) & 1) {

	/* What is the link doing? */

	if (jinf.link_status == 0) {	/* Already inactive, don't bother */
	    continue;
	}

	/* Terminate and fondle the name */

	jinf.l_name[8] = '\0';
	strip(strupr(jinf.l_name));

	/* Does it have to be down before we can continue? */

	if (mandatory_link(jinf.l_name)) {
	    return (jinf.l_name);
	}
    }
    return (NULL);
}

/* So what version of Jnet is this, anyway? */

static char * jver[32];
static struct dsc$descriptor_s
    jni_vers_dsc;

char * get_jnet_wrk(struct dsc$descriptor * jni_vers_dsc)
{
    strncpy(jver, jni_vers_dsc->dsc$a_pointer, jni_vers_dsc->dsc$w_length);
    jver[jni_vers_dsc->dsc$w_length] = '\0';
}
    
char * get_jnet_ver(void)
{
    rou_version_dsp(get_jnet_wrk);	/* Undocumented, but works */

    return(jver);
}
