option explicit

Function VerifProcess(objProcess)
  Dim Process
  For Each Process In GetObject("winmgmts:").InstancesOf("Win32_process")
    If UCase(Process.Name) = UCase(objProcess) Then
      VerifProcess = true
      Exit Function
    End If
  Next
  VerifProcess = false
End Function

Function DeleteProcess(objProcess)
  Dim Process
  For Each Process In GetObject("winmgmts:").InstancesOf("Win32_process")
    If UCase(Process.Name) = UCase(objProcess) Then
      Process.Terminate()
      Exit Function
    End If
  Next
End Function

Sub Install
  On Error Resume Next
  Dim privoxyPath, msgBoxResult, lotroIniPath, fso, flux, reg, key
  Dim aejversion, infologe
  aejversion = "2.01"
  infologe = 0
  'Message de presentation
  'msgBoxResult = MsgBox("Bienvenue dans le script de configuration du système AEJ" & vbCrLf & vbCrLf & "Ce script entièrement automatisé permet de configurer votre ordinateur afin de pouvoir profiter de services exclusifs EN JEU." & vbCrLf & vbCrLf & "Veuillez vérifier que le seigneur des anneaux est bien fermé avant de continuer. Dans le cas contraire il serait fermé automatiquement." & vbCrLf & vbCrLf & "Voulez vous qu'il en soit ainsi ?",vbOKCancel +vbQuestion,"Lotro.fr Configuration du Systeme AEJ version " & aejversion)
  Do while ( msgBoxResult <> 3 and msgBoxResult <> 4 and msgBoxResult <> 5 and msgBoxResult <> 6 )
    msgBoxResult = InputBox("Bienvenue dans le script de configuration des addons pour lotro (AEJ & Loge)" & vbCrLf & vbCrLf & "Ce script entièrement automatisé permet d'installer/désinstaller le système d'aide en jeu de Lotro.fr, ainsi que la loge intégrale." & vbCrLf & vbCrLf & "Veuillez vérifier que le seigneur des anneaux est bien fermé avant de continuer. Dans le cas contraire il serait fermé automatiquement." & vbCrLf & vbCrLf & "Souhaitez vous :" & vbCrLf & vbCrLf & " 1 - Obtenir des infos sur AEJ" & vbCrLf & vbCrLf & " 2 - Obtenir des infos sur la loge intégrale" & vbCrLf & vbCrLf & " 3 - Installer uniquement AEJ (désinstalle la loge)" & vbCrLf & vbCrLf & " 4 - Installer uniquement la loge (désinstalle AEJ)" & vbCrLf & vbCrLf & " 5 - Installer AEJ et la loge" & vbCrLf & vbCrLf & " 6 - Tout désinstaller" & vbCrLf,"Configuration du Systeme AEJ & Loge version " & aejversion)
    If msgBoxResult = vbCancel or msgBoxResult = "" Then
      Exit Sub
    End if
    If msgBoxResult = "1" Then
      MsgBox "AEJ est un addon pour lotro qui permet d'accéder aux données de lotro.fr directement depuis le jeu." & vbCrLf & vbCrLf & " - Accès aux cartes du jeu avec indication de votre position et + de 6000 poi." & vbCrLf & " - Raid planner pour organiser et/ou vous inscrire aux sorties" & vbCrLf & " - Gestion de perso (prouesses, traits, ...)" & vbCrLf & " - Gestion de confrérie" & vbCrLf & " - Base de données (objets, prouesses, ...)" & vbCrLf & " - Et plein d'autres choses (Blocnote, jeux...)", vbOKOnly+vbInformation, "Description de l'AEJ"
    End if
    If msgBoxResult = "2" Then
      MsgBox "La loge intégrale permet de prévisualiser en jeu sur son perso n'importe quel objet équipable." & vbCrLf & vbCrLf & "Cet addon est à utiliser de manière ponctuelle car il remplace le Lotro Store. Il est donc conseillé de ne l'installer que lorsque l'on souhaite s'en servir et de le désinstaller ensuite.", vbOKOnly+vbInformation, "Description de la loge"
      infologe = 1
    End if
    if ( msgBoxResult = "4" or msgBoxResult = "5" ) and infologe = 0 Then
      MsgBox "Merci de lire tout d'abord l'information sur la loge.", vbOKOnly+vbExclamation, "Avertissement"
      msgBoxResult = ""
    End if
  Loop

  'Verifie que le launcher lotro est bien coupe
  If VerifProcess("TurbineLauncher.exe") Then
    MsgBox "Le seigneur des anneaux est en cours d'exécution." & vbCrLf & "Il va maintenant être fermé.", vbOKOnly+vbExclamation, "Avertissement"
    DeleteProcess("TurbineLauncher.exe")
  End If
  If VerifProcess("lotroclient.exe") Then
    MsgBox "Le seigneur des anneaux est en cours d'exécution." & vbCrLf & "Il va maintenant être fermé.", vbOKOnly+vbExclamation, "Avertissement"
    DeleteProcess("lotroclient.exe")
  End If

  Set reg = CreateObject("WScript.Shell")
  Set fso = CreateObject("Scripting.FileSystemObject")

