version: '{build}'

# In the end, all the efforts to make the date of the release progress didn't work.
# Just make the tag progress by deleting/recreating it but do not rebuild it.
branches:
 except:
  - latest

environment:
  access_token:
    secure: piEKiVbXBFm0fcZCREEII0pxZth77lewlfMc7qApggKVDXswkPaFJKM9eyGAaUy3
  MSYS2_BASEVER: 20210604
  MSYS2_ARCH: x86_64
  MSYS2_URL: "http://repo.msys2.org/distrib/%MSYS2_ARCH%/msys2-base-%MSYS2_ARCH%-%MSYS2_BASEVER%.tar.xz"
  MSYS2_ARCHIVE: msys2.tar.xz
  MSYS2_SUBARCHIVE: msys2.tar
  IPF_LINUX_URL: "http://www.softpres.org/_media/files:ipflib42_linux-i686.tar.gz"
  PACKAGES: "ctags make zip diffutils %ARCH_PACKAGES%"
  matrix:
    - MINGW_DIR: "/mingw64"
      WINARCH: win64
      ARCH_PACKAGES: "mingw-w64-x86_64-ctags mingw-w64-x86_64-make mingw-w64-x86_64-gcc mingw-w64-x86_64-libpng mingw-w64-x86_64-freetype mingw-w64-x86_64-zlib mingw-w64-x86_64-SDL2"
      IPF_WIN_URL: "http://www.softpres.org/_media/files:ipflib42_win_x64.zip"
      IPF_DLL_SRC: "CAPSImg_x64.dll"
      IPF_LICENCE_SRC: "LICENSE.txt"
      RUN_E2E: true
    - MINGW_DIR: "/mingw32"
      WINARCH: win32
      ARCH_PACKAGES: "mingw-w64-i686-ctags mingw-w64-i686-make mingw-w64-i686-gcc mingw-w64-i686-libpng mingw-w64-i686-freetype mingw-w64-i686-zlib mingw-w64-i686-SDL2"
      IPF_WIN_URL: "http://www.softpres.org/_media/files:ipflib42_w32.zip"
      IPF_DLL_SRC: "ipflib42_w32/CAPSImg.dll"
      IPF_LICENCE_SRC: "ipflib42_w32/LICENSE.txt"
      # I'm not sure why, e2e tests do not work on win32 (binary doesn't seem to work), only on win64 ...
      RUN_E2E: false

install:
- if not exist "%MSYS2_ARCHIVE%" appveyor DownloadFile "%MSYS2_URL%" -FileName "%MSYS2_ARCHIVE%"
- 7z x "%MSYS2_ARCHIVE%"
- 7z x "%MSYS2_SUBARCHIVE%" > NUL
  # msys2 advise to restart the shell after a first start
- msys64\usr\bin\bash -lc ""
# Allow three updates to fail (can happen with 'To complete this update all MSYS2 processes including this terminal will be closed.' killing bash itself.
- msys64\usr\bin\bash -lc "pacman --noconfirm -Suy" & exit 0
- msys64\usr\bin\bash -lc "pacman --noconfirm -Suy" & exit 0
- msys64\usr\bin\bash -lc "pacman --noconfirm -Suy" & exit 0
# The last one should succeed with nothing to update.
- msys64\usr\bin\bash -lc "pacman --noconfirm -Suy"
# Install packages. Three attempts as this can fail with network error sometims.
- msys64\usr\bin\bash -lc "for i in {1..3}; do pacman --noconfirm -S %PACKAGES% && break || sleep 15 ; done"

before_build:
- msys64\usr\bin\bash -lc "%MINGW_DIR%/bin/g++ --version"
- msys64\usr\bin\bash -lc "/usr/bin/make --version"
- echo "NUMBER_OF_PROCESSORS=%NUMBER_OF_PROCESSORS%"

