# This is a basic workflow to help you get started with Actions

name: Cross-Build

on:
  push:
    branches-ignore: [ master ]
  pull_request:
    branches-ignore: [ master ]

jobs:
  job1:
    name: Build Number
    runs-on: ubuntu-latest
    outputs:
      build_number: ${{ steps.buildnumber.outputs.build_number }}
    steps:
    - name: Generate common build number
      id: buildnumber
      uses: einaregilsson/build-number@v3
      with:
        token: ${{secrets.github_token}}
  build:
    runs-on: ubuntu-latest
    needs: job1
    strategy:
      max-parallel: 1
      matrix:
        node: [I2S-4MFlash, SqueezeAmp, ESP32-A1S]
        depth: [16, 32]
    steps:
    - name: Set target name
      run: |
        echo "TARGET_BUILD_NAME=${{ matrix.node }}" >> $GITHUB_ENV
        echo "build_version_prefix=V0." >> $GITHUB_ENV
    - uses: actions/checkout@v2
      with:
        fetch-depth: 15
        submodules: true
    - name: Cache build
      id: cache-build
      uses: actions/cache@v2
      with:
        path: |
          build
          /var/lib/docker
        key: ${{ runner.os }}-${{ matrix.node }}-${{ matrix.depth }}
    - name: Set build parameters
      run: |
        git update-index --chmod=+x ./server_certs/getcert.sh
        cd server_certs;./getcert.sh;cat github.pem;cd ..
        shopt -s nocasematch
        branch_name="${GITHUB_REF//refs\/heads\//}"
        branch_name="${branch_name//[^a-zA-Z0-9\-~!@_\.]/}"
        BUILD_NUMBER=${{ needs.job1.outputs.build_number }}
        echo "BUILD_NUMBER=${BUILD_NUMBER}" >> $GITHUB_ENV
        tag="${TARGET_BUILD_NAME}.${{matrix.depth}}.${BUILD_NUMBER}.${branch_name}"
        echo "tag=${tag}" >> $GITHUB_ENV
        last_commit="$(git log --pretty=format:'%s' --max-count=1)"
        if [[ "$last_commit" =~ .*"Release".* ]]; then echo "release_flag=1" >> $GITHUB_ENV; else echo "release_flag=0" >> $GITHUB_ENV; fi
        name="dev.${BUILD_NUMBER}-${{matrix.depth}}#v4.0#${TARGET_BUILD_NAME}#${branch_name}"
        artifact_prefix="squeezelite-esp32-${branch_name}-${TARGET_BUILD_NAME}-${{matrix.depth}}-${build_version_prefix}${BUILD_NUMBER}"
        artifact_file_name="${artifact_prefix}.zip"
        artifact_bin_file_name="${artifact_prefix}.bin"
        echo "name=${name}" >> $GITHUB_ENV
        echo "last_commit=${last_commit}" >> $GITHUB_ENV
        echo "artifact_file_name=${artifact_file_name}" >> $GITHUB_ENV
        echo "PROJECT_VER=${TARGET_BUILD_NAME}-${{ steps.buildnumber.outputs.build_number }} " >> $GITHUB_ENV
        echo "artifact_bin_file_name=${artifact_bin_file_name}" >> $GITHUB_ENV
        description=""
        description=${description}$'------------------------------\n### Revision Log\n\n'
        description="$description$(git log --pretty=format:'%h %s (%cI) <%an>' --abbrev-commit --max-count=15 | sed --r 's/(^[\*]+)/\\\1/g') "        
        echo 'description<<~EOD' >> $GITHUB_ENV
        echo ${description}>> $GITHUB_ENV
        echo '~EOD' >> $GITHUB_ENV        
        echo ####### 
        echo ####### Environment
        echo ####### 
        env
        echo ####### 
        echo ####### GITHUB ENV
        echo ####### 
        cat $GITHUB_ENV
    - name: Build the firmware
      run: |
        env | grep "artifact\|tag\|GITHUB\|version\|NUMBER\|TARGET" >${TARGET_BUILD_NAME}-env.txt
        echo "${tag}" >version.txt
        docker pull sle118/idf:release-v4.0 
        docker info 
        docker run --env-file=${TARGET_BUILD_NAME}-env.txt  -v $PWD:/project -w /project sle118/idf:release-v4.0 /bin/bash -c "cp build-scripts/${TARGET_BUILD_NAME}-sdkconfig.defaults sdkconfig  && idf.py build -DDEPTH=${{ matrix.depth }} -DBUILD_NUMBER=${BUILD_NUMBER}-${{ matrix.depth }} && zip -r build_output.zip build && zip build/${artifact_file_name} partitions*.csv build/*.bin build/bootloader/bootloader.bin build/partition_table/partition-table.bin build/flash_project_args build/size_*.txt"
    # - name: Build Mock firmware
    #   run: |
    #     mkdir -p build
    #     cd build
    #     mkdir -p partition_table
    #     mkdir -p bootloader
    #     echo "mock content"> squeezelite.bin
    #     echo "mock content"> recovery.bin
    #     echo "mock content"> ./bootloader/bootloader.bin
    #     echo "mock content"> ./partition_table/partition-table.bin 
    #     echo "mock content"> flash_project_args 
    #     echo "mock content"> size_comp1.txt
    #     echo "mock content"> size_comp2.txt
    #     echo "mock content"> ../partitions.csv
    - uses: actions/upload-artifact@v2
      with:
        name: ${{ env.artifact_file_name }}
        path: |
          build/*.bin
          build/bootloader/bootloader.bin
          build/partition_table/partition-table.bin 
          build/flash_project_args 
          build/size_comp1.txt
          build/size_comp2.txt
          partitions.csv
          sdkconfig
          server_certs/github.pem
          build_output.zip
    - uses: actions/upload-artifact@v2
      with:
        name: ${{ env.artifact_bin_file_name }}
        path: |
          build/squeezelite.bin
    - name: Create Release
      if: env.release_flag == 1     
      id: create_release
      uses: actions/create-release@v1
      env:
        GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }} # This token is provided by Actions, you do not need to create your own token
      with:
        tag_name: ${{ env.tag }}
        release_name: ${{ env.name }}
        body: ${{ env.description }}
        draft: false
        prerelease: true
    - name: Upload Release Asset - Squeezelite binary file
      if: env.release_flag == 1         
      id: upload-release-asset 
      uses: actions/upload-release-asset@v1
      env:
        GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
      with:
        upload_url: ${{ steps.create_release.outputs.upload_url }} # This pulls from the CREATE RELEASE step above, referencing it's ID to get its outputs object, which include a `upload_url`. See this blog post for more info: https://jasonet.co/posts/new-features-of-github-actions/#passing-data-to-future-steps 
        asset_path: build/squeezelite.bin
        asset_name: ${{ env.artifact_bin_file_name }}
        asset_content_type: application/octet-stream        
    - name: Upload Release Asset - Zip file
      if: env.release_flag == 1         
      id: upload-release-asset-zip
      uses: actions/upload-release-asset@v1
      env:
        GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
      with:
        upload_url: ${{ steps.create_release.outputs.upload_url }} # This pulls from the CREATE RELEASE step above, referencing it's ID to get its outputs object, which include a `upload_url`. See this blog post for more info: https://jasonet.co/posts/new-features-of-github-actions/#passing-data-to-future-steps 
        asset_path: build/${{ env.artifact_file_name }}
        asset_name: ${{ env.artifact_file_name }}
        asset_content_type: application/octet-stream