# TODO
# https://github.com/Uriopass/Egregoria/blob/master/.github/workflows/release.yml
# has stuff for automating releases

name: Build
on: [push]
jobs:
  build:
    name: Build everything for ${{ matrix.os }}
    if: "contains(github.event.head_commit.message, '[rebuild]')"
    runs-on: ${{ matrix.os }}
    strategy:
      matrix:
        build: [linux, macos, windows]
        include:
          - build: linux
            os: ubuntu-18.04
          - build: macos
            os: macos-latest
          - build: windows
            os: windows-latest

    steps:
      - uses: actions/checkout@master

      - uses: hecrj/setup-rust-action@v1
        with:
          rust-version: 1.51.0

      - name: Cache build
        uses: actions/cache@v2
        with:
                path: target
                # The key could include hashFiles('Cargo.lock'), but cargo will figure out what can be reused.
                key: build-${{ matrix.os }}

      - name: Detect release
        shell: bash
        run: if [[ `git log -1 --pretty=format:"%s"` == *"[release]"* ]]; then echo 'features=--features=map_gui/release_s3' >> $GITHUB_ENV; else echo 'features=' >> $GITHUB_ENV; fi

      - name: Install dependencies
        if: matrix.os == 'ubuntu-18.04'
        run: sudo apt-get install libasound2-dev libxcb-shape0-dev libxcb-xfixes0-dev libpango1.0-dev

      - name: Build game
        working-directory: game
        run: cargo build --release ${features}
      - name: Build santa
        working-directory: santa
        run: cargo build --release ${features}
      - name: Build fifteen_min
        working-directory: fifteen_min
        run: cargo build --release ${features}
      - name: Build osm_viewer
        working-directory: osm_viewer
        run: cargo build --release ${features}
      - name: Build parking_mapper
        working-directory: parking_mapper
        run: cargo build --release ${features}

      - name: Build other tools
        run: cargo build --release --bin importer --bin one_step_import --bin geojson_to_osmosis --bin pick_geofabrik --bin clip_osm --bin import_grid2demand

      - name: Download system data
        run: cargo run --release --bin updater

      - name: Package release
        run: ./release/build.sh ${{ matrix.os }}
        shell: bash

      - name: Upload release for Linux
        if: matrix.os == 'ubuntu-18.04'
        uses: actions/upload-artifact@v2
        with:
          name: abst_linux
          path: abst_linux.zip
      - name: Upload release for Mac
        if: matrix.os == 'macos-latest'
        uses: actions/upload-artifact@v2
        with:
          name: abst_mac
          path: abst_mac.zip
      - name: Upload release for Windows
        if: matrix.os == 'windows-latest'
        uses: actions/upload-artifact@v2
        with:
          name: abst_windows
          path: abst_windows

      - name: Upload just the binary for Windows
        if: matrix.os == 'windows-latest'
        uses: actions/upload-artifact@v2
        with:
          name: abst_windows_binary
          path: target/release/game.exe

      - name: Upload just the binary for Mac
        if: matrix.os == 'macos-latest'
        uses: actions/upload-artifact@v2
        with:
          name: abst_mac_binary
          path: target/release/game