/*
  export-playerdb.c

  This program dumps db.global and db.players into an ASCII format into
  specified files.  Run this on the machine that you want to export a player
  database FROM.

  This should work on any type of architecture that can run the server; there
  are no architectural dependencies in here.  Drawbacks to this method:  it's
  slower, requires access to both machines (or cooperation from someone
  on the other machine), requires two steps, and generates large intermediate
  ASCII files.  If you know it's a 32 bit-32 bit conversion AND it requires
  and endian swap, use the other program that does it quicker and in one step.

  Benefits to this method:  Rob can use it.  ;) So can anyone with a 64-bit
  machine running a server (if such a thing exists).

  You must also use the import-playerdb.c program to import the database on
  the other end.

  Oh yeah; this falls under the Paradise copyright as stated in the server
  documentation; this is NOT under the GPL.

  Bob Glamm (June 26, 1995)

  Bug fixes:

  August 3, 1995    - Fixed bug that caused incorrect import for playernames
                      that contained spaces
*/

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

char VERSION[] = "ExportDB version 1.1";

void export_global( char *, char * );
void export_player( char *, char * );

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

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

  FILE *ifile, *ofile; struct status stati;

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

  fread( &stati, sizeof( struct status ), 1, ifile );
  fclose( ifile );
  fprintf( ofile, "%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\n",
          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 );
  fclose( ofile );
}

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

  FILE *ifile, *ofile;
  int n;
  struct statentry player;
  char c = 0;

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

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

  while( ( n = fread( &player, sizeof( struct statentry ), 1, ifile ) ) > 0 ){
    fprintf( ofile, "%c%s%c%s%c%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\n",
	     c,
             player.name,
             c,
             player.password,
             c,
             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 );
  }
  fclose( ifile );
  fclose( ofile );
}
