@echo off

set ROOT=%~dp0%
set PATH=%ROOT%bin;%PATH%
set MODE=%1
set SIA_VERSION=v1.2.2
set INNO_COMPILER=C:\Program Files (x86)\Inno Setup 5\Compil32.exe
pushd "%ROOT%"

setlocal
  call build_%MODE%_x64.cmd
endlocal

pushd 3rd_party
  if not exist "vc_redist.x64.exe" (
    wget --no-check-certificate https://download.microsoft.com/download/6/D/F/6DF3FF94-F7F9-4F0B-838C-A328D1A7D0EE/vc_redist.x64.exe || goto :ERROR
  )
  
  if not exist "%SIA_VERSION%.complete" (
    if not exist "Sia-%SIA_VERSION%-windows-amd64.zip" (
      wget --no-check-certificate https://github.com/NebulousLabs/Sia/releases/download/%SIA_VERSION%/Sia-%SIA_VERSION%-windows-amd64.zip || goto :ERROR
    )
    rd /s /q Sia > NUL
    unzip Sia-%SIA_VERSION%-windows-amd64.zip || goto :ERROR
    move Sia-%SIA_VERSION%-windows-amd64 Sia || goto :ERROR
    echo "1">%SIA_VERSION%.complete
  )
  
  if not exist "Dokan_x64.msi" (
    wget --no-check-certificate https://github.com/dokan-dev/dokany/releases/download/v1.0.3/Dokan_x64.msi || goto :ERROR
  )
popd

"%INNO_COMPILER%" /cc SiaDrive_Packager_%MODE%.iss || goto :ERROR

goto :END

:ERROR
  pause
  popd
  exit 1

:END
  popd