#DEFINE CRLF CHR(13)+CHR(10)

*   *   *   *   *   *   *   *   *   *   *   *
FUNCTION alphanum (texticek, dalsi_znaky)
IF PARAMETERS() = 1
    dalsi_znaky = ""
ENDIF 
ret = ""
FOR iii = 1 TO LEN(m.texticek)
    pismenko = SUBSTR(m.texticek, m.iii, 1)
    IF BETWEEN(m.pismenko, "A", "Z") .or. BETWEEN(m.pismenko, "a", "z") .or. m.pismenko $ "0123456789" + m.dalsi_znaky    && isalpha neni sikovne, pusti i diakritikova pismenka, ktera se do nazvu souboru nehodi
        ret = m.ret + m.pismenko
    ENDIF 
ENDFOR 
RETURN m.ret


*   *   *   *   *   *   *   *   *   *   *   *
FUNCTION update_or_insert (jmeno, hodnota, soubor_alias)
LOCAL zatvaraj
IF !USED(soubor_alias)
	zatvaraj=.t.
	IF m.issql
		PRIVATE hladajmodul, hladajvarname, hladajsetupskup
		hladajmodul = "FOOD_SET"
		hladajsetupskup=""
		rv("defsubor",soubor_alias)
		CURSORSETPROP("Buffering" ,5, soubor_alias)
	ELSE
		USE (m.gchom_path + "food_set") ALIAS ddnn IN 0 AGAIN 
	ENDIF 
ELSE
	zatvaraj=.f.
ENDIF 
SELECT (soubor_alias)
LOCATE FOR var_name = m.jmeno
IF FOUND()
	REPLACE var_value WITH '"' + m.hodnota + '"'
ELSE 
	IF m.issql
		INSERT INTO (soubor_alias) (var_name, var_value, modul) VALUES (jmeno, '"' + m.hodnota + '"', "FOOD_SET")
	ELSE
		INSERT INTO (soubor_alias) (var_name, var_value) VALUES (jmeno, '"' + m.hodnota + '"')
	ENDIF 
ENDIF 
IF zatvaraj
	USE IN &soubor_alias
ENDIF 


*   *   *   *   *   *   *   *   *   *   *   *   *   *   *   *   *   *   *   *   *   
FUNCTION send_lic
PARAMETERS jmeno_souboru, pripona
jmeno_souboru = LOWER(m.jmeno_souboru)
pripona       = LOWER(m.pripona)

STRTOFILE(CRLF + CRLF + "==================  " + TTOC(DATETIME(), 3) + "  ==================" + CRLF, m.jmeno_souboru + ".log", 1)
STRTOFILE(jmeno_souboru + "   " + pripona                                                     + CRLF, m.jmeno_souboru + ".log", 1)
WAIT "sendl" WINDOW NOWAIT 


kredenc = ' -hostkey "ssh-ed25519 255 SHA256:OHtAAkao2UGChCJl0sdyKXBCpIqFTq2tCqyhk4/4sOE" -pw 1lic2LIC. licence.asrv.cz@asrv.cz'     &&&&   tohle je na asrv.cz s novym certem
cmd = gcini_path + "psftp -bc -be -v -b send_lic.sftp " + kredenc + " >" + "send_lic.log 2>send_lic2.log"
STRTOFILE(m.cmd + CRLF, ".\send_lic.bat")

DO CASE 
CASE m.pripona = "info"
    STRTOFILE("cd /info"                                  + CRLF, ".\send_lic.sftp"   )
    STRTOFILE("put "          + m.jmeno_souboru + ".info" + CRLF, ".\send_lic.sftp", 1)
    STRTOFILE("put "          + m.jmeno_souboru + ".log"  + CRLF, ".\send_lic.sftp", 1)
    STRTOFILE("cd /licence"                               + CRLF, ".\send_lic.sftp", 1)
    STRTOFILE("get " + JUSTSTEM(m.jmeno_souboru) + ".lic" + CRLF, ".\send_lic.sftp", 1)

CASE m.pripona = "req"
    STRTOFILE("cd /info"                                  + CRLF, ".\send_lic.sftp"   )
    STRTOFILE("put "          + m.jmeno_souboru + ".log"  + CRLF, ".\send_lic.sftp", 1)
    STRTOFILE("cd /licence"                               + CRLF, ".\send_lic.sftp", 1)
    STRTOFILE("put " +          m.jmeno_souboru  + ".req" + CRLF, ".\send_lic.sftp", 1)
    STRTOFILE("get " + JUSTSTEM(m.jmeno_souboru) + ".lic" + CRLF, ".\send_lic.sftp", 1)

