@ECHO OFF
TITLE configure_client
ECHO Configuring Zwift client to use zoffline
ECHO.

NET SESSION >nul 2>&1 || ( PowerShell start -verb runas '%~0' & EXIT /B )

CD /D "%~dp0"

SET HOSTS="%WINDIR%\system32\drivers\etc\hosts"
COPY %HOSTS% %HOSTS%.bak >nul
TYPE %HOSTS%.bak | FINDSTR /V /I zwift > %HOSTS%
ECHO Adding servers to hosts file
ECHO 127.0.0.1 us-or-rly101.zwift.com secure.zwift.com cdn.zwift.com launcher.zwift.com>>%HOSTS%

ECHO.

certutil.exe -store Root | FIND /C /I "54f7f293407370a07679885767e5bd599458e471" >nul 2>&1
IF %ERRORLEVEL% NEQ 0 (
    ECHO Importing certificate
    (
    ECHO -----BEGIN CERTIFICATE-----
    ECHO MIIK/QIBAzCCCsMGCSqGSIb3DQEHAaCCCrQEggqwMIIKrDCCBOcGCSqGSIb3DQEH
    ECHO BqCCBNgwggTUAgEAMIIEzQYJKoZIhvcNAQcBMBwGCiqGSIb3DQEMAQYwDgQIY6PJ
    ECHO vb+86aMCAggAgIIEoCQbnzNfXivoA8tNaMbaHobG3XMGstm7ouyZIvk0Ohd/j//N
    ECHO bT/QaGXKT/961E9WOewkprtpAmZOP7LGF52INGlGI4hi4Nz385vXURIag0Dcpi+u
    ECHO 12LTF0HIv2Lnma8y9zljjQfN7g23b5plxgXu/JWeT421/agArsnE6zB5p/m8HjPB
    ECHO IaaaN8ird5IKAEfYY7dRyEFmr+7QuEqorK4ebjpaXbK4/C79Imtmg1rdv31uUb9C
    ECHO bSOfsXSfu5kloL1e1onAaSrokNPQ2zr14xU8QYhhXYG+0/NphrqJOV8uomisx18K
    ECHO AJ1nZJE8BlwpnISzQeHEJ21js+QOWJrusDFI9AO6+wP2ojPRv+UI5dZYN19McO7b
    ECHO PqNS8DJ6zTfQgSOTs/3D1Gh9prIciC915ifJoEbCCrwHFhUXcp/3K+pQEHODqhFb
    ECHO EJ5xzvJjgpEMAxdYxmfqctTZSZyUb0SqAwMYfIXnWTWXIuow1Ffmmy/qCI49sZIo
    ECHO MyaE9VCpXTOFxEp/BeIeor5PBd/MqNPGaJwGo7ZuUdBgengHqOJzCbQ0JDF8HSmL
    ECHO hi78eHoC6qf6vkYNYM0cBvuX46mCHbVaMdWQwKcVwhVHLKcxjyvT/B0kpWVWiGX8
    ECHO Ol86k9+VO/kMzZkCGA2r01T0SVj04SZJ1jizqRZKEuUOF1pm9mRuHhgKAfNNMcKi
    ECHO UvQ71HWCKMEKu1I9gau++GHCgztC6rGnKLNOIUaix9o7To1EKBVA352pJTJ5weav
    ECHO HonRB0oroxdMfPQ/LanbYvsxpgkiwq/9mxsPTNfZZGuPgOyptXu6AYGVbUN47Wwf
    ECHO jzHFnHcTyuyZMOsCZ/3SgICwQCNNc8sbamAu1e60KVsc8H4ttpAgxcEL4ssz1qPG
    ECHO xFBqWJQm5HshzZ+3mYPz/G5b1BcvniMsWDZQcm5TlpyNVd+LvCPSEFER8SBXl0P6
    ECHO E6aUyVqLCdoR3oaQSyTgIxKRSbOM0PNX2m5kquWusbfdl+QdsK1iuY0hEd6hqXQ4
    ECHO +Sa1xXhKUCWgzol8FcQduLriw6SpnBk/toqRSzcDZzqNSQAuOKIm6qlvmwn4FLYT
    ECHO UlRuwAzB4qX04hdtSR15LUANVWI9Dp85FaZjIoa+Fdf55kQuBlgBKJ50kzDSwJxa
    ECHO y2EsLS5L+3cqhb/7Px2yDgCpbEDB0/ildwhJiHd6Co/JbQMCpIsN70qHrFuvlmSy
    ECHO XTx46Fg5uixNCZnoH88h7irDTtdOBL84D32WIZPSQBVHv+ffOmqudJm9kC/k2OHV
    ECHO vy3FXsxC+6swOro25A02pEcKRmfStRfxpiq8/RsftpsWlFdCsZ8gOtfhDKfS8fSN
    ECHO OGo1MUWGAbL2OQfsAKNGQzNRdjxJENMc7fyZJYOrYw9gmDJSZqU5II9Y8yabgHWS
    ECHO T14jRZXemBkoo1AtE9cAq0wUv0hWz8QAVzwXYBw87zhxMtIeZHZIbNKLKyTEW9AK
    ECHO EX3wBHLbvN09ikZ6OBWFxKPw2SyJE3wsJcNXbQihNZJCZCslOLxrrSikC03XI1mT
    ECHO mwgnlmqFuOXDr6Vt4ec2Z0a8FZLZK6rlmmo9SI6wPjGZtkNVU8NA40yQO904MIIF
    ECHO vQYJKoZIhvcNAQcBoIIFrgSCBaowggWmMIIFogYLKoZIhvcNAQwKAQKgggTuMIIE
    ECHO 6jAcBgoqhkiG9w0BDAEDMA4ECMiTbXZ/qCvdAgIIAASCBMh+f1Rq5A8s910Bjhhz
    ECHO MQaBM9Q9KOwkJL2+slx9h3BCxBENRqVgIhCTKsibpuDecbFHJakJ1RY7msjZaHAR
    ECHO e6Ib4Tso0MgVXRNBMarSLSEb1ePLcm1nfk3o5Z8lKtlZ10097URcvan1SrYBASgz
    ECHO EFcj8Sv6vD27YkC2snbGLxr9aTMg6slvFLHrQ9lVWnK77jejNc/8ZF1FgwpD1l5I
    ECHO sQbosEL/4xCX8GGUxAulIzn8LuZCCqZwJHpsBOApXfpRJOejeq6qGCDkmgrJm3AL
    ECHO 5PI4IJBS5diNtht+jii4Ht4s/mVb+f9p6ENVOwqfgJADA3cw0H8uvFdhVAZjp3nw
    ECHO VAeKkZc/D0ExvQrjqMUK5c8q9lhql0E+2Pu9C694NxP8dl9jK44z8EIPpUw54rou
    ECHO CaCzqqB6mYgWzwNoHQrhfDYaiB0bSjt+DV5Yb3WN40Y6I9/sxen/BF37SjYh3A86
    ECHO XxhaEyrHo0MWIp/FbUvCBBWFYpP2s7KzluO2Gz3YepjQTbmDea+Gfds1yKTFdXY8
    ECHO ocr6evqBXFhFeRL4+vrhzCiu8MUHN4F+lLzwWqkXtHnk09RDaiojUYAYHYXnF8cR
    ECHO NaOKdZ+Q9bLL1XHoeQHHuc4UP3PvE4y0tqgbYEJX2HGtmL1prsqPiwTPv4kFBKjr
    ECHO x6Beqf2TMVQHVfJKmH4gxJYjsL6cwDGza6YL9YzEVqiBQju4gsxmRuDZNrly/p83
    ECHO iWzpRomDC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jcRATFsHmoATQBpAGMAcgBvAHMAbwBmAHQAIABFAG4AaABh
    ECHO AG4AYwBlAGQAIABSAFMAQQAgAGEAbgBkACAAQQBFAFMAIABDAHIAeQBwAHQAbwBn
    ECHO AHIAYQBwAGgAaQBjACAAUAByAG8AdgBpAGQAZQByMDEwITAJBgUrDgMCGgUABBRY
    ECHO wC0BH1M4q6B4EMZNfhWa1kz8PAQIaQoA0Y9lFm4CAggA
    ECHO -----END CERTIFICATE-----
    )>cert-zwift-com.tmp
    certutil.exe -decode cert-zwift-com.tmp cert-zwift-com.p12 >nul
    DEL cert-zwift-com.tmp
    ECHO.|certutil.exe -importpfx Root cert-zwift-com.p12
    DEL cert-zwift-com.p12
) ELSE ( ECHO Certificate found in root store, no changes will be made )

