//Faza M: Upgrade baze de date pt mai multe BD

xx_os := h_Os("l")
sfisc := h_AddPath("CITESTE.TXT",h_FilePathes[fil_tmp])
#IF !h_SafeErase(sfisc)
    h_MesErr("Fisierul nu poate fi sters: "+x_FileRight(sfisc, MaxCol()+1-28))
    Abandon()
#ENDIF
h_CopyFile(homepath+"CITESTE.TXT",sfisc)
#IF xx_os="linux_wp"
    h_MemoWrit(h_AddPath("CITESTE"+h_StatId()+".CMD",h_FilePathes[fil_scat]),       ;
               "[RUN]"                                            +Chr(13)+Chr(10)+ ;
               "MODE=/MAX"                                        +Chr(13)+Chr(10)+ ;
               "DIRECTORY="                                       +Chr(13)+Chr(10)+ ;
               "EXE="+h_StPath(sfisc)                             +Chr(13)+Chr(10)+ ;
               "PARAMETERS="                                      +Chr(13)+Chr(10))
#ELSEIF "linux" $ Lower(h_Build()) .AND. xx_os = "linux"
    h_DOS("xdg-open "+sfisc)
#ELSEIF xx_os = "windows_nt"
    h_Dos("CMD /C START "+sfisc)
#ELSE
    h_DOS('START notepad.exe '+sfisc)
#ENDIF        

NewMsg()
//proprietar BD
xx_prop_bd := ""
//proprietar program
xx_prop_prg:=""
#IF !h_UseAlias("comp")
    h_MesErr("Nu se poate deschide COMPANY.DBF")
    ABANDON()
#ENDIF
DBGOTOP()
xx_prop_prg := comp->SH_NAME
h_CloseAlias("comp")

xx_nume := "FMULTIS.TXT"
xx_log  := h_AddPath(xx_nume, h_FilePathes[fil_tmp])
h_OpenOut(xx_log) 
h_PutLine("")
h_PutLine("Baze de date pentru care a fost lansata procedura de upgrade:")
h_PutLine("")

// -----------------------------------------------------------------------------
// Parcurge bazele de date 

aMsg := {"","","","","","",""}
aWin := h_NewOptions ("O",,,,,"Upgrade",,{aMsg, {" Da "," Nu "," Abandon "}},{1})

aDir := h_Directory(h_AllF(),"D")
i    := 1
ll   := Len(aDir)
#IF ll > 1
    aDir := ASort (aDir,,,{|x,y| Upper(x[1]) < Upper(y[1])})
#ENDIF

