// Save prompt is set by caller to indicate the user has not chosen a save or save as command and
//  so needs to be prompted to save (eg user has gone File New, prompt him to save existing work)
void CentralWorkSaver::Save( bool prompt, bool save_as )
{
    bool file_exists   = TestFileExists();
    bool game_modified = TestGameModified();
    bool file_modified = TestFileModified();
    bool game_in_file  = TestGameInFile();
    if( !game_modified )
    {
        if( !prompt ) 
        {
            // if we are being asked to save, then save
            SaveFile(prompt,file_exists?FILE_EXISTS_GAME_UNCHANGED:FILE_NEW_GAME_NEW,save_as);
        }
        else
        {
            // else only prompt if file modified
            if( file_exists && file_modified )
                SaveFile(prompt,FILE_EXISTS_GAME_UNCHANGED,save_as);
        }
    }
    else
    {
        if( !file_exists )
        {
            // file doesn't exist, game is modified
            int answer = SaveGamePrompt(prompt,FILE_NEW_GAME_NEW,true);
            if( answer == wxYES )
                SaveFile(false,FILE_NEW_GAME_NEW,true);
        }
        else
        {
            // file exists, game is modified
            FILE_MODE fm = game_in_file ? FILE_EXISTS_GAME_MODIFIED : FILE_EXISTS_GAME_NEW;
            int answer = SaveGamePrompt(prompt,fm,save_as);
            if( answer != wxCANCEL )
            {
                bool file_prompt = false;   // normally prompt only on game
                if( answer == wxNO )
                {
                    file_prompt = prompt;   // but user doesn't want to save game, may need
                                            //  to ask if they want to save file
                    fm = FILE_EXISTS_GAME_UNCHANGED;
                }
                if( file_modified || answer==wxYES )
                    SaveFile( file_prompt, fm, save_as );
            }
        }
    }
}
bool CentralWorkSaver::GameNew()
{
    any_cancel = false;
    bool file_exists   = TestFileExists();
    bool game_modified = TestGameModified();
    bool game_in_file  = TestGameInFile();
    if( file_exists && game_modified )
    {
        if( game_in_file )
        {
            int answer = SaveGamePrompt(true,FILE_EXISTS_GAME_MODIFIED,false);
            if( answer == wxYES )
                SaveFile(false,FILE_EXISTS_GAME_MODIFIED,false);
        }
        else
        {
            int answer = SaveGamePrompt(true,FILE_EXISTS_GAME_NEW,false);
            if( answer == wxYES )
                SaveFile(false,FILE_EXISTS_GAME_NEW,false);
        }
    }
    bool okay = !any_cancel;
    return okay; 
}
bool CentralWorkSaver::FileSaveGameAs()
{
    bool ok=true;
    bool append=false;
    wxString wx_filename;
    GameDetailsDialog dialog(objs.frame);
    if( dialog.Run(*gd) )
        objs.gl->GameRedisplayPlayersResult();
    else
        ok = false;
    if( ok )
    {
        wxFileDialog fd( objs.frame, "Select new .pgn file or existing .pgn file", "", "", "*.pgn", wxFD_SAVE ); //|wxFD_CHANGE_DIR );
        wxString dir = objs.repository->nv.m_doc_dir;
        fd.SetDirectory(dir);
        int answer = fd.ShowModal();
        ok = (answer==wxID_OK);
        if( ok )
        {
            wxString dir;
            wxFileName::SplitPath( fd.GetPath(), &dir, NULL, NULL );
            objs.repository->nv.m_doc_dir = dir;
            wx_filename = fd.GetPath();
            
            // If file exists, append or replace
            if( ::wxFileExists(wx_filename ) )
            {
                int answer = wxMessageBox( "Append game to file ?", "Yes to append, no to replace",  wxYES_NO|wxCANCEL, objs.frame );
                if( answer == wxYES )
                    append = true;
                else if( answer == wxCANCEL )
                    ok = false;
            }
        }
    }
    if( ok )
    {
        gd->FleshOutDate();
        gd->FleshOutMoves();
        std::string head;
        gd->ToFileTxtGameDetails( head );
        std::string body;
        gd->ToFileTxtGameBody( body );
        FILE *file = NULL;
        file = fopen( wx_filename.c_str(), append ? "ab" : "wb" );
        if( file )
        {
            fseek(file,0,SEEK_END);
            fwrite( head.c_str(), 1, head.length(), file );
            fwrite( body.c_str(), 1, body.length(), file );
            fclose( file );
            if( !TestGameInFile() )
            {
                gd->modified = false;
                gd->game_prefix_edited = false;
                gd->game_details_edited = false;
                undo->Clear(*gd);
            }
        }
    }
    return ok;
}