/* 
  import-playerdb.c

  Takes ASCII representations of the global & player databases and generates
  a machine-specific binary database of each into specified files.  See
  export-playerdb.c for info on how to generate the ASCII representations.

  Not under the GPL; this software falls under the Paradise copyright.

  NOTE:  Neither this or export-playerdb.c are guaranteed to work.  I wrote
  these up as a quick hack to complement the endian conversion one, since
  64-bit machines seem to be in our future.

  Bob Glamm (June 26, 1995)
  
  Bug fixes:

  August 3, 1995      - Fixed the bug where players with spaces in their names
                        wouldn't be imported correctly.
  August 16, 1995     - Fixed bug where *c++ was hosed.  Should get both name
                        and password correctly now.
*/

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

char VERSION[] = "ImportDB version 1.2";

void import_global( char *, char * );
void import_player( char *, char * );

int main( int argc, char *argv[] ){
  
  if( argc != 5 ){
    printf( "Usage: %s <global_ASCII> <player_ASCII> <global_db> <player_db>\n", argv[0] );
    exit( 1 );
  }
  printf( "%s\n", VERSION );
  import_global( argv[1], argv[3] );
  import_player( argv[2], argv[4] );
}

void import_global( char *infile, char *outfile ){

  FILE *ifile, *ofile; 
  struct status stati;

  ifile = fopen( infile, "r" );
  if( !ifile ){
    fprintf( stderr, "Cannot open ASCII global file (%s).\n", infile );
    return;
  }
  ofile = fopen( outfile, "w" );
  if( !ofile ){
    fprintf( stderr, "Cannot open global DB file.\n" );
    fclose( ifile );
    return;
  }

  fscanf( ifile, "%d %u %u %u %u %u %d %lu %lu %lu %lu %lu %lu %lu %lu %lu %lu %lu %lu %lu %lu %lu %lu %lu %lu %d %d",
          &stati.active,
          &stati.wait,
          &stati.count,
          &stati.number,
          &stati.request,
          &stati.answer,
          &stati.tourn,
          &stati.dooshes,
          &stati.armsbomb,
          &stati.resbomb,
          &stati.planets,
          &stati.kills,
          &stati.losses,
          &stati.genocides,
          &stati.sbkills,
          &stati.sblosses,
          &stati.sbtime,
          &stati.wbkills,
          &stati.wblosses,
          &stati.wbtime,
          &stati.jsplanets,
          &stati.jstime,
          &stati.time,
          &stati.timeprod,
          &stati.clock,
          &stati.nukegame,
          &stati.gameup );

  fwrite( &stati, sizeof( struct status ), 1, ofile );
  fclose( ifile );
  fclose( ofile );
}

void import_player( char *infile, char *outfile ){

  FILE *ifile, *ofile;
  struct statentry player;
  char buf[1000];
  char *pname, *password, *rol;

  ifile = fopen( infile, "r" );
  if( !ifile ){
    fprintf( stderr, "Cannot open ASCII player file (%s).\n", infile );
    return;
  }

  ofile = fopen( outfile, "w" );
  if( !ofile ){
    fprintf( stderr, "Cannot open output player DB file.\n" );
    fclose( ifile );
    return;
  }

  while( fgets( buf, 999, ifile ) ){
    pname = &(buf[1]);
    password = pname;
    while( *password )
      password++;
    password++;
    rol = password;
    while( *rol )
      rol++;
    rol++;
    strcpy( player.name, pname );
    strcpy( player.password, password );
    sscanf( rol, "%d %f %f %d %d %d %d %d %d %d %d %d %d %f %d %d %d %f %d %d %ld %d %d %d %d",
             &player.stats.st_genocides,
             &player.stats.st_tmaxkills,
             &player.stats.st_di,
             &player.stats.st_tkills,
             &player.stats.st_tlosses,
             &player.stats.st_tarmsbomb,
             &player.stats.st_tresbomb,
             &player.stats.st_tdooshes,
             &player.stats.st_tplanets,
             &player.stats.st_tticks,
             &player.stats.st_sbkills,
             &player.stats.st_sblosses,
             &player.stats.st_sbticks,
             &player.stats.st_sbmaxkills,
             &player.stats.st_wbkills,
             &player.stats.st_wblosses,
             &player.stats.st_wbticks,
             &player.stats.st_wbmaxkills,
             &player.stats.st_jsplanets,
             &player.stats.st_jsticks,
             &player.stats.st_lastlogin,
             &player.stats.st_flags,
             &player.stats.st_cluesuccess,
             &player.stats.st_rank,
             &player.stats.st_royal );
    fwrite( &player, sizeof( struct statentry ), 1, ofile );
  }
  fclose( ifile );
  fclose( ofile );
}
