*************************************************************************************
*
*   2015-09-10  opravi i tabulky s fieldy typu autoinc, ktere knihovna qfix neumi
*   2018-02-20  packuje databaze UG__*
*   2018-11-27  databaze, ktere lze postradat, se pri neopravitelne chybe proste smazou
*   2019-09-24  mazani postradatelnych obaleno try-catch
*
*************************************************************************************

IF m.issql
	RETURN
ENDIF 
IF EMPTY(m.gcpri_path)
    gcpri_path = ".\tmp\"
    DO mk_dir WITH m.gcpri_path
ENDIF 

set library to qfixdbf.fll ADDITIVE 
CLOSE DATA

soubory = m.gcpri_path + "s_" + SYS(3)
do najdi_soubory
DELETE FOR LEFT(nazev, 3) = "cen"
DELETE FOR LEFT(nazev, 6) = "uc_pol"
DELETE FOR SUBSTR(nazev, 2,1) = "2"
DELETE FOR LEFT(nazev, 1) = "2"

zobrazit = .F.
SCAN
    cas = SECONDS()
    nazev = ""
    otevreno = allt(soubory.adresar)

    ON ERROR DO opravovac_otevirace with ERROR(), MESSAGE()
    sele 0
    use (m.otevreno) EXCLUSIVE 

    IF ATC("UG__", m.otevreno) > 0
        PACK 
    ENDIF 

    USE 
    ON ERROR 

    cas = ROUND(SECONDS() - m.cas, 1)
    IF m.cas > .5
        ACTIVATE SCREEN 
        ? m.cas, m.otevreno
        zobrazit = .T.
    ENDIF 

    sele soubory
    gather field nazev memvar
ENDSCAN 
set filter to ! empty(nazev)
CLOSE DATABASES 
IF m.zobrazit
     WAIT "" TIMEOUT 5
ENDIF 
RELEASE LIBRARY qfixdbf
ERASE (m.soubory + ".dbf")



* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * 
* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * 
procedure najdi_soubory
CREATE TABLE (m.soubory) FREE (adresar c(222), nazev c(111), atrib c(1), delka I)
USE (m.soubory) ALIAS soubory EXCLUSIVE 
do jedenadresar with m.gchom_path

sele soubory
do while .t.
  locate for "D" $ atrib .and. delka = 0
  if found()
     repl delka with -1111111
     do jedenadresar with allt(adresar) + allt(nazev) + "\"
  else
     exit
  endif
ENDDO
delete for atrib = "D"
delete for right(alltrim(nazev), 4) # ".dbf"
#IF "SLOVAK"$UPPER(GETENV("language"))
	DELETE FOR "\D_KAS\"$UPPER(adresar)
#ENDIF 
replace ALL adresar with alltrim(adresar) + nazev
DELETE FOR ATC("\_tm", adresar) > 0


* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * 
proc jedenadresar
param adresar
pocet = adir(qqq, m.adresar + "*.*", "D")
for ii = 1 to m.pocet
  if left(qqq(ii, 1), 1) # "."
     insert into soubory (adresar, nazev, atrib, delka) ;
        value (lowe(m.adresar), lowe(qqq(ii, 1)), iif("D" $ qqq(ii, 5), "D", ""), qqq(ii, 2))
  endif
endfor


* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * 
PROCEDURE opravovac_otevirace
LPARAMETERS err_no, mssg

mssg = str(m.err_no,5) + ":" + m.mssg
ACTIVATE SCREEN 
? m.otevreno, m.mssg

** osetreni databazi s autoinc, ktere knihovna qfix neumi    ZACATEK
#DEFINE FILETYPE_VFP         CHR(48)
#DEFINE FILETYPE_VFP_AUTOINC CHR(49)

file_type_bajt = FILETYPE_VFP
fh = FOPEN(m.otevreno, 2)
IF fh > 0
   file_type_bajt = FGETS(m.fh, 1)
   IF m.file_type_bajt = FILETYPE_VFP_AUTOINC
      FSEEK(m.fh, 0, 0)
      FWRITE(m.fh, FILETYPE_VFP)
   ENDIF 
   FCLOSE(m.fh)
ENDIF 
** osetreni databazi s autoinc, ktere knihovna qfix neumi    KONEC


