name: switch
on:
  push:
    branches:
      - master
      - feature/gh-actions-switch-cache
    tags:
      - '*'
  pull_request: {}
  workflow_dispatch:

jobs:
  build_switch:
    name: Build Switch
    runs-on: ubuntu-latest
    container:
      image: 'devkitpro/devkita64'
    steps:
      - name: Checkout code
        uses: actions/checkout@v2
        with:
          # We need 0 so we get all commits for mtime!
          fetch-depth: 0
          path: "./stk-code"
      - name: Grab assets
        run: |
          wget -q https://github.com/supertuxkart/stk-assets-mobile/releases/download/git/stk-assets-full.zip
          unzip -q stk-assets-full.zip -d stk-assets
          rm stk-assets-full.zip
      - name: Restore modified date
        run: |
          cd stk-code
          git restore-mtime .
          cd ..

      # Env setup!
      - name: Configure packaging name for git master branch
        if: ${{ github.ref == 'refs/heads/master' }}
        run: |
          echo "release_tag=git`date +%Y%m%d`" >> $GITHUB_ENV
          echo "release_name=preview" >> $GITHUB_ENV
      - name: Configure packaging name for tag
        if: ${{ startsWith(github.ref, 'refs/tags/') }}
        run: |
          echo "release_tag=`basename $GITHUB_REF`" >> $GITHUB_ENV
          echo "release_name=`basename $GITHUB_REF`" >> $GITHUB_ENV
      - name: Configure packaging name for non-releasing branch
        if: ${{ (github.ref != 'refs/heads/master' && !startsWith(github.ref, 'refs/tags/')) || github.repository_owner != 'supertuxkart' }}
        run: |
          echo "release_tag=" >> $GITHUB_ENV
          echo "release_name=" >> $GITHUB_ENV
      - name: Check for prerelease
        if: ${{ github.ref == 'refs/heads/master' || contains(github.ref, 'rc') || contains(github.ref, 'beta') }}
        run: |
          echo "release_pre=true" >> $GITHUB_ENV
      - name: Check for non-prerelease
        if: ${{ github.ref != 'refs/heads/master' && !contains(github.ref, 'rc') && !contains(github.ref, 'beta') }}
        run: |
          echo "release_pre=false" >> $GITHUB_ENV
      - name: Show packaging name
        run : |
          echo "${{ env.release_tag }}"
          echo "${{ env.release_name }}"
          echo "${{ env.release_pre }}"
      - name: List build cache restore keys
        shell : bash
        run: |
          # Look for the last 9 build caches (GitHub supports max 10 including current one)
          for number in 1 2 3 4 5 6 7 8 9
          do
            id=$((${{ github.run_number }} - number))
            echo "cache_$number=switch-${{ github.ref }}-$id" >> $GITHUB_ENV
          done
      - name: Handle build cache
        uses: actions/cache@v2
        with:
          # This is unnecessarily verbose and might break, but again ! seems broken
          # See: https://github.com/Mstrodl/stk-code/runs/2333673736?check_suite_focus=true#step:16:34
          path: |
            stk-code/cmake_build/CMakeFiles
            stk-code/cmake_build/Makefile
            stk-code/cmake_build/lib
            stk-code/cmake_build/bin/supertuxkart
            stk-code/cmake_build/*.cmake
            stk-code/cmake_build/*.txt

          # Make sure PRs can't overwrite!
          key: switch-${{ github.ref }}-${{ github.run_number }}
          restore-keys: |
            ${{ env.cache_1 }}
            ${{ env.cache_2 }}
            ${{ env.cache_3 }}
            ${{ env.cache_4 }}
            ${{ env.cache_5 }}
            ${{ env.cache_6 }}
            ${{ env.cache_7 }}
            ${{ env.cache_8 }}
            ${{ env.cache_9 }}
      - name: Run build script
        run: |
          cd stk-code/switch
          PROJECT_VERSION="${{ env.release_tag }}" ./make.sh
      - name: Create release
        uses: ncipollo/release-action@v1.8.8
        if: ${{ env.release_tag != '' }}
        with:
          token: ${{ secrets.GITHUB_TOKEN }}
          artifacts: "stk-code/cmake_build/bin/SuperTuxKart-${{ env.release_tag }}-switch.zip"
          tag: ${{ env.release_name }}
          omitBodyDuringUpdate: true
          omitNameDuringUpdate: true
          allowUpdates: true
          prerelease: ${{ env.release_pre }}