OTHERWISE  && posilani licence z licencatoru
    STRTOFILE("cd /licence"                               + CRLF, ".\send_lic.sftp"   )
    STRTOFILE("put " + m.jmeno_souboru + "." + "lic"      + CRLF, ".\send_lic.sftp", 1)

    TRY 
        COPY FILE (m.jmeno_souboru + ".lic") TO ("d:\bitvise\updater\licence\" + JUSTFNAME(jmeno_souboru) + ".lic")
    CATCH 
        WAIT "nepodarilo se nakopirovat " + m.jmeno_souboru + ".lic" + "  -->  " + "d:\bitvise\updater\licence\" + JUSTFNAME(jmeno_souboru) + ".lic" WINDOW TIMEOUT 5
    ENDTRY     

*    STRTOFILE(m.cmd + CRLF, ".\send_lic.bat", 1)

ENDCASE 
    
proved_a_loguj_sftp()

IF "FATAL ERROR: Host key" $ FILETOSTR(".\send_lic2.log")
    kredenc = ' -hostkey "ssh-rsa 2048 SHA256:eVxVrWQEDzaNxbo+h+q/AxRYyApXHMVBip0ZL78mn/Q" -pw 1lic2LIC. licence.asrv.cz@asrv.cz' 
    cmd = gcini_path + "psftp -bc -be -v -b send_lic.sftp " + kredenc + " >" + "send_lic.log 2>send_lic2.log"
    STRTOFILE(m.cmd + CRLF, ".\send_lic.bat")
    proved_a_loguj_sftp()

    IF  .T.  &&            "FATAL ERROR: Host key" $ FILETOSTR(".\send_lic2.log")
        kredenc = ' -pw 1lic2LIC. licence.asrv.cz@asrv.cz' 
        cmd = "echo n|" + gcini_path + "psftp -bc -be -v -b send_lic.sftp " + kredenc + " >" + "send_lic.log 2>send_lic2.log"
        STRTOFILE(m.cmd + CRLF, ".\send_lic.bat")
        proved_a_loguj_sftp()
    ENDIF 

ENDIF 

ERASE                    .\send_lic.bat
ERASE                    .\send_lic.sftp


* pokud byl po poslani req nebo info getovan lic, je v aktualnim adresari, tak ho presunu do hom_path
IF FILE(JUSTSTEM(m.jmeno_souboru) + ".lic")
    datum_v_poslanem = LEFT(FILETOSTR(JUSTSTEM(m.jmeno_souboru) + ".lic"), 10)
    IF m.datum_v_poslanem > TTOC(DATE() - 7, 3)
        ERASE                                          (m.jmeno_souboru + ".lic")
        IF JUSTSTEM(m.jmeno_souboru) # m.jmeno_souboru
            RENAME (JUSTSTEM(m.jmeno_souboru) + ".lic") TO (m.jmeno_souboru + ".lic")
        ENDIF 
    ENDIF 
ENDIF 

*   *   *   *   *   *   *   *   *   *   *   *   *   *   *   *   *   *   *   *   *   
FUNCTION proved_a_loguj_sftp
RUN  .\send_lic.bat


cmd = STRTRAN(FILETOSTR(".\send_lic.bat" ), "-pw 1lic2LIC.", "")
pppoz = AT(">", m.cmd)
IF pppoz > 0
    cmd = LEFT(m.cmd, m.pppoz-1)
ENDIF 
STRTOFILE( CRLF + "CMD: "                    + m.cmd                                                     , m.jmeno_souboru + ".log", 1)
STRTOFILE( CRLF + "psftp.exe STDOUT:" + CRLF + STRTRAN(FILETOSTR(".\send_lic.log" ), "psftp> quit"  , ""), m.jmeno_souboru + ".log", 1)
STRTOFILE( CRLF + "psftp.exe STDERR:" + CRLF +         FILETOSTR(".\send_lic2.log")                      , m.jmeno_souboru + ".log", 1)


*   *   *   *   *   *   *   *   *   *   *   *   *   *   *   *   *   *   *   *   *   
FUNCTION jsem_v_altu
#DEFINE ALTO_ADDRESS_SUBSTRING "10.109.3"

oWMI = getobject("winmgmts:")
oAdapters = oWMI.ExecQuery("Select * from Win32_NetworkAdapterConfiguration where IPEnabled=True")

jsme_v_altu = .F.
for each oAdapter in oAdapters
    if not isnull(oAdapter.ipaddress)
        for each cAddress in oAdapter.ipaddress
*            ? cAddress 
            IF ALTO_ADDRESS_SUBSTRING $ m.cAddress
                jsme_v_altu = .T.
                WAIT "jooo, jsme v Altu   " + m.cAddress WINDOW TIMEOUT 2
            ENDIF 
        next
    endif
NEXT
RETURN m.jsme_v_altu


*   *   *   *   *   *   *   *   *   *   *   *   *   *   *   *   *   *   *   *   *   
FUNCTION spocti_strediska (odkud)
PRIVATE soucet, o_pocet
SELECT left(pgm,1) as pgm, CNT(pgm) as pocet GROUP BY 1 ORDER BY 2 FROM (m.odkud) INTO CURSOR spocti_strediska
soucet = ""
o_pocet = 1
SCAN 
   IF pocet # o_pocet
       soucet = soucet + trans(pocet)
       o_pocet = pocet
   ENDIF 
   soucet = m.soucet + pgm
ENDSCAN 
USE IN spocti_strediska
RETURN m.soucet


*  *  *  *  *  *  *  *  *  *  *  *  *  *  *  *  *  *  *  *  *
FUNCTION add_2_txt(kousektextu)
txt = m.txt + m.kousektextu + CRLF



*   *   *   *   *   *   *   *   *   *   *   *
FUNCTION s_h_a_hex (retizek, oddelovac)
pom = LEFT( chrtohex(s_h_a_1(m.retizek)),15)
IF EMPTY(m.oddelovac)
    RETURN m.pom
ELSE 
    RETURN LEFT(m.pom, 5) + m.oddelovac + SUBSTR(m.pom, 6, 5) + m.oddelovac + SUBSTR(m.pom, 11, 5)
ENDIF 

*   *   *   *   *   *   *   *   *   *   *   *
FUNCTION s_h_a_1 (pcstringin)
LOCAL lcin, i, j, l, T, h0, h1, h2, h3, h4, a, b, c, d, e
LOCAL ARRAY w(80)

l = MOD(LEN(pcstringin)+1+8,64)
lcin = pcstringin + ;
    CHR(0x80) + ;
    REPLICATE(CHR(0x00), 64-MOD(LEN(pcstringin)+1+8,64))+;
    numtobin (8*LEN(pcstringin), 8)

h0 = 0x67452301
h1 = 0xefcdab89
h2 = 0x98badcfe
h3 = 0x10325476
h4 = 0xc3d2e1f0

FOR i = 1 TO LEN(lcin)-64+1 STEP 64
    FOR T=0 TO 15
        w(T+1) = ;
            256*256*256*ASC(SUBSTR(lcin,i+4*T+0,1))+;
            256*256*ASC(SUBSTR(lcin,i+4*T+1,1))+;
            256*ASC(SUBSTR(lcin,i+4*T+2,1))+;
            ASC(SUBSTR(lcin,i+4*T+3,1))
    ENDFOR

    FOR T = 16 TO 79
        w(T+1) = sn(1, BITXOR(BITXOR(BITXOR(w(T+1-3),w(T+1-8)),w(T+1-14)),w(T+1-16)))
    ENDFOR
    a = h0
    b = h1
    c = h2
    d = h3
    e = h4
    FOR T = 0 TO 79
        temp = sn(5,a) + F(T,b,c,d) + e + w(T+1) + k(T)
        e = d
        d = c
        c = sn(30,b)
        b = a
        a = temp

    ENDFOR
    h0 = h0 + a
    h1 = h1 + b
    h2 = h2 + c
    h3 = h3 + d
    h4 = h4 + e
ENDFOR
RETURN ;
    numtobin(h0,4)+;
    numtobin(h1,4)+;
    numtobin(h2,4)+;
    numtobin(h3,4)+;
    numtobin(h4,4)

FUNCTION sn(N, x)
RETURN BITOR(BITLSHIFT(x,N), BITRSHIFT(x,32-N))

FUNCTION F(T,b,c,d)

DO CASE
    CASE BETW(T,  0, 19)
        RETURN BITOR(BITAND(b,c),BITAND(BITNOT(b),d))
    CASE BETWEEN(T, 20, 39)
        RETURN BITXOR(BITXOR(b,c),d)
    CASE BETWEEN(T, 40, 59)
        RETURN BITOR(BITOR(BITAND(b,c),BITAND(b,d)),BITAND(c,d))
    CASE BETWEEN(T, 60, 79)
        RETURN BITXOR(BITXOR(b,c),d)
ENDCASE

FUNCTION k(T)

DO CASE
    CASE BETWEEN(T,  0, 19)
        RETURN 0x5a827999
    CASE BETWEEN(T, 20, 39)
        RETURN 0x6ed9eba1
    CASE BETWEEN(T, 40, 59)
        RETURN 0x8f1bbcdc
    CASE BETWEEN(T, 60, 79)
        RETURN 0xca62c1d6
ENDCASE


*  *  *  *  *  *  *  *  *  *  *  *  *  *  *  *  *  *  *  *  *
FUNCTION numtobin (pnnumber, pnnoofbytes)
LOCAL num, i, ret
ret = ""
num = pnnumber
IF num<0
    num = num+0xffffffff+1
ENDIF
FOR i = 1 TO pnnoofbytes
    ret = CHR(MOD(num,256))+ret
    num = INT(num/256)
ENDFOR
RETURN ret


*  *  *  *  *  *  *  *  *  *  *  *  *  *  *  *  *  *  *  *  *
FUNCTION chrtohex (pcstring)
LOCAL i, N, ret, retez
retez      = "ABCDEFGHIJKLMNOP"
ret = ""
FOR i = 1 TO LEN(pcstring)
    N = ASC(SUBSTR(pcstring,i,1))
    ret = ret + ;
        SUBSTR(m.retez,INT(N/16)+1,1) + ;
        SUBSTR(m.retez,MOD(N,16)+1,1)
ENDFOR
RETURN ret
