/*
 *  Kevin P. Smith      12/05/88
 *
 *  Takes the output of scores A and generates the player file.
 *  (Also creates the .GLOBAL file)
 *  This program reads stdin for its data.
 */

#include <stdio.h>
#include <sys/types.h>
#include <sys/file.h>
#include <pwd.h>
#include "defs.h"
#include INC_FCNTL
#include "data.h"
#include "struct.h"

struct statentry play_entry;
struct rawdesc *output=NULL;
char mode;

trimblanks2(str)
char *str;
{
    *str=0;
    str--;
    while (*str==' ') {
	*str=0;
	str--;
    }
    if (*str=='_') *str=0;
}

trimblanks(str)
char *str;
{
    *str=0;
    str--;
    while (*str==' ') {
	*str=0;
	str--;
    }
}

/* ARGSUSED */
main(argc, argv)
int argc;
char **argv;
{
    int fd;
    char buf[512];

    if (argc>1) usage();
    getpath();
    printf("Warning:  If you do not know how to use this program, break it now!\n");
    status=(struct status *) malloc(sizeof(struct status));
    scanf("%10ld %10d %10d %10d %10d %10ld\n", 
	&status->time, 
	&status->planets, 
	&status->armsbomb, 
	&status->kills, 
	&status->losses,
	&status->timeprod);

#ifdef CONVERT 
    status->planets*=3;
#endif

    fd = open(Global, O_WRONLY|O_CREAT|O_TRUNC, 0600);
    if (fd < 0) {
	printf("Cannot open the global file %s\n", Global);
	exit(0);
    }
    write(fd, (char *) status, sizeof(struct status));
    close(fd);

    fd = open(PlayerFile, O_WRONLY|O_CREAT|O_TRUNC, 0600);
    if (fd < 0) {
	perror(PlayerFile);
	exit(1);
    }

    while (gets(buf)) {
	trimblanks2(buf+16);
	trimblanks(buf+33);
	trimblanks(buf+129);
	strcpy(play_entry.name, buf);
	strcpy(play_entry.password, buf+17);
	strcpy(play_entry.stats.st_keymap, buf+34);
#ifdef GENO_COUNT
	sscanf(buf+130, " %d %lf %d %d %d %d %d %d %d %d %d %d %d %d %d %lf %ld %d %d\n",
#else
	sscanf(buf+130, " %d %lf %d %d %d %d %d %d %d %d %d %d %d %d %d %lf %ld %d\n",
#endif
	    &play_entry.stats.st_rank,
	    &play_entry.stats.st_maxkills,
	    &play_entry.stats.st_kills,
	    &play_entry.stats.st_losses,
	    &play_entry.stats.st_armsbomb,
	    &play_entry.stats.st_planets,
	    &play_entry.stats.st_ticks,
	    &play_entry.stats.st_tkills,
	    &play_entry.stats.st_tlosses,
	    &play_entry.stats.st_tarmsbomb,
	    &play_entry.stats.st_tplanets,
	    &play_entry.stats.st_tticks,
	    &play_entry.stats.st_sbkills,
	    &play_entry.stats.st_sblosses,
	    &play_entry.stats.st_sbticks,
	    &play_entry.stats.st_sbmaxkills,
	    &play_entry.stats.st_lastlogin,
#ifdef GENO_COUNT
	    &play_entry.stats.st_flags,
	    &play_entry.stats.st_genos);
#else
	    &play_entry.stats.st_flags);
#endif

#ifdef CONVERT
	play_entry.stats.st_planets*=3;
	play_entry.stats.st_tplanets*=3;
#endif

	write(fd, (char *) &play_entry, sizeof(struct statentry));
    }
    return 1;		/* satisfy lint */
}

usage()
{
    printf("Usage: newscores < scoredb\n");
    printf("This program takes input of the form generated by the 'scores A'\n");
    printf(" command.  This allows you to create an ascii form of the database\n");
    printf(" with 'scores A > scoredb', then you can edit it as needed, then you\n");
    printf(" can issue a 'newscores < scoredb' command to restore the database.\n");
    printf("Note:  The form of the output from the 'scores A' command is very\n");
    printf(" specific.  Be sure that the input stream matches it very accurately\n");
    exit(0);
}
