name: Build all

on:
  workflow_dispatch:
  push:
    branches:
      - master

env:
  # Customize the CMake build type here (Release, Debug, RelWithDebInfo, etc.)
  BUILD_TYPE: Release
  # This makes it correctly use C++11 API on GCC by default.
  CONAN_V2_MODE: 1
  CONAN_REVISIONS_ENABLED: 1
  CONAN_SCM_TO_CONANDATA: 1
  CONAN_SYSREQUIRES_MODE: enabled
  CONAN_PASSWORD: ${{ secrets.CONAN_PASSWORD }}
  CONAN_LOGIN_USERNAME: ${{ secrets.CONAN_LOGIN_USERNAME }}
  RSYNC_PASSWORD: ${{ secrets.RSYNC_PASSWORD }}

jobs:
  build_unix:
    name: Build on *NIX
    runs-on: ${{ matrix.os }}
    strategy:
      matrix:
        os: [ ubuntu-18.04, ubuntu-20.04, macos-10.15 ]

    steps:
      - uses: actions/checkout@v2

      - name: Cache media
        uses: actions/cache@v2
        with:
          path: |
            ${{github.workspace}}/build/media-0.8.0
          key: dev-media

      - name: Setup environment
        shell: bash
        run: |
          if [[ "$ImageOS" == "ubuntu18" ]]; then
            echo "BUILD_SNAP=true" >> $GITHUB_ENV
          fi
          if [[ "$ImageOS" == "ubuntu18" ]]; then
            echo "BUILD_APPIMAGE=true" >> $GITHUB_ENV
          fi

      - name: Install autotools
        shell: bash
        if: runner.os == 'macOS'
        run: |
          brew install autoconf automake

      - name: Install Snapcraft
        if: env.BUILD_SNAP == 'true'
        uses: samuelmeuli/action-snapcraft@v1.2.0
        with:
          snapcraft_token: ${{ secrets.SNAPCRAFT_TOKEN }}

      - name: Install Conan
        shell: bash
        run: |
          export PATH=~/.local/bin:$PATH
          pip3 install --upgrade pip wheel setuptools
          pip3 install --upgrade conan
          conan --version
          conan user
          conan remote add worldforge https://artifactory.ogenvik.org/artifactory/api/conan/conan
          conan profile new default --detect

      - name: Create Build Environment
        run: cmake -E make_directory ${{github.workspace}}/build

      - name: Have Conan install packages
        working-directory: ${{github.workspace}}/build
        shell: bash
        run: |
          export PATH=~/.local/bin:$PATH
          conan install .. -pr default --build=missing --update
          if [[ x"$CONAN_PASSWORD" != "x" && x"$CONAN_LOGIN_USERNAME" != "x" ]]; then
            conan user -p $CONAN_PASSWORD -r worldforge $CONAN_LOGIN_USERNAME
            conan upload "*" -r worldforge -c --all
          fi

      - name: Configure CMake
        # Use a bash shell so we can use the same syntax for environment variable
        # access regardless of the host operating system
        shell: bash
        working-directory: ${{github.workspace}}/build
        # Note the current convention is to use the -S and -B options here to specify source
        # and build directories, but this is only available with CMake 3.13 and higher.
        # The CMake binaries on the Github Actions machines are (as of this writing) 3.12
        run: |
          mkdir -p ~/install/usr
          cmake $GITHUB_WORKSPACE -DCMAKE_INSTALL_PREFIX=~/install/usr -DCMAKE_BUILD_TYPE=$BUILD_TYPE

      - name: Download media
        if: runner.os != 'Windows'
        working-directory: ${{github.workspace}}/build
        shell: bash
        run: cmake --build . --config $BUILD_TYPE --target media-download

      - name: Build
        working-directory: ${{github.workspace}}/build
        shell: bash
        # Execute the build.  You can specify a specific target with "--target <NAME>"
        run: cmake --build . --config $BUILD_TYPE

      - name: Test
        working-directory: ${{github.workspace}}/build
        shell: bash
        # Execute tests defined by the CMake configuration.
        # See https://cmake.org/cmake/help/latest/manual/ctest.1.html for more detail
        run: ctest -C $BUILD_TYPE

      - name: Install
        working-directory: ${{github.workspace}}/build
        shell: bash
        run: cmake --build . --config $BUILD_TYPE --target install

      - name: Build Snap
        if: env.BUILD_SNAP == 'true'
        working-directory: ${{github.workspace}}/build
        shell: bash
        run: |
          echo "Building Snap"
          cp snap/snapcraft.yaml ~/install/usr/
          cd ~/install/usr/
          snapcraft --destructive-mode
          echo "Uploading Snap"
          snapcraft upload ~/install/usr/*.snap --release edge

      - name: Store Snap
        if: env.BUILD_SNAP == 'true'
        uses: actions/upload-artifact@v2
        with:
          name: snap
          path: ~/install/usr/*.snap

      - name: Build AppImage
        if: env.BUILD_APPIMAGE == 'true'
        working-directory: ${{github.workspace}}/build
        run: |
          echo "Building AppImage"
          ln -s ./usr/share/icons/hicolor/64x64/apps/ember.png ~/install/
          ln -s ./usr/share/applications/org.worldforge.ember.desktop ~/install/
          ln -s ember.png ~/install/.DirIcon
          curl -L https://github.com/AppImage/AppImageKit/releases/download/12/AppRun-x86_64 > ~/install/AppRun
          chmod a+x ~/install/AppRun
          curl -L https://github.com/AppImage/AppImageKit/releases/download/12/appimagetool-x86_64.AppImage > appimagetool-x86_64.AppImage
          chmod a+x appimagetool-x86_64.AppImage
          ./appimagetool-x86_64.AppImage -n ~/install

      - name: Store AppImage
        if: env.BUILD_APPIMAGE == 'true'
        uses: actions/upload-artifact@v2
        with:
          name: AppImage
          path: ${{github.workspace}}/build/Ember-x86_64.AppImage

      - name: Upload AppImage to amber
        if: env.BUILD_APPIMAGE == 'true'
        working-directory: ${{github.workspace}}/build
        run: rsync -uavzcP --no-p ${{github.workspace}}/build/Ember-x86_64.AppImage ${{ secrets.RSYNC_USER }}@amber.worldforge.org::ember-unstable/


  build_windows:
    name: Build on Windows
    runs-on: ${{ matrix.os }}
    strategy:
      matrix:
        os: [ windows-2019 ]

    steps:
      - uses: actions/checkout@v2

      - name: Cache media
        uses: actions/cache@v2
        with:
          path: |
            ${{github.workspace}}/build/media-0.8.0
          key: dev-media

      - name: Install Conan
        id: conan
        uses: turtlebrowser/get-conan@main

      - name: Set up Cygwin
        uses: egor-tensin/setup-cygwin@v3
        with:
          platform: x64
          packages: rsync

      - name: Create Build Environment
        shell: cmd
        run: |
          rsync --version
          conan --version
          conan user
          conan remote add worldforge https://artifactory.ogenvik.org/artifactory/api/conan/conan
          conan profile new default --detect
          cmake -E make_directory ${{github.workspace}}/build
          conan user -p %CONAN_PASSWORD% -r worldforge %CONAN_LOGIN_USERNAME%

      - name: Have Conan install packages
        working-directory: ${{github.workspace}}/build
        shell: cmd
        run: |
          conan install .. -pr default --build=missing --update
          conan upload "*" -r worldforge -c --all

      - name: Configure CMake
        shell: cmd
        working-directory: ${{github.workspace}}/build
        # Note the current convention is to use the -S and -B options here to specify source
        # and build directories, but this is only available with CMake 3.13 and higher.
        # The CMake binaries on the Github Actions machines are (as of this writing) 3.12
        run: |
          cmake %GITHUB_WORKSPACE% -DCMAKE_INSTALL_PREFIX=%USERPROFILE%\install -DVERSION_PACKAGE=latest -DCMAKE_BUILD_TYPE=%BUILD_TYPE% -DWF_USE_WIDGET_PLUGINS=OFF

      - name: Download media
        working-directory: ${{github.workspace}}/build
        shell: cmd
        run: cmake --build . --config %BUILD_TYPE% --target media-download

      - name: Build
        working-directory: ${{github.workspace}}/build
        shell: cmd
        run: cmake --build . --config %BUILD_TYPE% --parallel -- /m

      - name: Test
        working-directory: ${{github.workspace}}/build
        shell: cmd
        run: ctest -C %BUILD_TYPE%

      - name: Install
        working-directory: ${{github.workspace}}/build
        shell: cmd
        run: cmake --build . --config %BUILD_TYPE% --target install

      - name: Build NSIS
        working-directory: ${{github.workspace}}/build
        shell: cmd
        run: makensis Ember.nsi

      - name: Store NSIS
        uses: actions/upload-artifact@v2
        with:
          name: NSIS
          path: ${{github.workspace}}/build/*.exe

      - name: Upload exe to amber
        working-directory: ${{github.workspace}}/build
        shell: cmd
        run: rsync -uavzcP --no-p *.exe ${{ secrets.RSYNC_USER }}@amber.worldforge.org::ember-unstable/
