name: Build

on:
  push:
    branches: ci*
    tags: '*'

jobs:
  create_release:
    name: Create release
    runs-on: ubuntu-latest
    outputs:
      upload_url: ${{ steps.create_release.outputs.upload_url }}
    steps:
      - uses: actions/checkout@v2
        with:
          fetch-depth: 0
          submodules: false
      - name: Generate release name
        id: release_name
        # https://github.community/t/how-to-get-just-the-tag-name/16241/4
        run: echo ::set-output name=name::${GITHUB_REF/refs\/*s\//}
      - name: Generate changes file
        uses: sarnold/gitchangelog-action@master
        with:
          config_file: .gitchangelog.rc
          github_token: ${{ secrets.GITHUB_TOKEN }}
      - name: Create release
        id: create_release
        uses: actions/create-release@v1
        env:
          GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
        with:
          tag_name: ${{ steps.release_name.outputs.name }}-tag
          release_name: ${{ steps.release_name.outputs.name }}
          body_path: CHANGES.md
          draft: false
          prerelease: true

  build-linux:
    runs-on: ubuntu-20.04
    needs: [create_release]
    steps:
      - uses: actions/checkout@v2
        with:
          submodules: true
      - name: Workaround for apt update failure
        run: sudo rm /etc/apt/sources.list.d/github_git-lfs.*
      - name: Install deps
        run: sudo apt update && sudo apt install build-essential bison flex libncurses5-dev gcc-arm-linux-gnueabi qemu-user-static debootstrap python3-pip
      - name: Upgrade pip and setuptools
        run: pip3 install -U pip setuptools
      - name: Install listconfig
        run: pip3 install listconfig
      - name: Configure for Linux
        run: make ldefconfig
      - name: Build Linux
        run: make lbuild
      - name: Setup releases
        id: release_name
        run: |
          mkdir release
          cp ./linux-brain/arch/arm/boot/dts/imx28-pwsh*.dtb release/
          cp ./linux-brain/arch/arm/boot/zImage release/zImage
          zip -r release.zip release/
      - name: Generate archive name
        id: archive_name
        run: echo ::set-output name=name::linux-${GITHUB_REF/refs\/*s\//}
      - name: Upload release.zip
        uses: actions/upload-release-asset@v1
        env:
          GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
        with:
          upload_url: ${{ needs.create_release.outputs.upload_url }}
          asset_path: release.zip
          asset_name: ${{ steps.archive_name.outputs.name }}.zip
          asset_content_type: application/zip
      - name: Generate listconfig
        run: listconfig ./linux-brain/Kconfig ./linux-brain/.config > listconfig
      - name: Upload listconfig
        uses: actions/upload-release-asset@v1
        env:
          GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
        with:
          upload_url: ${{ needs.create_release.outputs.upload_url }}
          asset_path: listconfig
          asset_name: listconfig
          asset_content_type: text/plain

  build-linux-x1:
    runs-on: ubuntu-20.04
    needs: [create_release]
    steps:
      - uses: actions/checkout@v2
        with:
          submodules: true
      - name: Workaround for apt update failure
        run: sudo rm /etc/apt/sources.list.d/github_git-lfs.*
      - name: Install deps
        run: sudo apt update && sudo apt install build-essential bison flex libncurses5-dev gcc-arm-linux-gnueabihf libssl-dev lzop qemu-user-static debootstrap
      - name: Configure for Linux
        run: make ldefconfig-x1
      - name: Build Linux
        run: make lbuild
      - name: Setup releases
        id: release_name
        run: |
          mkdir release
          cp ./linux-brain/arch/arm/boot/dts/imx7ulp-pwh*.dtb release/
          cp ./linux-brain/arch/arm/boot/zImage release/zImage
          zip -r release.zip release/
      - name: Generate archive name
        id: archive_name
        run: echo ::set-output name=name::linux-x1-${GITHUB_REF/refs\/*s\//}
      - name: Upload release.zip
        uses: actions/upload-release-asset@v1
        env:
          GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
        with:
          upload_url: ${{ needs.create_release.outputs.upload_url }}
          asset_path: release.zip
          asset_name: ${{ steps.archive_name.outputs.name }}.zip
          asset_content_type: application/zip

  build-uboot:
    runs-on: ubuntu-20.04
    needs: [create_release]
    strategy:
      matrix:
        include:
          - model: a7200
            nk: edna3exe.bin
            lilo: gen2.bin
          - model: sh1
            nk: edsa1exe.bin
            lilo: gen3_1.bin
          - model: sh2
            nk: edsa2exe.bin
            lilo: gen3_2.bin
          - model: sh3
            nk: edsa3exe.bin
            lilo: gen3_3.bin
          - model: sh4
            nk: edsh4exe.bin
            lilo: gen3_4.bin
          - model: sh5
            nk: edsh5exe.bin
            lilo: gen3_5.bin
          - model: sh6
            nk: edsh6exe.bin
            lilo: gen3_6.bin
          - model: sh7
            nk: edsh7exe.bin
            lilo: gen3_7.bin

    steps:
      - uses: actions/checkout@v2
        with:
          submodules: true
      - name: Workaround for apt update failure
        run: sudo rm /etc/apt/sources.list.d/github_git-lfs.*
      - name: Install deps
        run: sudo apt update && sudo apt install build-essential bison flex libncurses5-dev gcc-arm-linux-gnueabi qemu-user-static debootstrap
      - name: Build nkbin-maker
        run: make nkbin-maker
      - name: Configure for U-Boot
        run: make udefconfig-${{ matrix.model }}
      - name: Build U-Boot
        run: make ubuild
      - name: Generate NK.bin
        run: make nk.bin
      - name: Setup releases
        id: release_name
        run: |
          mkdir release
          cp ./u-boot-brain/u-boot.bin release/u-boot.bin
          cp ./u-boot-brain/u-boot.bin release/${{ matrix.lilo }}
          cp ./u-boot-brain/u-boot.sb release/u-boot.sb
          cp ./nk.bin release/${{ matrix.nk }}
          zip -r release.zip release/
      - name: Generate archive name
        id: archive_name
        run: echo ::set-output name=name::uboot-${{ matrix.model }}-${GITHUB_REF/refs\/*s\//}
      - name: Upload release.zip
        uses: actions/upload-release-asset@v1
        env:
          GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
        with:
          upload_url: ${{ needs.create_release.outputs.upload_url }}
          asset_path: release.zip
          asset_name: ${{ steps.archive_name.outputs.name }}.zip
          asset_content_type: application/zip

  build-uboot-x1:
    runs-on: ubuntu-20.04
    needs: [create_release]
    strategy:
      matrix:
        include:
          - model: h1

    steps:
      - uses: actions/checkout@v2
        with:
          submodules: true
      - name: Workaround for apt update failure
        run: sudo rm /etc/apt/sources.list.d/github_git-lfs.*
      - name: Install deps
        run: sudo apt update && sudo apt install build-essential bison flex libncurses5-dev gcc-arm-linux-gnueabihf libssl-dev lzop qemu-user-static debootstrap
      - name: Configure for U-Boot
        run: make udefconfig-${{ matrix.model }}
      - name: Build U-Boot
        run: make ubuild
      - name: Setup releases
        id: release_name
        run: |
          mkdir release
          cp ./u-boot-brain/u-boot.bin release/u-boot.bin
          zip -r release.zip release/
      - name: Generate archive name
        id: archive_name
        run: echo ::set-output name=name::uboot-${{ matrix.model }}-${GITHUB_REF/refs\/*s\//}
      - name: Upload release.zip
        uses: actions/upload-release-asset@v1
        env:
          GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
        with:
          upload_url: ${{ needs.create_release.outputs.upload_url }}
          asset_path: release.zip
          asset_name: ${{ steps.archive_name.outputs.name }}.zip
          asset_content_type: application/zip

  build-sd:
    runs-on: ubuntu-20.04
    needs: [create_release]
    steps:
      - uses: actions/checkout@v2
        with:
          submodules: true
      - name: Make /opt writable
        run: sudo chown "$(whoami):$(whoami)" /opt
      - name: Install cegcc
        run: |
          wget -O cegcc.zip https://github.com/brain-hackers/cegcc-build/releases/download/2022-04-11-133546/cegcc-2022-04-11-133546.zip
          unzip -q cegcc.zip
          cp -r cegcc /opt/
      - name: Workaround for apt update failure
        run: sudo rm /etc/apt/sources.list.d/github_git-lfs.*
      - name: Install deps
        run: sudo apt update && sudo apt install kpartx build-essential bison flex libncurses5-dev gcc-arm-linux-gnueabi qemu-user-static debootstrap
      - name: Configure for Linux
        run: make ldefconfig
      - name: Build Linux
        run: make lbuild
      - name: Build Debian Root
        run: make brainux
      - name: Build bsd-ce
        run: make -C nkbin_maker bsd-ce
      - name: Generate image name
        id: image_name
        run: echo ::set-output name=name::sdimage-${GITHUB_REF/refs\/*s\//}
      - name: Build SD image
        run: make image/sd.img && mv image/sd.img ${{ steps.image_name.outputs.name }}.img
      - name: Compress
        run: zip ${{ steps.image_name.outputs.name }}.zip ${{ steps.image_name.outputs.name }}.img
      - name: Upload the image
        uses: actions/upload-release-asset@v1
        env:
          GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
        with:
          upload_url: ${{ needs.create_release.outputs.upload_url }}
          asset_path: ${{ steps.image_name.outputs.name }}.zip
          asset_name: ${{ steps.image_name.outputs.name }}.zip
          asset_content_type: application/zip

  build-sd-x1:
    runs-on: ubuntu-20.04
    needs: [create_release]
    steps:
      - uses: actions/checkout@v2
        with:
          submodules: true
      - name: Workaround for apt update failure
        run: sudo rm /etc/apt/sources.list.d/github_git-lfs.*
      - name: Install deps
        run: sudo apt update && sudo apt install kpartx build-essential bison flex libncurses5-dev gcc-arm-linux-gnueabihf libssl-dev lzop qemu-user-static debootstrap
      - name: Upgrade pip and setuptools
        run: pip3 install -U pip setuptools
      - name: Install pyelftools
        run: pip3 install pyelftools
      - name: Build Boot4u
        run: make boot4ubuild
      - name: Configure for U-Boot
        run: make udefconfig-h1
      - name: Build U-Boot
        run: make ubuild
      - name: Configure for Linux
        run: make ldefconfig-x1
      - name: Build Linux
        run: make lbuild
      - name: Build Debian Root
        run: make brainux
      - name: Generate image name
        id: image_name
        run: echo ::set-output name=name::sdimage-x1-${GITHUB_REF/refs\/*s\//}
      - name: Build SD image
        run: make image/sd_x1.img && mv image/sd_x1.img ${{ steps.image_name.outputs.name }}.img
      - name: Compress
        run: zip ${{ steps.image_name.outputs.name }}.zip ${{ steps.image_name.outputs.name }}.img
      - name: Upload the image
        uses: actions/upload-release-asset@v1
        env:
          GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
        with:
          upload_url: ${{ needs.create_release.outputs.upload_url }}
          asset_path: ${{ steps.image_name.outputs.name }}.zip
          asset_name: ${{ steps.image_name.outputs.name }}.zip
          asset_content_type: application/zip
