Option Explicit
Dim oFSO, oWS, sDir, sDesk, sBase
Set oFSO = CreateObject("Scripting.FileSystemObject")
Set oWS = WScript.CreateObject("WScript.Shell")
sBase = "https://smallflow.hubgcontabil.com"
Dim sApp
sApp = "retaguarda"
sDir = oWS.ExpandEnvironmentStrings("%LocalAppData%") & "\SmallFlow"
If Not oFSO.FolderExists(sDir) Then oFSO.CreateFolder(sDir)
sDesk = oWS.SpecialFolders("Desktop")

Function Baixar(sUrl, sDestino)
    Dim oHttp, oStream
    Baixar = False
    On Error Resume Next
    Set oHttp = CreateObject("MSXML2.XMLHTTP")
    oHttp.Open "GET", sUrl, False
    oHttp.Send
    If Err.Number = 0 And oHttp.Status = 200 Then
        Set oStream = CreateObject("ADODB.Stream")
        oStream.Type = 1
        oStream.Open
        oStream.Write oHttp.responseBody
        oStream.SaveToFile sDestino, 2
        oStream.Close
        If oFSO.FileExists(sDestino) Then Baixar = True
    End If
    On Error Goto 0
End Function

Function AcharNavegador()
    Dim c, i
    c = Array( _
        oWS.ExpandEnvironmentStrings("%ProgramFiles(x86)%") & "\Microsoft\Edge\Application\msedge.exe", _
        oWS.ExpandEnvironmentStrings("%ProgramFiles%") & "\Microsoft\Edge\Application\msedge.exe", _
        oWS.ExpandEnvironmentStrings("%ProgramFiles%") & "\Google\Chrome\Application\chrome.exe", _
        oWS.ExpandEnvironmentStrings("%ProgramFiles(x86)%") & "\Google\Chrome\Application\chrome.exe", _
        oWS.ExpandEnvironmentStrings("%LocalAppData%") & "\Google\Chrome\Application\chrome.exe")
    AcharNavegador = ""
    For i = 0 To UBound(c)
        If oFSO.FileExists(c(i)) Then
            AcharNavegador = c(i)
            Exit Function
        End If
    Next
End Function

Sub AtalhoUrl(sNome, sUrl, sIcone)
    Dim f
    Set f = oFSO.CreateTextFile(sDesk & "\" & sNome & ".url", True)
    f.WriteLine "[InternetShortcut]"
    f.WriteLine "URL=" & sUrl
    If sIcone <> "" Then
        f.WriteLine "IconFile=" & sIcone
        f.WriteLine "IconIndex=0"
    End If
    f.Close
End Sub

Dim sIcoSF, sIcoPDV, temSF, temPDV, sNav, lnk
sIcoSF = sDir & "\smallflow-amarelo.ico"
sIcoPDV = sDir & "\pdv-nfce.ico"
temSF = False
temPDV = False
If sApp <> "nfce" Then temSF = Baixar(sBase & "/assets/smallflow-amarelo.ico", sIcoSF)
If sApp <> "retaguarda" Then temPDV = Baixar(sBase & "/assets/pdv-nfce.ico", sIcoPDV)
If Not temSF Then sIcoSF = ""
If Not temPDV Then sIcoPDV = ""

Dim sFeitos
If sApp <> "nfce" Then
    ' Small Flow Retaguarda - navegador padrao (remove versoes anteriores e recria sem duplicar)
    On Error Resume Next
    oFSO.DeleteFile sDesk & "\Small Flow.url", True
    oFSO.DeleteFile sDesk & "\Small Flow Retaguarda.url", True
    On Error Goto 0
    AtalhoUrl "Small Flow Retaguarda", sBase & "/login.php?atalho=retaguarda", sIcoSF
    sFeitos = sFeitos & "  - Small Flow Retaguarda (amarelo)" & vbCrLf
End If

If sApp <> "retaguarda" Then
    ' Small Flow NFCe (PDV) - janela propria (modo aplicativo) quando houver Edge/Chrome
    On Error Resume Next
    oFSO.DeleteFile sDesk & "\PDV Small Flow.lnk", True
    oFSO.DeleteFile sDesk & "\PDV Small Flow.url", True
    oFSO.DeleteFile sDesk & "\Small Flow NFCe.lnk", True
    oFSO.DeleteFile sDesk & "\Small Flow NFCe.url", True
    On Error Goto 0
    sNav = AcharNavegador()
    If sNav <> "" Then
        Set lnk = oWS.CreateShortcut(sDesk & "\Small Flow NFCe.lnk")
        lnk.TargetPath = sNav
        lnk.Arguments = "--app=" & sBase & "/pdv.php?origem=app"
        lnk.Description = "Small Flow NFCe (PDV)"
        lnk.WorkingDirectory = oFSO.GetParentFolderName(sNav)
        If sIcoPDV <> "" Then lnk.IconLocation = sIcoPDV
        lnk.Save
    Else
        AtalhoUrl "Small Flow NFCe", sBase & "/pdv.php?origem=app", sIcoPDV
    End If
    sFeitos = sFeitos & "  - Small Flow NFCe (azul - abre direto no PDV, pedindo o login)" & vbCrLf
End If

MsgBox "Pronto! Criado na Area de Trabalho:" & vbCrLf & vbCrLf & sFeitos, vbInformation, "Small Flow"