#include <stdio.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <errno.h>
#include <unistd.h>
#include "defs.h"
#include "struct.h"
#include "data.h"

struct old_status {
    int               active;
    u_int     wait, count;
    u_int     number, request, answer;
    u_char    tourn;          /* Tournament mode? */
    /* These stats only updated during tournament mode */
    u_int       armsbomb, planets, kills, losses, time;
    /* Use long for this, so it never wraps */
    U_LONG      timeprod;
    int         gameup;
};


main()
{
    int c;
    int glfd;
    struct old_status *old_stats;
    struct status *status;
    char command[256];
    struct stat gl_stat;

    getpath();

    old_stats = (struct old_status *) malloc (sizeof(struct old_status));
    status = (struct status *) malloc (sizeof(struct status));

    printf("Checking %s file for conversion\n",Global);
    glfd = open(Global, O_RDWR, 0744);
    if (glfd < 0) {
        fprintf(stderr, "No global file.  Resetting all stats\n");
        MZERO((char *) old_stats, sizeof(struct old_status));
    } else {
	fstat(glfd, &gl_stat);
        if ((c=read(glfd, (char *) old_stats, sizeof(struct old_status))) !=
            sizeof(struct old_status)) {
            fprintf(stderr, "Global file wrong size.  exitting\n");
        }
	if (lseek(glfd, (off_t)0, SEEK_CUR) != gl_stat.st_size) {
	    printf("Looks like a new style global file. Not doing anything.\n");
	    exit(0);
	}
    }

    sprintf(command,"cp %s %s/.GLOBAL.BAK",Global,LIBDIR);
    printf("Copying current .global to %s/.GLOBAL.BAK\n",LIBDIR);
    system(command);

    printf("Old Global Stats:\n");
    printf("\tHours: %-8.1f\n",old_stats->timeprod/36000.0);
    printf("\tPlanets: %-8d\n", old_stats->planets);
    printf("\tBombing: %-8d\n", old_stats->armsbomb);
    printf("\tOffense: %-8d\n", old_stats->kills);
    printf("\tDefense: %-8d\n", old_stats->losses);

    status->active = old_stats->active;
    for (c=0; c < MAXQUEUE; c++) {
        status->wait[c] = 0;
        status->count[c] = 0;
        status->free_slots[c] = 0;
        status->tournmask[c] = 0;
        status->number[c] = 0;
        status->request[c] = 0;
        status->answer[c] = 0;
    }
    status->tourn = old_stats->tourn;
    status->armsbomb = old_stats->armsbomb;
    status->planets = old_stats->planets;
    status->kills = old_stats->kills;
    status->losses = old_stats->losses;
    status->time = old_stats->time;
    status->timeprod = old_stats->timeprod;
    status->gameup = old_stats->gameup;

    printf("New Global Stats:\n");
    printf("\tHours: %-8.1f\n",status->timeprod/36000.0);
    printf("\tPlanets: %-8d\n", status->planets);
    printf("\tBombing: %-8d\n", status->armsbomb);
    printf("\tOffense: %-8d\n", status->kills);
    printf("\tDefense: %-8d\n", status->losses);

    printf("Writing new global\n");
    if (glfd >= 0) {
        (void) lseek(glfd, (off_t) 0, 0);
        (void) write(glfd, (char *) status, sizeof(struct status));
    }
    close(glfd);
    return 1;		/* satisfy lint */
}

