name: Build Configs

on:
  release:
    types: [created]

  workflow_dispatch:

jobs:
  Build-Default:
    name: Build Default Files
    runs-on: ubuntu-latest
    strategy:
      matrix:
        board: [V4]
    steps:
    - uses: actions/checkout@v2
    - name: Cache pip
      uses: actions/cache@v2
      with:
        path: ~/.cache/pip
        key: ${{ runner.os }}-pip-${{ hashFiles('**/requirements.txt') }}
        restore-keys: |
          ${{ runner.os }}-pip-
    - name: Cache PlatformIO
      uses: actions/cache@v2
      with:
        path: ~/.platformio
        key: ${{ runner.os }}-${{ hashFiles('**/lockfiles') }}
    - name: Set up Python
      uses: actions/setup-python@v2
    - name: Install PlatformIO
      run: |
        python -m pip install --upgrade pip
        pip install --upgrade platformio
    - name: Run PlatformIO BL-Touch
      id: build_configs
      run: |
        echo "::set-output name=filename::Default-NoProbe.bin"
        cp Configuration\ Files/Aquila\ Templates/Default-NoProbe/Configuration.h Marlin/Configuration.h
        cp Configuration\ Files/Aquila\ Templates/Default-NoProbe/Configuration_adv.h Marlin/Configuration_adv.h
        sed -i "s/#define MOTHERBOARD.*/#define MOTHERBOARD BOARD_CREALITY_$BOARD/g" Marlin/Configuration.h
        pio run
        mkdir temp
        mv .pio/build/STM32F103RET6_creality/*.bin temp/build.bin
      env:
        BOARD: ${{ matrix.board }}
    - name: Get release
      id: get_release
      uses: bruceadams/get-release@v1.2.2
      env:
        GITHUB_TOKEN: ${{ github.token }}
    - name: Upload Release Asset
      uses: actions/upload-release-asset@v1
      env:
        GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
      with:
        upload_url: ${{ steps.get_release.outputs.upload_url }}
        asset_path: temp/build.bin
        asset_name: ${{ steps.build_configs.outputs.filename }}
        asset_content_type: application/x-binary
  Build-BLTouch:
    name: Build BL-Touch Files
    runs-on: ubuntu-latest
    strategy:
      matrix:
        board: [V4]
        grid: [3,4,5,9]
        highspeed: [true,false]
    steps:
    - uses: actions/checkout@v2
    - name: Cache pip
      uses: actions/cache@v2
      with:
        path: ~/.cache/pip
        key: ${{ runner.os }}-pip-${{ hashFiles('**/requirements.txt') }}
        restore-keys: |
          ${{ runner.os }}-pip-
    - name: Cache PlatformIO
      uses: actions/cache@v2
      with:
        path: ~/.platformio
        key: ${{ runner.os }}-${{ hashFiles('**/lockfiles') }}
    - name: Set up Python
      uses: actions/setup-python@v2
    - name: Install PlatformIO
      run: |
        python -m pip install --upgrade pip
        pip install --upgrade platformio
    - name: Run PlatformIO BL-Touch
      id: build_configs
      run: |
        if ($HS); then HSTMP=-HS; else HSTMP=; fi;
        echo "::set-output name=filename::BLTouch-${GRID}x${GRID}${HSTMP}.bin"
        cp Configuration\ Files/Aquila\ Templates/BLTouch-3x3/Configuration.h Marlin/Configuration.h
        cp Configuration\ Files/Aquila\ Templates/BLTouch-3x3/Configuration_adv.h Marlin/Configuration_adv.h
        sed -i "s/#define MOTHERBOARD.*/#define MOTHERBOARD BOARD_CREALITY_$BOARD/g" Marlin/Configuration.h
        sed -i "s/#define GRID_MAX_POINTS_X.*/#define GRID_MAX_POINTS_X $GRID/g" Marlin/Configuration.h
        if ($HS); then TEMP=; else TEMP='\/\/'; fi;
        sed -i "s/[^ ]*#define BLTOUCH_HS_MODE/$TEMP#define BLTOUCH_HS_MODE/g" Marlin/Configuration_adv.h
        pio run
        mkdir temp
        mv .pio/build/STM32F103RET6_creality/*.bin temp/build.bin
      env:
        BOARD: ${{ matrix.board }}
        GRID: ${{ matrix.grid }}
        HS: ${{ matrix.highspeed }}
    - name: Get release
      id: get_release
      uses: bruceadams/get-release@v1.2.2
      env:
        GITHUB_TOKEN: ${{ github.token }}
    - name: Upload Release Asset
      uses: actions/upload-release-asset@v1
      env:
        GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
      with:
        upload_url: ${{ steps.get_release.outputs.upload_url }}
        asset_path: temp/build.bin
        asset_name: ${{ steps.build_configs.outputs.filename }}
        asset_content_type: application/x-binary
  Build-Manual-Mesh:
    name: Build Manual-Mesh Files
    runs-on: ubuntu-latest
    strategy:
      matrix:
        board: [V4]
        grid: [3,4,5]
    steps:
    - uses: actions/checkout@v2
    - name: Cache pip
      uses: actions/cache@v2
      with:
        path: ~/.cache/pip
        key: ${{ runner.os }}-pip-${{ hashFiles('**/requirements.txt') }}
        restore-keys: |
          ${{ runner.os }}-pip-
    - name: Cache PlatformIO
      uses: actions/cache@v2
      with:
        path: ~/.platformio
        key: ${{ runner.os }}-${{ hashFiles('**/lockfiles') }}
    - name: Set up Python
      uses: actions/setup-python@v2
    - name: Install PlatformIO
      run: |
        python -m pip install --upgrade pip
        pip install --upgrade platformio
    - name: Run PlatformIO BL-Touch
      id: build_configs
      run: |
        echo "::set-output name=filename::ManualMesh-${GRID}x${GRID}.bin"
        cp Configuration\ Files/Aquila\ Templates/ManualMesh-3x3/Configuration.h Marlin/Configuration.h
        cp Configuration\ Files/Aquila\ Templates/ManualMesh-3x3/Configuration_adv.h Marlin/Configuration_adv.h
        sed -i "s/#define MOTHERBOARD.*/#define MOTHERBOARD BOARD_CREALITY_$BOARD/g" Marlin/Configuration.h
        sed -i "s/#define GRID_MAX_POINTS_X.*/#define GRID_MAX_POINTS_X $GRID/g" Marlin/Configuration.h
        pio run
        mkdir temp
        mv .pio/build/STM32F103RET6_creality/*.bin temp/build.bin
      env:
        BOARD: ${{ matrix.board }}
        GRID: ${{ matrix.grid }}
    - name: Get release
      id: get_release
      uses: bruceadams/get-release@v1.2.2
      env:
        GITHUB_TOKEN: ${{ github.token }}
    - name: Upload Release Asset
      uses: actions/upload-release-asset@v1
      env:
        GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
      with:
        upload_url: ${{ steps.get_release.outputs.upload_url }}
        asset_path: temp/build.bin
        asset_name: ${{ steps.build_configs.outputs.filename }}
        asset_content_type: application/x-binary
  Build-UBL-BLTouch:
    name: Build BL-Touch UBL Files
    runs-on: ubuntu-latest
    strategy:
      matrix:
        board: [V4]
        grid: [10,15]
        highspeed: [true,false]
    steps:
    - uses: actions/checkout@v2
    - name: Cache pip
      uses: actions/cache@v2
      with:
        path: ~/.cache/pip
        key: ${{ runner.os }}-pip-${{ hashFiles('**/requirements.txt') }}
        restore-keys: |
          ${{ runner.os }}-pip-
    - name: Cache PlatformIO
      uses: actions/cache@v2
      with:
        path: ~/.platformio
        key: ${{ runner.os }}-${{ hashFiles('**/lockfiles') }}
    - name: Set up Python
      uses: actions/setup-python@v2
    - name: Install PlatformIO
      run: |
        python -m pip install --upgrade pip
        pip install --upgrade platformio
    - name: Run PlatformIO BL-Touch
      id: build_configs
      run: |
        if ($HS); then HSTMP=-HS; else HSTMP=; fi;
        echo "::set-output name=filename::UBL-BLTouch-${GRID}x${GRID}${HSTMP}.bin"
        cp Configuration\ Files/Aquila\ Templates/UBL-BLTouch-10x10/Configuration.h Marlin/Configuration.h
        cp Configuration\ Files/Aquila\ Templates/UBL-BLTouch-10x10/Configuration_adv.h Marlin/Configuration_adv.h
        sed -i "s/#define MOTHERBOARD.*/#define MOTHERBOARD BOARD_CREALITY_$BOARD/g" Marlin/Configuration.h
        sed -i "s/#define GRID_MAX_POINTS_X.*/#define GRID_MAX_POINTS_X $GRID/g" Marlin/Configuration.h
        if ($HS); then TEMP=; else TEMP='\/\/'; fi;
        sed -i "s/[^ ]*#define BLTOUCH_HS_MODE/$TEMP#define BLTOUCH_HS_MODE/g" Marlin/Configuration_adv.h
        pio run
        mkdir temp
        mv .pio/build/STM32F103RET6_creality/*.bin temp/build.bin
      env:
        BOARD: ${{ matrix.board }}
        GRID: ${{ matrix.grid }}
        HS: ${{ matrix.highspeed }}
    - name: Get release
      id: get_release
      uses: bruceadams/get-release@v1.2.2
      env:
        GITHUB_TOKEN: ${{ github.token }}
    - name: Upload Release Asset
      uses: actions/upload-release-asset@v1
      env:
        GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
      with:
        upload_url: ${{ steps.get_release.outputs.upload_url }}
        asset_path: temp/build.bin
        asset_name: ${{ steps.build_configs.outputs.filename }}
        asset_content_type: application/x-binary
  Build-UBL-NoProbe:
    name: Build NoProbe UBL Files
    runs-on: ubuntu-latest
    strategy:
      matrix:
        board: [V4]
        grid: [3,5]
    steps:
    - uses: actions/checkout@v2
    - name: Cache pip
      uses: actions/cache@v2
      with:
        path: ~/.cache/pip
        key: ${{ runner.os }}-pip-${{ hashFiles('**/requirements.txt') }}
        restore-keys: |
          ${{ runner.os }}-pip-
    - name: Cache PlatformIO
      uses: actions/cache@v2
      with:
        path: ~/.platformio
        key: ${{ runner.os }}-${{ hashFiles('**/lockfiles') }}
    - name: Set up Python
      uses: actions/setup-python@v2
    - name: Install PlatformIO
      run: |
        python -m pip install --upgrade pip
        pip install --upgrade platformio
    - name: Run PlatformIO BL-Touch
      id: build_configs
      run: |
        echo "::set-output name=filename::UBL-NoProbe-${GRID}x${GRID}.bin"
        cp Configuration\ Files/Aquila\ Templates/UBL-NoProbe-3x3/Configuration.h Marlin/Configuration.h
        cp Configuration\ Files/Aquila\ Templates/UBL-NoProbe-3x3/Configuration_adv.h Marlin/Configuration_adv.h
        sed -i "s/#define MOTHERBOARD.*/#define MOTHERBOARD BOARD_CREALITY_$BOARD/g" Marlin/Configuration.h
        sed -i "s/#define GRID_MAX_POINTS_X.*/#define GRID_MAX_POINTS_X $GRID/g" Marlin/Configuration.h
        pio run
        mkdir temp
        mv .pio/build/STM32F103RET6_creality/*.bin temp/build.bin
      env:
        BOARD: ${{ matrix.board }}
        GRID: ${{ matrix.grid }}
    - name: Get release
      id: get_release
      uses: bruceadams/get-release@v1.2.2
      env:
        GITHUB_TOKEN: ${{ github.token }}
    - name: Upload Release Asset
      uses: actions/upload-release-asset@v1
      env:
        GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
      with:
        upload_url: ${{ steps.get_release.outputs.upload_url }}
        asset_path: temp/build.bin
        asset_name: ${{ steps.build_configs.outputs.filename }}
        asset_content_type: application/x-binary