name: Building GXDE Package

on:
  workflow_call:
    secrets:
      PASSWD:
        required: true
      USERS:
        required: true
      HOST: 
        required: true
      UPATH: 
        required: true
      UPROGRAM: 
        required: true
      UPORT: 
        required: true

    inputs:
      arch:
        type: string
        description: 'Rootfs Arch'
        required: true

      rootfs-codename:
        type: string
        description: 'Rootfs Codename'
        required: true

      rootfs-source:
        type: string
        description: 'Rootfs Source Url'
        required: true

      gxde-codename:
        type: string
        description: 'GXDE OS Codename'
        required: true

      gxde-codename-upload:
        type: string
        description: 'GXDE OS Codename'
        required: true

      gxde-is-backport:
        type: boolean
        description: 'Is build with backport'
        required: false

      gxde-cross-arch:
        type: string
        description: 'Build deb to which arch'
        required: false

      display-name:
        required: false
        type: string



env:
  REPOSITORY: https://github.com/GXDE-OS/GXDE  
  BOTTLEPATH: system-bottle

jobs:  
  build:
    if: github.repository_owner == 'GXDE-OS'
    name: ${{ inputs.display-name || 'Build Job' }}
    runs-on: ubuntu-24.04
    steps:
      # 由于默认的 Ubuntu 环境只有 17G 左右的剩余空间，所以我们需要先利用如下 action 对编译环境进行扩容
      - name: Maximize build space
        uses: easimon/maximize-build-space@master
        with:
          # 这个值是保留给系统的空间大小，之前设置太小，总会在安装依赖或者编译过程中报设备空间不足的错误而导致编译失败
          root-reserve-mb: 4096
          swap-size-mb: 512
          remove-dotnet: 'true'
          remove-docker-images: 'true'
          remove-codeql: 'true'
          remove-haskell: 'true'
          remove-android: 'true'

      - name: Clone Repository To Get Script
        run: |
          #apt update
          #apt install sudo git -y
          git clone $REPOSITORY --depth=1
          mv $(basename $REPOSITORY)/* . -v
          mv $(basename $REPOSITORY)/.* . -v | true

      - name: Configure Base System
        run: |
          if [[ ${{ inputs.gxde-is-backport }} == true ]]; then
            isBackport=backport
          fi
          export REPO_BRANCH=${{ github.ref_name }}
          bash .github/workflows/configure-building-enviroment-base-system.sh ${{ inputs.arch }} \
            ${{ inputs.rootfs-codename }} \
            $REPOSITORY \
            https://github.com/$GITHUB_REPOSITORY \
            ${{ inputs.rootfs-source }} \
            ${{ github.ref_name }} \
            $isBackport

      - name: Building ${{ GITHUB.repository }}
        run: |
          gitPath=$(basename $GITHUB_REPOSITORY)
          if [[ -f system-bottle/$gitPath/debian/control ]]; then
            env gitPath=$gitPath bash .github/workflows/run-command-in-chroot.sh \
              env GXDE_ARCH=${{ inputs.arch }} GXDE_CODENAME=${{ inputs.gxde-codename }} \
              GXDE_ARCH=${{ inputs.arch }} GXDE_CROSS_ARCH=${{ inputs.gxde-cross-arch }} GO111MODULE=off \
              dpkg-buildpackage -b
          else
            env gitPath=$gitPath bash .github/workflows/run-command-in-chroot.sh \
              env GXDE_ARCH=${{ inputs.arch }} GXDE_CODENAME=${{ inputs.gxde-codename }} \
              GXDE_ARCH=${{ inputs.arch }} GXDE_CROSS_ARCH=${{ inputs.gxde-cross-arch }} GO111MODULE=off \
              bash build-deb.sh
          fi
          cp $BOTTLEPATH/*.deb . -rv
          sudo rename 's/\.deb$/_${{ inputs.gxde-codename }}.deb/' *.deb

      - name: Remove Debug Package
        run: |
          cd $BOTTLEPATH
          echo -e "ls -1 | grep dbgsym | grep .deb | grep -v dtk | grep -v qt | grep -v dock | grep -v gxtk\nexit 0" | sudo tee remove-deb.sh
          removeDeb=$(bash remove-deb.sh)
          sudo rm -rf $removeDeb
          echo -e "ls -1 | grep dbg_ | grep .deb | grep -v dtk | grep -v qt | grep -v dock | grep -v gxtk\nexit 0" | sudo tee remove-deb.sh
          removeDeb=$(bash remove-deb.sh)
          sudo rm -rf $removeDeb

      - name: Pack tar
        run: |
          cd $BOTTLEPATH
          sudo tar -cvf ../deb-$(date +%s).tar *.deb 

      - name: Push
        env: 
          UPASSWD: ${{ secrets.PASSWD }}
          UUSERS: ${{ secrets.USERS }}
          UHOST: ${{ secrets.HOST }}
          UPATH: ${{ secrets.UPATH }}
          UPROGRAM: ${{ secrets.UPROGRAM }}
          UPORT: ${{ secrets.UPORT }}
        run: |
          sudo apt update
          sudo apt install sshpass -y
          mkdir -p ~/.ssh/
          ssh-keyscan -p $UPORT -H $UHOST >> ~/.ssh/known_hosts
          tarName=$(ls | grep .tar | head -n 1)
          sshpass -p "$UPASSWD" rsync -e "ssh -p $UPORT" $tarName $UUSERS@$UHOST:$UPATH
          sshpass -p "$UPASSWD" ssh $UUSERS@$UHOST -p $UPORT $UPROGRAM ${{ inputs.gxde-codename-upload }} $UPATH/$tarName > /dev/null

      - name: Create Release
        uses: softprops/action-gh-release@v1
        if: startsWith(github.ref, 'refs/tags/')
        env:
          GITHUB_TOKEN: ${{ secrets.RELEASE_TOCKEN_GITHUB }}
        with:
          files: |
            *.deb
          generate_release_notes: true