build_script:
# TODO(cpitrat): Find a way to use %APPVEYOR_BUILD_FOLDER%, replacing \ by /
# ===================================
# = 1- Build and test in DEBUG mode =
# ===================================
- msys64\usr\bin\bash -lc "cd c:/projects/caprice32 && PATH=$PATH:%MINGW_DIR%/bin /usr/bin/make -j %NUMBER_OF_PROCESSORS% ARCH=%WINARCH% MINGW_PATH=\"%MINGW_DIR%\" WINE=\"\" debug"
- msys64\usr\bin\bash -lc "[ \"%RUN_E2E%\" == \"false\" ] || (cd c:/projects/caprice32 && PATH=$PATH:%MINGW_DIR%/bin /usr/bin/make -j %NUMBER_OF_PROCESSORS% ARCH=%WINARCH% MINGW_PATH=\"%MINGW_DIR%\" WINE=\"\" e2e_test)"
# ===================================
# 2- Build and test in RELEASE mode =
# ===================================
- msys64\usr\bin\bash -lc "cd c:/projects/caprice32 && PATH=$PATH:%MINGW_DIR%/bin /usr/bin/make -j %NUMBER_OF_PROCESSORS% ARCH=%WINARCh% clean"
- msys64\usr\bin\bash -lc "cd c:/projects/caprice32 && PATH=$PATH:%MINGW_DIR%/bin /usr/bin/make -j %NUMBER_OF_PROCESSORS% ARCH=%WINARCH% MINGW_PATH=\"%MINGW_DIR%\""
- msys64\usr\bin\bash -lc "cd c:/projects/caprice32 && PATH=$PATH:%MINGW_DIR%/bin /usr/bin/make -j %NUMBER_OF_PROCESSORS% ARCH=%WINARCH% MINGW_PATH=\"%MINGW_DIR%\" WINE=\"\" unit_test"
# =============================
# = 3- Build with IPF support =
# =============================
# Install IPF lib for linux (for build)
- msys64\usr\bin\bash -lc "cd c:/projects/caprice32 && wget \"%IPF_LINUX_URL%\" -O ipflib42_linux-i686.tar.gz && 7z x ipflib42_linux-i686.tar.gz && 7z x ipflib42_linux-i686.tar && cd i686-linux-gnu-capsimage/ && ln -s libcapsimage.so.4.2 libcapsimage.so.4 && ln -s libcapsimage.so.4.2 libcapsimage.so"
# Install IPF lib for windows
- msys64\usr\bin\bash -lc "wget \"%IPF_WIN_URL%\" && 7z x *ipflib*zip && cp %IPF_DLL_SRC% %MINGW_DIR%/bin/ && cp %IPF_LICENCE_SRC% c:/projects/caprice32/licenses/CAPSImg\ license.txt"
- msys64\usr\bin\bash -lc "cd c:/projects/caprice32 && PATH=$PATH:%MINGW_DIR%/bin /usr/bin/make -j %NUMBER_OF_PROCESSORS% ARCH=%WINARCh% clean"
- msys64\usr\bin\bash -lc "cd c:/projects/caprice32 && PATH=$PATH:%MINGW_DIR%/bin /usr/bin/make -j %NUMBER_OF_PROCESSORS% ARCH=%WINARCH% MINGW_PATH=\"%MINGW_DIR%\" WITH_IPF=true CFLAGS=-Ii686-linux-gnu-capsimage/include"
- msys64\usr\bin\bash -lc "cd c:/projects/caprice32 && PATH=$PATH:%MINGW_DIR%/bin /usr/bin/make -j %NUMBER_OF_PROCESSORS% ARCH=%WINARCH% MINGW_PATH=\"%MINGW_DIR%\" WINE=\"\" WITH_IPF=true CFLAGS=-Ii686-linux-gnu-capsimage/include unit_test"
- msys64\usr\bin\bash -lc "[ \"%RUN_E2E%\" == \"false\" ] || (cd c:/projects/caprice32 && PATH=$PATH:%MINGW_DIR%/bin /usr/bin/make -j %NUMBER_OF_PROCESSORS% ARCH=%WINARCH% MINGW_PATH=\"%MINGW_DIR%\" WINE=\"\" WITH_IPF=true CFLAGS=-Ii686-linux-gnu-capsimage/include e2e_test)"
- msys64\usr\bin\bash -lc "cd c:/projects/caprice32 && PATH=$PATH:%MINGW_DIR%/bin ./test/integrated/test_win_package.sh %WINARCH%"
- msys64\usr\bin\bash -lc "cd c:/projects/caprice32 && PATH=$PATH:%MINGW_DIR%/bin ./test/integrated/test_win_deps.sh %WINARCH%"

# ======
# = 4- Delete and recreate latest tag to trigger the 2nd build that will update the release date (https://github.com/appveyor/ci/issues/827)
# =    Only do it for build on master branch to avoid looping !
# ====
- ps: |
    if ($env:APPVEYOR_REPO_TAG -eq "false") {
      try {
        git config --global credential.helper store
        Set-Content -Path "$HOME\.git-credentials" -Value "https://$($env:access_token):x-oauth-basic@github.com`n" -NoNewline
        git config --global user.email "colin.pitrat@gmail.com"
        git config --global user.name "Colin Pitrat (AppVeyor)"
        git tag -d latest
        git tag latest HEAD
        git push -q -f origin latest
        if (-not $?) {
          throw "git push failed ..."
        }
      } catch {
        Write-Output "Deletion of tag latest failed, ignoring."
      }
    }

artifacts:
- path: "release\\cap32-%WINARCH%.zip"
  name: cap32-%WINARCH%.zip

deploy:
# Ugly hack for https://github.com/appveyor/ci/issues/827
# We build 2 times.
# 1st build: a pre-release on master at each commit, marking the release as prerelease and setting tag 'latest'.
- release: "Caprice32 latest"
  tag: latest
  description: "Latest build of Caprice 32, with the newest cool features and the most recent bugs."
  provider: GitHub
  auth_token:
    secure: piEKiVbXBFm0fcZCREEII0pxZth77lewlfMc7qApggKVDXswkPaFJKM9eyGAaUy3
  artifact: cap32-%WINARCH%.zip
  draft: false
  prerelease: false
  force_update: true
  on:
    branch: master

# 2nd build: a release from 'latest' tag. The only purpose of this release is to update the release date
#- release: "Caprice32 latest"
#  tag: latest
#  description: "Latest build of Caprice 32, with the newest cool features and the most recent bugs."
#  provider: GitHub
#  auth_token:
#    secure: piEKiVbXBFm0fcZCREEII0pxZth77lewlfMc7qApggKVDXswkPaFJKM9eyGAaUy3
#  artifact: cap32-%WINARCH%.zip
#  draft: false
#  prerelease: false
#  force_update: true
#  on:
#    branch: latest

# This deployment is for tagged versions (e.g: v4.4.0)
- release: "Caprice32 %APPVEYOR_REPO_TAG_NAME%"
  tag: "%APPVEYOR_REPO_TAG_NAME%"
  provider: GitHub
  auth_token:
    secure: piEKiVbXBFm0fcZCREEII0pxZth77lewlfMc7qApggKVDXswkPaFJKM9eyGAaUy3
  artifact: cap32-%WINARCH%.zip
  draft: false
  prerelease: false
  force_update: true
  on:
    appveyor_repo_tag: true
