#include <stdio.h>
#include MUT_H
#include MLO_H
#include MPH_H
#include SCR_H
#include "mbkgen.h"
#include "util.h"
static channel_list *channel;
char buffer[BUFSIZ];

main(argc, argv)
int argc;
char **argv;
{
	mbkenv();
	if (argc != 3)
		exit(1);
	srandom(getpid());
	bordel("pwet", atoi(argv[1]), atoi(argv[2]));
	return 0;
}

bordel(name, nsp, ncp)
char *name;
int nsp, ncp;
{
int a, b, i, j, k, p, q, r, x, mx = 0, z;
lofig_list *l;
pt_list *pt;
bnet_list *net = NULL, *oldnet;
char *str;
int tabN[4096];
int tabS[4096];
int *tab;
phcon_list *pc;
int no1, no2;
phfig_list *pf;

	sprintf(buffer, "%s%d", name, getpid());
	l = addlofig(buffer);
	pf = addphfig("prout");
	for (i = 0; i < 4096; i++)
		tabN[i] = tabS[i] = 0;
	channel = addchannel(channel, buffer, NULL, NULL, HOR, 0);
	p = (random() & 0xFFF) + 30 + nsp; /* number of signals */
	for (i = 0; i < p; i++)	{
		q = (random() & 0xF) + 1 + ncp; /* number of connectors on the signal */
		str = nameindex(name, i);
		addlocon(l, str,
			addlosig(l, i, addchain(NULL, str), EXTERNAL, 0), UNKNOWN);
		net = channel->NETS = addnet(channel->NETS, str, NULL);
		no1 = no2 = 0;
		for (j = 0; j < q; j++)	{
			x = (random() & 0x1FF) + 5;
			for (k = -4; k < 5; k++)
				if (tabN[x + k] != 0)
					break;
			if (k != 5)
				continue;
			if ((random()%4) == 0)
				continue;
			b = random() & 1;
			pc = addphcon(pf, SOUTH, str, 0, 0,
								b ? ALU1 : ALU2, b ? 1 : 2);
			tabN[x] = x;
			mx = mx < x ? x : mx;
			net->POINTS = addpoint(net->POINTS, str, x * SCALE_X,
											0, SOUTH, pc, NULL);
			if (!no1 && !(x % 5)) {
				pc = addphcon(pf, WEST, str, 0, 0, ALU2, 2);
				net->POINTS = addpoint(net->POINTS, str, x * SCALE_X,
												0, WEST, pc, NULL);
				no1 = 1;
			}
			if (!no2 && !(x % 7)) {
				pc = addphcon(pf, EAST, str, 0, 0, ALU2, 2);
				net->POINTS = addpoint(net->POINTS, str, x * SCALE_X,
												0, EAST, pc, NULL);
				no2 = 1;
			}
			z = 0;
			while (1) {
				x = (random() & 0x1FF) + 5;
				for (k = -4; k < 5; k++)
					if (tabS[x + k] != 0)
						break;
				if (z++ > 10)
					break;
				if (k != 5)
					continue;
				if ((random()%4) == 0)
					continue;
				b = random() & 1;
				pc = addphcon(pf, NORTH, str, 0, 0,
									b ? ALU1 : ALU2, b ? 1 : 2);
				tabS[x] = x;
				mx = mx < x ? x : mx;
				net->POINTS = addpoint(net->POINTS, str, x * SCALE_X,
												0, NORTH, pc, NULL);
				break;
			}
		}
	}

	if (channel->NETS == NULL) {
		fprintf(stderr, "No nets!\n");
		EXIT();
	}
	for (oldnet = net = channel->NETS; net; net = net->NEXT) {
		if (net->POINTS == NULL || net->POINTS->NEXT == NULL) {
			dellosig(l, getlocon(l, net->NAME)->SIG->INDEX);
			dellocon(l, net->NAME);
			if (net == channel->NETS)
				channel->NETS = net->NEXT;
			else
				oldnet->NEXT = net->NEXT;
			continue;
		}
		oldnet = net;
	}
	savelofig(l);
	pf->XAB1 = 0;
	pf->XAB2 = mx * SCALE_X;
	fprintf(stdout, "Routing the channel\n");
	routechannel(pf, channel);
	pf = getphfig(channel->NAME, 'A');
	destroysegments(pf, null);
	verticalmerge(pf);
	/* connectsupplies(getphfig(ch->NAME, 'A'), pt); */
	savephfig(pf);
	printf(buffer);
}