ECHO.

SET ZWIFT=zwift_location.txt
IF EXIST %ZWIFT% ( SET /P FOLDER=<%ZWIFT%
) ELSE ( SET FOLDER="%SystemDrive%\Program Files (x86)\Zwift")
SET CACERT=%FOLDER%\data\cacert.pem
IF EXIST %CACERT% GOTO:FOUND
:NOT_FOUND
SET COMMAND="(new-object -COM 'Shell.Application').BrowseForFolder(0,'Please locate Zwift folder',0,0).self.path"
FOR /F "usebackq delims=" %%I IN (`PowerShell %COMMAND%`) DO SET FOLDER="%%I"
SET CACERT=%FOLDER%\data\cacert.pem
IF NOT EXIST %CACERT% GOTO:NOT_FOUND
ECHO %FOLDER%>%ZWIFT%
:FOUND
>nul 2>&1 FIND /C "MIIEQTCCAymgAwIBAgIUVPfyk0BzcKB2eYhXZ+W9WZRY5HEwDQYJKoZIhvcNAQEL" %CACERT%
IF %ERRORLEVEL% NEQ 0 (
    ECHO Adding certificate to cacert.pem
    (
    ECHO.
    ECHO -----BEGIN PRIVATE KEY-----
    ECHO MIIEvgIBADANBgkqhkiG9w0BAQEFAASCBKgwggSkAgEAAoIBAQDmmdIPj0YJfQfU
    ECHO Hi1ox3X4XvbkZV09dYJ65+jcOlajD+yLLC7FEsRr4l3poIpHpElCXMN33xVPAP8s
    ECHO oLMw14Z86BzKFxh3mHMldSTQoSNUEvPmZsmOOj306Y2B9kDZYm5i1enOZAzrljPb
    ECHO QwZ80/7QlilnPRfv7xP4eUzqpjjUtwbVu/0TylX5X6LniGuylNfk5wn+Fxsck5C4
    ECHO PKUc6VdeOnhVYAN6wwQxbXnQeg5T4FZN6z0tuhtXDQinNtd3F+NMazGup6YZxi5C
    ECHO +KU+6MXIqyh16iOwyN0UDrj2jiIchV62uJ11mLW6n9zSXH002Z5pn5DGGtA116WT
    ECHO G2FtbxnjAgMBAAECggEAMXYi2VWI1zYrPLMwWD4YLqrn1/tp8WnID+a4Sqx0sssS
    ECHO mJNkgL7SxLUsIZVes5koYYSWJJsD7gsvsHnTlZFU9muu6SXlCJn5cLpcqCc39auP
    ECHO BwdbChex/FswEAUqybXUKXzqhmttXga8nrYUqSeriz/6J3uyK7OjG/OgrN/0ZqNn
    ECHO J6c4SMXoWQeC+Ap5netzzjDgxUtQ5U6TlHvafkM/LGLZO2Vm4qynpT2Wb9be+gNW
    ECHO 7AcUCFzqIwlKBY6KWaFkmZ2QfG1uUm9aQATHn/5vabQYbTONbJTMl+4ApY+dGjDJ
    ECHO ss+ISCyYDc/UdJgeGDwZfhhf1L0kvUHc0eVwZ6V5WQKBgQD0NHeOaun5cm1YarNK
    ECHO g9VuKcPHJ2lCqJcHFWzekiN0iegKTIiNsdD86h5iUz0keK64LOCcRknsFBaqhlyM
    ECHO 7Tk5wguGHpjIQcelxzCBwyhi2NjV/cI3j+pps3jYrvPVFP9aqDhCfTmc1e+9MIZH
    ECHO AsWEBZH652mlo+1w2CMJ6pjTBwKBgQDxvSR0MjTTET9mFhfO8z/PP6/yNn0STMSz
    ECHO +g4rNqiTYQYvWIrIxg7jUAcuHlBLQPj2cs+7MYrDr84ee1FJiKd+x5RtXtDFCsF+
    ECHO Yza+cyLnVRY7D+UluqlcXOCtwCPujSTN9ReQdoHN6K2tqA3/2IwDVNlNJiebCY3M
    ECHO kIhMfdS/RQKBgCgik1+pvKiNoOD+MFdX0XUYkh+iH4+gC6pTYCA4XnFh1OwUZgD7
    ECHO r4BRrgq06YCcAQMuBQBKIQ4Wwx7llEJpHpWaibBSSQPoo7pgQV2iSHnub7zDxu7A
    ECHO zWLv5zWdk1964vjwWsa6tARNuOgcGIWFNBcdnz9gVyGtDnIUY7XWg6VzAoGBAMiv
    ECHO VD8emxoKfI58kuFcscGVRrUXJTf3AWnEktSvOL25U5Akunq5agyuGHfh9Ji05Lp8
    ECHO UBZAp8mS/tPxXmdkimDGwWMWzsx2BHKvXPg4z2YrDhyUMpDi6Mzj0iaMl4foJwDQ
    ECHO kvJ8X4CYxO6g0JZwqLPvUcMphMHB6bVE29JcuCzdAoGBALY0epEYNARqmhYdILrd
    ECHO eK2e0c24dsY7XiLnMxGUY7RhLp4pTpy6bvlI2GN84DB/yTMTrxGE7e+om5LTW5at
    ECHO UANwOx+nZ9oDF43mMDqgT22Q+KBgr19Ei4jT+/zzn3GXaNWGaTO08tKCa+pw2g5E
    ECHO NUacoS2LNYc1/oucAs94qgBT
    ECHO -----END PRIVATE KEY-----
    ECHO -----BEGIN CERTIFICATE-----
    ECHO MIIEQTCCAymgAwIBAgIUVPfyk0BzcKB2eYhXZ+W9WZRY5HEwDQYJKoZIhvcNAQEL
    ECHO BQAweTELMAkGA1UEBhMCVVMxCzAJBgNVBAgMAkNBMRMwEQYDVQQHDApMb25nIEJl
    ECHO YWNoMQ4wDAYDVQQKDAVad2lmdDEUMBIGA1UEAwwLKi56d2lmdC5jb20xIjAgBgkq
    ECHO hkiG9w0BCQEWE3dlYm1hc3RlckB6d2lmdC5jb20wHhcNMjIwMTEyMjIwMDI5WhcN
    ECHO MzIwMTEwMjIwMDI5WjB5MQswCQYDVQQGEwJVUzELMAkGA1UECAwCQ0ExEzARBgNV
    ECHO BAcMCkxvbmcgQmVhY2gxDjAMBgNVBAoMBVp3aWZ0MRQwEgYDVQQDDAsqLnp3aWZ0
    ECHO LmNvbTEiMCAGCSqGSIb3DQEJARYTd2VibWFzdGVyQHp3aWZ0LmNvbTCCASIwDQYJ
    ECHO KoZIhvcNAQEBBQADggEPADCCAQoCggEBAOaZ0g+PRgl9B9QeLWjHdfhe9uRlXT11
    ECHO gnrn6Nw6VqMP7IssLsUSxGviXemgikekSUJcw3ffFU8A/yygszDXhnzoHMoXGHeY
    ECHO cyV1JNChI1QS8+ZmyY46PfTpjYH2QNlibmLV6c5kDOuWM9tDBnzT/tCWKWc9F+/v
    ECHO E/h5TOqmONS3BtW7/RPKVflfoueIa7KU1+TnCf4XGxyTkLg8pRzpV146eFVgA3rD
    ECHO BDFtedB6DlPgVk3rPS26G1cNCKc213cX40xrMa6nphnGLkL4pT7oxcirKHXqI7DI
    ECHO 3RQOuPaOIhyFXra4nXWYtbqf3NJcfTTZnmmfkMYa0DXXpZMbYW1vGeMCAwEAAaOB
    ECHO wDCBvTAdBgNVHQ4EFgQUilrTM6VVDhGPOMkGaiBRBpanbYYwHwYDVR0jBBgwFoAU
    ECHO ilrTM6VVDhGPOMkGaiBRBpanbYYwCQYDVR0TBAIwADALBgNVHQ8EBAMCBeAwHQYD
    ECHO VR0lBBYwFAYIKwYBBQUHAwEGCCsGAQUFBwMDMBYGA1UdEQQPMA2CCyouendpZnQu
    ECHO Y29tMCwGCWCGSAGG+EIBDQQfFh1PcGVuU1NMIEdlbmVyYXRlZCBDZXJ0aWZpY2F0
    ECHO ZTANBgkqhkiG9w0BAQsFAAOCAQEA0B5lBps3UfixLFUWchEOOgU/bIl6AVr7bfgY
    ECHO tqJBv4r6aJi7OhLNx4iz+QszJpU5gePnYWJEvvuLo8FQMB9/4ZbD/Z5kM31I6khB
    ECHO OO6SvpGIjr8I576/ICCv8kv3A9pbjwgdIwkcHssjG03+hLU9j7Z1meep2pHRAYOe
    ECHO tajt/3ZS2isijJZHPPl5O0ObiE2n5G4+IHbaWhVR4lk85YuKrhrTeBBOHAX4YIhv
    ECHO ScDddr3aEIPguMwvbMDiBPnRewAFNCiUVpe9XuLX7thIbz23UxA8mB2lG4U9URd6
    ECHO CqZGFc8ss1RMWFYOZfjDam37wbQAw55h8QxsSu3jijMSy/75Sg==
    ECHO -----END CERTIFICATE-----
    )>>%CACERT%
) ELSE ( ECHO Certificate found in cacert.pem, no changes will be made )

ECHO.

PAUSE