*   smazani CDX nulove delky
IF ADIR(qqq, strtran(m.otevreno, ".dbf", ".cdx")) = 1
   IF qqq(1,2) = 0
        ERASE STRTRAN(m.otevreno, ".dbf", ".cdx")
        small_log ("SMAZAN NULOVY CDX " + m.otevreno, "poskozene_soubory")
   ENDIF 
ENDIF 


vysledek = Q_FIXDBF(m.otevreno)
DO case
CASE err_no = 1705    && used by another
*    ? m.otevreno + m.mssg
 
CASE BITAND(m.vysledek, 14)> 0  &&    neopravitelne chyby hlavicky(8), nerozpoznany format(4), prilis kratky soubor(2)
    zobrazit = .T.
    RENAME (m.otevreno) TO (strtran(m.otevreno, ".dbf", ".db_" + TTOC(DATETIME(), 1)))
    ERASE                   strtran(m.otevreno, ".dbf", ".cdx")
    small_log ("NEOPRAVITELNA CHYBA " + m.otevreno, "poskozene_soubory")

CASE INLIST(err_no, 41)    && memo(.fpt) not found or corrupted
    zobrazit = .T.
    smazat_fpt = .T.
    fpt_filename = STRTRAN(m.otevreno, ".dbf", ".fpt")
    IF FILE(m.fpt_filename)
       vysledek = Q_FIXFPT( m.fpt_filename )
       IF m.vysledek = 4    && poskozeny .fpt
          vysledek = Q_FIXFPT( m.fpt_filename , 4)
          IF m.vysledek = 0
             smazat_fpt = .F.
          ENDIF 
       ENDIF
    ENDIF 
    IF m.smazat_fpt
       IF FILE(m.fpt_filename)
           RENAME (m.fpt_filename) TO (strtran(m.fpt_filename, ".fpt", ".fp_" + TTOC(DATETIME(), 1)))
       ENDIF 
       vysledek = Q_FIXDBF(m.otevreno, 256)
       small_log ("MEMO(.FPT) SMAZAN " + m.otevreno, "poskozene_soubory")
    ENDIF 


CASE INLIST(err_no, 1707, 2066, 114, 1141)    && cdx not found, corrupted, doesn't match, unrecognized
    zobrazit = .T.
    erase strtran(m.otevreno, ".dbf", ".cdx")
    erase strtran(m.otevreno, ".dbf", ".idx")
    = Q_FIXDBF(m.otevreno, 128)
    use (m.otevreno) EXCLUSIVE 
    DO indexuj IN indexy
    small_log ("REINDEX " + m.otevreno, "poskozene_soubory")
    USE 


*CASE inlist(err_no, 15, 2091)    && not a table, table corrupted
OTHERWISE        && ve vsech ostatnich pripadech se pokusim o opravu
    zobrazit = .T.
    nazev = m.nazev + m.mssg
    vysledek = Q_FIXDBF(m.otevreno, 16+32+64)
    ACTIVATE SCREEN 
    ?? " .... fix"
    small_log ("Q_FIXDBF: " + IIF(m.vysledek=0, "OK", "NEOPRAVENO, CHYBA " + ALLTRIM(STR(m.vysledek))) + " " + m.otevreno, "poskozene_soubory")
    USE 

ENDCASE 


** osetreni databazi s autoinc, ktere knihovna qfix neumi
IF m.file_type_bajt = FILETYPE_VFP_AUTOINC
    fh = FOPEN(m.otevreno,1)
    IF fh > 0
          FSEEK(m.fh, 0, 0)
          FWRITE(m.fh, FILETYPE_VFP_AUTOINC)
       FCLOSE(m.fh)
    ENDIF 
ENDIF 


** tyhle databaze se muzou klidne smazat, pokud se je nepodarilo opravit
IF ATC("err_log.", m.otevreno) + ATC("errors.", m.otevreno) + ATC("semafor", m.otevreno) + ATC("machines", m.otevreno) + ATC("ser_time", m.otevreno) + ATC("xxx", m.otevreno) > 0
    TRY 
        USE (m.otevreno) EXCLUSIVE 
    CATCH
        TRY 
            ERASE (m.otevreno)
        CATCH
        ENDTRY 
        DO loguj WITH "smazan neopravitelny " + m.otevreno
    ENDTRY 
    USE 
ENDIF 
