*  *  *  *  *  *  *  *  *  *  *  *  *  *  *  *  *  *  *  *  *  *  *  *  *  *  *  *  *  *  *  *  *  *  *  *  *  *  *
* 2014-11-05 logovano spusteni i shutdown
*            do zurnalu je pri spusteni zapsana cesta
*            v hlavicce okna se zobrazuje verze a adresare, mezi kterymi se kopiruje
*            lze ukoncit, pri shutdownu se zobrazi dotaz
*            doby prenosu jsou logaritmicky zaokrouhlovany
*            pracovni databaze jsou na konci smazany
*            pri volani bez parametru se kopiruje do podaresare .\remote, takze se testuje rychlost mistniho disku
*            v pripade neexistence remote_path se adresar vytvori
* 2014-11-06 ze zurnalu vyhozeny uvozovky, format CSV neni treba
*            na zacatek nove tvoreneho zurnalu se vypise navod k pouziti
*            
*  *  *  *  *  *  *  *  *  *  *  *  *  *  *  *  *  *  *  *  *  *  *  *  *  *  *  *  *  *  *  *  *  *  *  *  *  *  *

PARAMETERS remote_path

#DEFINE VERZE "2014-11-06"
#DEFINE POCET_VZORKU 22

gcini_path = LOWER(JUSTPATH(SYS(16)))
gcini_path = gcini_path + IIF(RIGHT(m.gcini_path, 1)="\", "", "\")
SET DEFA TO (m.gcini_path)

IF EMPTY(m.remote_path)
    remote_path = ".\remote\"
ENDIF 
remote_path = remote_path + IIF(RIGHT(m.remote_path, 1)="\", "", "\")
IF .not. DIRECTORY(m.remote_path)
    MKDIR (m.remote_path)
ENDIF 

DO nastavsety
machine = get_computername()
CLOSE DATABASES

_screen.WindowState = 1
_screen.Height = 555
_screen.Width  = 777
_screen.caption = "SITOMERKA ver. " + VERZE + "   " + gcini_path + " --> " + remote_path

ON SHUTDOWN do shut_dn

DO zurnal_log WITH "started " + _screen.caption

CREATE TABLE (m.gcini_path + m.machine) (radek I, text C(250))
FOR iii = 1 TO 9999
    INSERT INTO (m.gcini_path + machine) (radek) VALUES (m.iii)
ENDFOR 

DIMENSION casy(POCET_VZORKU)

cislo_vzorku = 0
pokracovat = .T.
DO WHILE m.pokracovat
    CLOSE DATABASES
    chybicka = ""
    poznamka = ""
    doba_kopirovani = 0
    LOCAL skin_oErr AS Exception
    TRY     
        ERASE (m.gcini_path  + "zpet.dbf")
        USE (m.gcini_path + m.machine) EXCLUSIVE ALIAS zdroj
        REPLACE ALL text WITH REPLICATE(STR(99999*rand(),5),66)
        zacatek = SECONDS()
        poznamka = " copy na server"
        COPY all to (m.remote_path + m.machine)
        SELECT 0
        poznamka = " USE na serveru"
        USE         (m.remote_path + m.machine)
        poznamka = " copy zpet"
        COPY all to (m.gcini_path  + "zpet")
        USE         (m.gcini_path  + "zpet") EXCLUSIVE 
        doba_kopirovani = SECONDS() - m.zacatek
        SELECT zdroj
        SCAN
            SCATTER MEMVAR 
            SELECT zpet
            LOCATE FOR radek=m.radek .and. text=m.text
            IF .not. FOUND()
                chybicka = chybicka + " soubory se lisi " + ALLTRIM(STR(m.radek))
            ENDIF 
        ENDSCAN 
        USE
    CATCH TO skin_oErr 
        chybicka = chybicka + ALLTRIM(STR(skin_oErr.ErrorNo)) + " " + skin_oErr.Message + " Line:" + ALLTRIM(STR(skin_oErr.LineNo)) + m.poznamka
        DO zurnal_log WITH m.chybicka
    FINALLY
    ENDTRY 

    cislo_vzorku = m.cislo_vzorku + 1
    IF m.cislo_vzorku = POCET_VZORKU+1
       retez = ""
       ASORT(casy)
       minuly_cas = -2222
       FOR iii = 1 TO POCET_VZORKU
           DO CASE 
           CASE casy[iii] = m.minuly_cas
               retez = m.retez + "x"
           CASE casy[iii] < 7
               retez = m.retez + "  " + ALLTRIM(STR(casy[iii], 8,1))
               minuly_cas = casy[iii]
           OTHERWISE 
               retez = m.retez + "  " + ALLTRIM(STR(casy[iii], 7,0))
               minuly_cas = casy[iii]
           ENDCASE
       ENDFOR
       DO zurnal_log WITH m.retez
       cislo_vzorku = 1
    ELSE
       casy[cislo_vzorku] = krouhli_cas(m.doba_kopirovani)
    ENDIF 
    ? TIME(), STR(m.cislo_vzorku, 3), ROUND(m.doba_kopirovani, 1)
    WAIT "" TIMEOUT 5
ENDDO 
CLOSE DATABASES


*!*****************************************************************************
*!       Procedure : Get_ComputerName
*!*****************************************************************************
FUNCTION get_computername

DECLARE INTEGER GetComputerName IN WIN32API STRING@ cComputerName, INTEGER@ nSize
lccomputer=SPACE(80)
lnsize=80

=getcomputername(@lccomputer,@lnsize)
IF lnsize < 2
    gcn_machine = GETENV("computername")
ELSE
    gcn_machine = SUBSTR(lccomputer,1,lnsize)
ENDIF
RETURN ALLTRIM(CHRTRAN(m.gcn_machine,  " ./\,-'_", ""))


*!*****************************************************************************
PROCEDURE nastavsety
SET SYSFORMATS ON
SET BELL TO    888,1
SET BELL       OFF
SET CARRY      OFF
SET CLOCK      OFF
SET CENTURY    ON
SET CONFIRM    ON
SET ESCAPE     ON
SET EXACT      OFF
SET TALK       OFF 
SET ECHO       OFF 
SET SAFETY     OFF
SET DELETED    ON 
SET DATE TO    GERMAN 
SET EXCLUSIVE  ON
SET NEAR       ON 
SET HOURS TO    24
SET REPROCESS TO AUTO
SET CPDIALOG   OFF
SET NULLDISPLAY TO ""
SET POINT TO "."
SET STATUS OFF 
SET STATUS BAR OFF 

*!*****************************************************************************
PROCEDURE zurnal_log
LPARAMETERS parch1
parch1   = IIF(EMPTY(m.parch1), "", m.parch1)
? m.parch1
fn_zurn = m.gcini_path + "zu" + SUBST(DTOS(DATE()), 3, 4) + m.machine + ".txt"

IF .NOT. FILE(m.fn_zurn)

   SET TEXTMERGE ON TO (m.fn_zurn)
   TEXT
   volani: sitomerka.exe [<remotepath>]
           pri volani bez parametru se kopiruje do podaresare .\remote, takze se testuje rychlost lokalniho disku
   program vytvori v aktualnim adresari docasnou databazi s nazvem "<computername>.dbf", tu kopiruje do <remotepath> , pak zpet pod nazvem "zpet.dbf" a oba soubory porovna
   do zurnalu se zaznamena doba techto kopirovani ve vterinach a pripadne chyby.
   provadi se vzdy <<POCET_VZORKU>> mereni a zurnaluje najednou; pokud se nejaky cas opakuje vickrat, je to zaznamenano poctem "x"



   ENDTEXT 
   SET TEXTMERGE TO 
ENDIF

fh__zur = -99
_iii = 0.1

DO WHILE m.fh__zur < 0 .AND. m._iii < 1
    fh__zur = FOPEN(m.fn_zurn, 2)
    IF _iii > .3
        WAIT "Cekejte, oteviram " + m.fn_zurn + STR(m._iii*10, 5) WIND TIME m._iii
    ENDIF
    _iii = m._iii + 0.2
ENDDO

IF m.fh__zur > 0
    FSEEK(m.fh__zur, 0, 2)
    m.retez = RIGHT(DTOS(DATE()),2) + " " + LEFT(TIME(),5) + '   ' + m.parch1
    FPUTS(m.fh__zur, IIF("started" $ m.parch1, CHR(13)+CHR(10), "") + m.retez)
ENDIF
FCLOSE(m.fh__zur)

TRY     
    COPY FILE (m.fn_zurn) TO (m.remote_path + JUSTFNAME(m.fn_zurn))
CATCH 
        *
FINALLY
ENDTRY 


*!*****************************************************************************
PROCEDURE shut_dn

IF MESSAGEBOX("Opravdu ukoncit program?",3+32, PROGRAM(1), 5555) = 6

    CLOSE DATABASES 
    ERASE (m.gcini_path  + "zpet.dbf")
    ERASE (m.gcini_path  + m.machine + ".dbf")
    ERASE (m.remote_path + m.machine + ".dbf")

    DO zurnal_log WITH "shut down"
    QUIT
ENDIF 


************************************************************************
FUNCTION krouhli_cas(doba)
DO CASE 
CASE m.doba < 2.3
   RETURN ROUND(m.doba/2, 1)*2
CASE m.doba < 6
   RETURN ROUND(m.doba/5, 1)*5
CASE m.doba < 13
   RETURN ROUND(m.doba, 0)
CASE m.doba < 44
   RETURN ROUND(m.doba/3, 0)*3
OTHERWISE 
   RETURN ROUND(m.doba, -1)
ENDCASE 