#WHILE i<=ll
    xx_cond := .T.
    // verifica daca catalog
    xx_cond := xx_cond .AND. "D"$aDir[i,5]
    // verifica daca bd HAMOR Soft
    xx_cond := xx_cond .AND. h_FileEx(h_AddPath("FILES.DBF",aDir[i,1]))
    // elimina cataloagele ., .. si BD .DEM
    xx_cond := xx_cond .AND. !(AllTrim(aDir[i,1])=".") .AND. ! (Upper(h_FileExt(aDir[i,1]))="DEM")
    // verifica sa fie .0 (DBF.0) si sa nu fie demo (.DEM) sau arhiva (.11)
    // xx_cond := xx_cond .AND. IIF(Right(aDir[i,1],2)=".0",.T.,!("."$aDir[i,1]))
    // verifica proprietar program, adica prima inregistrare 

    #IF xx_cond    
        h_NewSelect()
        xx_sfile := h_AddPath("COMPANY",aDir[i,1])
        #IF h_NetUse(xx_sfile,,,aDir[i, 1]+xx_sfile,"man")
             DbGoTop()
             xx_cond    := xx_cond .AND. (man->SH_NAME == xx_prop_prg)
             DbGoBottom()
             xx_prop_bd:=man->SH_NAME
        #ELSE
             xx_cond := .F.
        #ENDIF        
        h_CloseAlias("man")
    #ENDIF
    // verifica daca este aplicatia ceruta
    #IF xx_cond    
        xx_sfile := h_AddPath("APLIC",aDir[i,1])
        #IF h_NetUse(xx_sfile,,,aDir[i, 1]+xx_sfile,"man")
            __dbLocate( {|| man->CODE = h_ApplCode .AND. (man->V="V 7.2B1" .OR. man->V="V 7.2B2" .OR. man->V="V 7.2B3")},,,, .F. )
            #IF !Found()
                xx_cond := .F.
            #ENDIF
        #ELSE
             xx_cond := .F.
        #ENDIF        
        h_CloseAlias("man")
    #ENDIF
    
    // verifica sa se lanseze pt. 2017.10
    #IF xx_cond    
        #IF File(h_OSPath(aDir[i,1]+"\SALAR\HSALAR.DBF")) .AND. ;
            h_NetUse(h_OSPath(aDir[i,1]+"\SALAR\HSALAR.DBF"),"R",,h_OSPath(aDir[i,1]+"\SALAR\HSALAR.DBF"),"man")
             xx_cond := xx_cond .AND. ;
                        (Empty(man->DATA) .OR. h_DTOC(man->DATA,"aaaa.ll")="2017.10")
        #ELSE
             xx_cond := .F.
        #ENDIF        
        h_CloseAlias("man")
    #ENDIF
    
    #IF xx_cond
        aMsg[ 1] := ""
        aMsg[ 2] := "Baza de date : "+RTRIM(aDir[i,1])
        aMsg[ 3] := "Proprietar bd: "+xx_prop_bd
        aMsg[ 5] := ""
        aMsg[ 6] := "Executati upgrade?"
        aMsg[ 7] := ""

        lExecuta := .T.
        // ------------------------------------------------------------------
        // Ca sa nu ceara confirmare, comentati randurile de mai jos
        // ------------------------------------------------------------------
        lPopUp := h_PopUpWindow(aWin)
        #IF lPopUp .AND. aWin[13,1] == 1
            // 1-Da
            lExecuta := .T.
        #ELSEIF lPopUp .AND. aWin[13,1] == 3
            lExecuta := .F.
            // 3-Abandon
            #EXIT
        #ELSE     
            // 2-Nu sau Esc, sare peste aceasta bd
            lExecuta := .F.
        #ENDIF       
            
        #IF lExecuta
            PutMsg(aDir[i, 1])
            h_PutLine(h_PADR(aDir[i,1],20)+"-"+xx_prop_bd)
            // --------------------------------------------------------------
            // Actiune executata pentru bd curenta
            //---------------------------------------------------------------

            // --------------------------------------------------------------
            // Lansare procedura upgrade pentru bd curenta
            // --------------------------------------------------------------

            #IF xx_os = "linux"
                #IF "linux" $ Lower(h_Build())
                    h_DOS('echo "  -"'+aDir[i,1])
                #ENDIF
                h_DOS("./HMET2UP "+aDir[i,1]+" "+h_ApplCode+" "+homepath)    
            #ELSEIF xx_os = "windows_nt"
                h_DOS("cmd /c start /w hmet2up "+aDir[i,1]+" "+h_ApplCode+" TMP\TMP"+h_ApplCode)    
            #ELSE
                h_DOS("start /w    hmet2up "+aDir[i,1]+" "+h_ApplCode+" TMP\TMP"+h_ApplCode)    
            #ENDIF
            
        #ENDIF    
    #ENDIF    
    i := i+1
#ENDDO
DBCLOSEALL()
EndMsg()
h_CloseOut("")
// Afiseaza bd parcurse, pentru care s-a raspuns cu Da si s-a lansat hmet2up.
// Nu neaparat coincide cu bd pentru care s-a executat upgrade,
// pentru ca s-a lansat hmet2up, dar 
// fie nu s-au indeplinit conditii,
// fie pana la urma operatorul nu a lansat fazele de upgrade.
h_ViewFile(xx_log, "Parcurgere baze de date in regim multisocietate", 70,60,"P") 
