#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_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 status status;
    struct old_status old_stats;
    char command[256];
    struct stat gl_stat;

    getpath();

    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 (gl_stat.st_size == sizeof(struct status)) {
	    printf("Looks like a double global file. Not doing anything.\n");
	    exit(0);
	}
        if ((c=read(glfd, (char *) &old_stats, sizeof(struct old_status))) !=
        	sizeof(struct old_status)) {
            fprintf(stderr, "Global file wrong size.  exitting\n");
        }
    }

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

    status.active = old_stats.active;
    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, SEEK_SET);
        (void) write(glfd, (char *) &status, sizeof(struct status));
    }
    close(glfd);
    return 1;		/* satisfy lint */
}

