proc RecoverBackupIfCrash { currentgidtmpdir } {
    #currentgidtmpdir can be for example .../tmp/gid8
    #normalize to compare after with $dir
    set currentgidtmpdir [file normalize $currentgidtmpdir]
    set backupfile ""
    set tmpdir [file dirname $currentgidtmpdir]
    foreach dir [glob -nocomplain [file join $tmpdir gid*]] {
	if { [file isdirectory $dir] && [file normalize $dir] != $currentgidtmpdir } {
	    foreach i [glob -nocomplain [file join $dir backup-*]] {
		if { [regexp {backup-([0-9]+)} $i {} pid] } {
		    #any number (for example 0) after $pid is to query 
		    #for a process not started from GiD                    
		    if { ![gidproc isalive $pid 0] } {
			set filebackupgeo [file join $i [file root [file tail $i]].geo]
			if { [file exists $filebackupgeo] && [file size $filebackupgeo] > 155 } {
			    #ignore a backap *.geo without entities (size<=155)
			    set backupfile $i
			    break
			}
		    }
		}
	    }
	}
    }

    set recoverbackup 0
    if { $backupfile != "" } {
	set filebkp [file join $backupfile [file root [file tail $backupfile]].bkp]
	if { [file exists $filebkp] } {
	    set fp [open $filebkp r]
	    gets $fp projectname
	    close $fp
	    if { $projectname == "UNNAMED" } {
		#the project is not saved
		set recoverbackup 1
	    } else {
		set filelastgeo [file join $projectname.gid [file tail $projectname].geo]
		if { ![file exists $filelastgeo] || 
		    [file mtime $filelastgeo] < [file mtime $filebkp] } {
		    #the backup is more recent that the last saved project
		    set recoverbackup 1
		}
	    }
	}
    }

    if { $recoverbackup } {
	set filemtime [clock format [file mtime $backupfile] -format "%d %b %Y %H:%M:%S"]
	set strmes [_ "The program finished abnormally."] 
	append strmes " " [_ "Do you want to recover the last backup file (date %s)?" $filemtime]
	set Ret [tk_dialogRAMFull .gid.askyn [_ "backup recover"] \
		$strmes "" "" gidquestionhead 0 [_ "Yes"] [_ "No"]]
	if { $Ret == 0 } {
	    .central.s process escape escape escape escape files read \"$backupfile\" escape
	}        
    }

    if { $backupfile != "" } {
	if { [catch { file delete -force $backupfile } msg] } {
	    WarnWinText [_ "Error deleting file $backupfile: $msg"]
	}
    }
}
