#include <stdio.h>
#include <stdlib.h>

#include <string.h>

#include "necro.h"

/*
 * $Header: /blue/homes/cowell/src/nec/RCS/main.c,v 1.1 1996/04/20 21:06:57 cowell Exp cowell $
 *
 * main.c
 * Copyright Andy Cowell <cowell@cs.utk.edu>, all rights reserved.
 *
 * $Log: main.c,v $
 * Revision 1.1  1996/04/20  21:06:57  cowell
 * Initial revision
 *
 *
 */

static char *rcsid = "$Id: main.c,v 1.1 1996/04/20 21:06:57 cowell Exp cowell $";

void build_html(struct _gang *g);
void do_stats(struct _gang *g);
static int gang_compare(struct _gang **x, struct _gang **y);
static int reverse_gang_compare(struct _gang **x, struct _gang **y);

extern struct _gang *root;
int debug = 1;



main(int argc, char **argv) {
    struct _gang *g;

    if (argc < 2) {
	printf("Usage: %s -h[tml] | -s[tats]\n", argv[0]);
	exit(-1);
    }

    yyparse();

    if (!(strcmp("-h", argv[1]))) {
	/* We assume with the -h option that there was only 1 gang. */
	build_html(root);
    } else if (!(strcmp("-s", argv[1]))) {
	/* We're getting several gangs and want to do statistics */
	do_stats(root);
    }
}
    

void
build_html(struct _gang *g) {
    struct _member *m;
    char *s;

    /* Print our HTML header, and start the body */
    printf("<HTML>\n<HEAD>\n");
    printf("<TITLE>Necromunda Gang: %s</TITLE>\n", g->name);
    printf("<LINK REV=\"made\" HREF=\"mailto:cowell@cs.utk.edu\">\n\n");
    printf("<META name=\"resource-type\" value=\"document\">\n");
    printf("<META name=\"description\" value=\"The gang roster for ");
    printf("the Necromunda gang %s\">\n", g->name);
    printf("<META name=\"keywords\" value=\"Games, Workshop, Necromunda, ");
    printf("Gang, Roster, Miniature, War, Game, Wargame, Wargaming\">\n");
    printf("<META name=\"distribution\" value=\"global\">\n");
    printf("\n</HEAD>\n\n");

    printf("<BODY");
    /* Comment out the following to remove Netscape colors */
    printf(" BGCOLOR=\"#000000\" ");
    printf(" TEXT=\"#FFFF00\" ");
    printf(" link=\"#ffffff\" ");
    printf(" vlink=\"#ff5020\" ");
    printf(">\n\n");

    /* Formatting for printing the table stuff is horrible, sorry */

    /*  MAIN TABLE */
    printf("<TABLE>\n");
    printf("<TR><TD VALIGN=TOP>\n\n\n");

    /* Gang Rating table */
    printf("  <table border=3 cellspacing=2 cellpadding=2>\n");
    printf("  <tr><th>Rating:</th><td><STRONG>%d</STRONG></td></tr>\n", g->rating);
    printf("  </table>\n\n\n<br>\n\n\n");
    
    /* Territory table */
    /* Every gang has at least one territory */
    printf("  <Table border=3 cellspacing=2 cellpadding=2>\n");
    printf("  <tr><th>Territory</th></tr>\n");
    printf("  <tr><td>%s</td></th>\n", strtok(g->ttory, ","));
    while ((s = strtok(((char *)NULL), ",")) != ((char *)NULL))
	printf("  <tr><td>%s</td></th>\n", s);
    printf("  </tr></table>\n\n\n<br>\n\n\n");
    
    /* Stash and Hoard */
    printf("  <Table border=3 cellspacing=2 cellpadding=2><tr><th>Stash</th></tr>\n");
    printf("  <tr><td>%d creds</td></tr></table>\n\n\n<br>\n\n\n", g->stash);
    printf("  <Table border=3 cellspacing=2 cellpadding=2><tr><th>Hoard</th></tr>\n");
    printf("  <tr><td>");
    if (g->hoard != ((char *)NULL)) {
	printf("  %s  <br>\n", strtok(g->hoard, ","));
	while ((s = strtok(((char *)NULL), ",")) != ((char *)NULL))
	    printf("  %s <br>\n", s);
    }
    printf("  </td></tr></table></td>\n\n\n<br>\n\n\n");
    

    /* MAIN TABLE */
    printf("</TD><TD ROWSPAN=3>\n\n");
    

    /* Roster table */
    printf("  <table border=5 cellspacing=2 cellpadding=2>\n");
    printf("  <tr><th colspan=5><h2>%s</h2></th></tr>\n", g->name);
    printf("  <tr><th>Name</th><th>Characteristics</th><th>Equipment, Skills & Injuries</th><th>Cost</th><th>Exp.</th></tr>\n\n");
    
    for (m=g->member; m!=((struct _member *)NULL); m=m->next) {
	printf("  <tr><td><strong>%s</strong><br>%s</td>\n", m->name, m->pos);
	printf("  <td><table border=1>\n");
	printf("  <tr><th>M</th><th>WS</th><th>BS</th><th>S</th><th>T</th><th>W</th><th>I</th><th>A</th><th>Ld</th></tr>\n");
	printf("  <tr><th>%d</th><th>%d</th><th>%d</th><th>%d</th><th>%d</th><th>%d</th><th>%d</th><th>%d</th><th>%d</th></tr>\n", m->m, m->ws, m->bs, m->s, m->t, m->w, m->i, m->a, m->ld);
	printf("  </table></td>\n");
	printf("  <td>%s\n", m->equipment);
	if (m->skills != ((char *)NULL))
	    printf("  <br><strong>%s</strong>\n", m->skills);
	if (m->injuries != ((char *)NULL))
	    printf("  <br><em>%s</em>\n", m->injuries);
	printf("  </td><td>%d</td><td>%d</td></tr>\n\n", m->cost, m->exp);
    }
    
    printf("  </table>\n\n");

    /* MAIN TABLE */
    printf("</td></tr></table>\n\n");

    /* Footer */
    printf("<HR>\n");
    printf("This page generated by ");
    printf("<A HREF=\"http://www.cs.utk.edu/~cowell/min/nec/\">%s", progname);
    printf("</A>, written by <A HREF=\"http://www.cs.utk.edu/~cowell\">");
    printf("Andy Cowell</A>.\n");
    printf("<BR>\n");
    printf("Thanks to ");
    printf("<A HREF=\"http://www.cs.cmu.edu/afs/andrew/usr/msde/www/\">");
    printf("Mark Shieh</A> for the basis of the Netscape table format.\n");
    
    printf("</BODY>\n\n</HTML>\n");
}