' Traitement relatif a privoxy sauf pour désinstall
  if msgBoxResult <> 6 then
    ' Fermer privoxy s'il est déja lancé
    DeleteProcess("privoxy.exe")
    
    'Recherche du dossier d'installation de Privoxy
    key = reg.RegRead("HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows\CurrentVersion\Uninstall\Privoxy\UninstallString")
    If key = "" Then
      key = reg.RegRead("HKEY_LOCAL_MACHINE\SOFTWARE\Wow6432Node\Microsoft\Windows\CurrentVersion\Uninstall\Privoxy\UninstallString")
      If key = "" Then
        MsgBox "Privoxy n'a pas été trouvé sur votre machine." & vbCrLf & "Merci de l'installer puis de relancer ce script de configuration.", vbOKOnly+vbCritical, "Avertissement"
        Exit Sub
      End If
    End If
                  
    privoxyPath = Replace(key,"""","")
    privoxyPath = Left(privoxyPath, InStrRev(privoxyPath, "\"))
  
    Set flux = fso.createTextFile(privoxyPath & "user.action", true)
    'AEJ
    If msgBoxResult = "3" or msgBoxResult = "5" Then
      flux.writeline("{-fast-redirects +redirect{s@http://my.lotro.com/in-game/\?(.+)@http://lotro.fr/ingame/?aejversion=" & aejversion & "&$1@}}")
      flux.writeline("my.lotro.com/in-game/")
    end if
    'Loge
    If msgBoxResult = "4" or msgBoxResult = "5" Then
      flux.writeline("{+redirect{s@(.+)@http://lotro.fr/loge/?$1&@}}")
      flux.writeline("turbine.playspan.com:443")
    end if
    flux.Close()
  
    Set flux = fso.createTextFile(privoxyPath & "config.txt", true)
    flux.writeline("confdir "&privoxyPath)
    flux.writeline("logdir "&privoxyPath)
    flux.writeline("")
    flux.writeline("actionsfile "&privoxyPath&"user.action")
    flux.Close()
  end if
  
  'Recherche du dossier contenant le UserPreferences.ini
  key = reg.RegRead("HKEY_CURRENT_USER\Software\Microsoft\Windows\CurrentVersion\Explorer\Shell Folders\Personal")
  If key = "" Then
          MsgBox "Une erreur est survenue !" & vbCrLf & "Le répertoire 'Mes Documents' est introuvable. Merci de contacter un admin de lotro.fr",vbOKOnly+vbCritical,"Avertissement"
          Exit Sub
  End If
  lotroIniPath =  Replace(key,"""","") & "\The Lord of the Rings Online\"

  'Generation du nouveau fichier de config : utilisation de privoxy
  If fso.FileExists(lotroIniPath & "UserPreferences.ini.bak") <> "" Then
          fso.CopyFile lotroIniPath & "UserPreferences.ini", lotroIniPath & "UserPreferences.ini.bak"
  End If
  Set flux = fso.createTextFile(lotroIniPath & "UserPreferences.ini", true)
  Dim ObjTextStream : Set ObjTextStream = Fso.OpenTextFile(lotroIniPath & "UserPreferences.ini.bak", 1, False)
  Dim strtmp, i, line
  Do While Not ObjTextStream.AtEndOfStream
    strtmp = Split(ObjTextStream.ReadAll, vbCrLf)
    For i = 0 To UBound(strtmp)
      line = strtmp(i)
      if msgBoxResult = "6" then 'desinstallation
        If InStr(line, "HttpsProxyPort=8118") Then
          line = "HttpsProxyPort="
        End If
        If InStr(line, "HttpProxyPort=8118") Then
          line = "HttpProxyPort="
        End If
        If InStr(line, "HttpProxyHost=127.0.0.1") Then
          line = "HttpProxyHost="
        End If
        If InStr(line, "HttpsProxyHost=127.0.0.1") Then
          line = "HttpsProxyHost="
        End If
        If InStr(line, "InitialConfigDone=True") Then
          line = "InitialConfigDone="
        End If
      else
        If InStr(line, "HttpsProxyPort=") Then
          line = "HttpsProxyPort=8118"
        End If
        If InStr(line, "HttpProxyPort=") Then
          line = "HttpProxyPort=8118"
        End If
        If InStr(line, "HttpProxyHost=") Then
          line = "HttpProxyHost=127.0.0.1"
        End If
        If InStr(line, "HttpsProxyHost=") Then
          line = "HttpsProxyHost=127.0.0.1"
        End If
        If InStr(line, "InitialConfigDone=") Then
          line = "InitialConfigDone=True"
        End If
      end if
      flux.writeline(line)
    Next
  Loop
  ObjTextStream.Close
  flux.Close
  Set ObjTextStream = Nothing
  Set Fso = Nothing

  'Lancement de privoxy sauf pour désinstall
  if msgBoxResult <> 6 then
    Dim wshell
    Set wshell = CreateObject("WScript.Shell")
    wshell.Run """"&privoxyPath&"privoxy.exe"" """&privoxyPath&"config.txt", 0
  end if
  
  MsgBox "Configuration terminée avec succès",vbOKOnly+vbInformation,"Configuration terminée"
End Sub

Install