void db_maintenance_verify_compression()
{
    ifile = fopen( PGN_FILE, "rt" );
    if( !ifile )
        cprintf( "Cannot open %s\n", PGN_FILE );
    else
    {
        // Verify compression
        void CompressMovesDiagBegin();
        void CompressMovesDiagEnd();
        DebugPrintfTime turn_on_time_reporting;
        CompressMovesDiagBegin();
        PgnRead *pgn = new PgnRead('V');
        cprintf( "Begin\n" );
        pgn->Process(ifile);
        cprintf( "End\n" );
        CompressMovesDiagEnd();
    }
    if( ifile )
        fclose(ifile);
    if( ofile )
        fclose(ofile);
    if( logfile )
        fclose(logfile);
}
void db_maintenance_create_player_database()
{
    //std::string input("C:\\Users\\Bill\\Documents\\T3Database\\millionbase-2.22");
    std::string input("C:\\Users\\Bill\\Documents\\T3Database\\twic-2006-2015");
    players_database_begin();
    std::string input_file = input+".pgn";
    std::string output_file = input+"-output.pgn";
    std::string log_file = input+".log";
    ifile = fopen( input_file.c_str() , "rt" );
    if( !ifile )
        cprintf( "Cannot open %s\n", input_file.c_str()  );
    else
    {
        ofile = fopen( output_file.c_str(), "wt" );
        if( !ofile )
            cprintf( "Cannot open %s\n", output_file.c_str() );
        else
        {
            logfile = fopen( log_file.c_str(), "wt" );
            if( !logfile )
                cprintf( "Cannot open %s\n", log_file.c_str() );
            else
            {
                PgnRead *pgn = new PgnRead('G');
                pgn->Process(ifile);
            }
        }
    }
    players_database_end();
    if( ifile )
        fclose(ifile);
    if( ofile )
        fclose(ofile);
    if( logfile )
        fclose(logfile);
}
// Build the default database if .pgn exists and .tdb doesn't
void db_primitive_build_default_database( const char *db_file_name )
{
    bool build = true;
    std::string error_msg;
    std::string db(db_file_name);
    std::string pgn;
    int offset = db.find(".tdb");
    if( offset == std::string::npos )
    {
        cprintf( "Database file extension not found in %s?!\n", db_file_name );
        build = false;
    }
    else
    {
        pgn = db.substr(0,offset) + ".pgn";
        cprintf( "Possible pgn file to build default database is %s\n", pgn.c_str() );
        wxFileName wxpgn( pgn.c_str() );
        wxFileName wxdb( db_file_name );
        bool exists_p = wxpgn.FileExists();;
        bool exists_d = wxdb.FileExists() && (wxdb.GetSize()>0);
        build = exists_p && !exists_d;      // pgn present, but not database
        cprintf( "exists_p=%s, exists_d=%s, build=%s\n", exists_p?"true":"false", exists_d?"true":"false", build?"true":"false" );
    }
    if( build )
    {
        db_primitive_error_msg();   // clear error reporting mechanism
        bool ok = db_primitive_open( db_file_name, true );
        if( ok )
            ok = db_primitive_transaction_begin(NULL);
        if( ok )
            ok = db_primitive_create_tables();
        if( ok )
            ok = (db_primitive_count_games()>=0); // to set game_id to zero
        FILE *ifile = fopen( pgn.c_str(), "rt" );
        if( !ifile )
        {
            error_msg = "Cannot open ";
            error_msg += pgn;
            ok = false;
        }
        else
        {
            std::string title( "Creating database, step 2 of 4");
            std::string desc("Reading file ");
            wxFileName wxpgn( pgn.c_str() );
            desc += wxpgn.GetFullName().c_str();
            ProgressBar progress_bar( title, desc, NULL, ifile );
            PgnRead *pr = new PgnRead('A',&progress_bar);
            bool aborted = pr->Process(ifile);
            if( aborted )
            {
                error_msg = db_primitive_error_msg();
                if( error_msg == "" )
                    error_msg = "cancel";
                ok = false;
            }
            delete pr;
            fclose(ifile);
        }
        if( ok )
            ok = db_primitive_flush();
        if( ok )
            ok = db_primitive_transaction_end();
        if( ok )
            ok = db_primitive_create_indexes();
        if( ok )
            db_primitive_close();
        if( !ok )
        {
            if( error_msg == "" )
                error_msg = db_primitive_error_msg();
            if( error_msg == "cancel" )
                error_msg = "Database creation cancelled";
            wxMessageBox( error_msg.c_str(), "Database creation failed", wxOK|wxICON_ERROR );
            db_primitive_close();
            unlink(db.c_str());
        }
    }
}