static int gang_compare(struct _gang **x, struct _gang **y) {
    /* qsort will sort low to high */
    return((*x)->rating - (*y)->rating);
}
    
static int reverse_gang_compare(struct _gang **x, struct _gang **y) {
    /* qsort will sort high to low */
    return((*y)->rating - (*x)->rating);
}
    
static int reverse_member_compare(struct _member **x, struct _member **y) {
    /* qsort will sort high to low */
    return((*y)->exp - (*x)->exp);
}
    
void 
do_stats(struct _gang *g) {
    struct _gang **gangs, *p;
    int gang_count, i;
    struct _member **members, *q;
    int member_count;

    /* First, let's sort our gang ratings */

    /* How many gangs do we have? */
    for (p=g, gang_count=0; p!=((struct _gang *)NULL); p=p->next, gang_count++);
    if (debug) printf("We have %d gangs.\n", gang_count);

    /* Copy pointers into gangs[] for sorting */
    gangs = (struct _gang **)malloc(sizeof(struct _gang *) * gang_count);
    for (p=g, i=0; p!=((struct _gang *)NULL); p=p->next, i++)
	gangs[i] = p;

    /* Sort our gangs based on their ratings */
    qsort(gangs, gang_count, sizeof(struct _gang *), reverse_gang_compare);

    for (i=0; i<gang_count; i++)
	printf("%s:%d    ", gangs[i]->name, gangs[i]->rating);
    printf("\n\n");


    /* Now, do similarly for their members, based on experience. */

    /* How many members do we have? */
    for (p=g, member_count=0; p!=((struct _gang *)NULL); p=p->next)
	for (q=p->member; q!=((struct _member *)NULL); q=q->next, member_count++);
    if (debug) printf("We have %d members.\n", member_count);

    /* Copy pointers into members[] for sorting */
    members = (struct _member **)malloc(sizeof(struct _member *) * member_count);
    for (p=g, i=0; p!=((struct _gang *)NULL); p=p->next)
	for (q=p->member; q!=((struct _member *)NULL); q=q->next, i++)
	    members[i] = q;

    /* Sort our members based on their ratings */
    qsort(members, member_count, sizeof(struct _member *), reverse_member_compare);

    for (i=0; i<member_count; i++)
	if (debug) printf("%s (%s):\t%d\n", members[i]->name, members[i]->gang_name, members[i]->exp);
    printf("\n");

}
