name: Building GXDE Backport Package
run-name: Building ${{ GITHUB.REPOSITORY }}

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

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

jobs:  
  i386:
    runs-on: ubuntu-latest
    steps:
      - name: Clone Repository To Get Script
        run: |
          git clone $REPOSITORY --depth=1
          mv $(basename $REPOSITORY)/* . -v
          mv $(basename $REPOSITORY)/.* . -v | true

      - name: Configure Base System
        run: |
          bash .github/workflows/configure-building-enviroment-base-system.sh i386 bookworm $REPOSITORY https://github.com/$GITHUB_REPOSITORY https://deb.debian.org/debian/ backport

      - name: Building ${{ GITHUB.repository }}
        run: |
          env gitPath=$(basename $GITHUB_REPOSITORY) bash .github/workflows/run-command-in-chroot.sh env GXDE_CODENAME=bixie GO111MODULE=off dpkg-buildpackage -b
          cp $BOTTLEPATH/*.deb . -rv

      - 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

      - 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 tianlu-bpo $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

  amd64:
    runs-on: ubuntu-latest
    steps:
      - name: Clone Repository To Get Script
        run: |
          git clone $REPOSITORY --depth=1
          mv $(basename $REPOSITORY)/* . -v
          mv $(basename $REPOSITORY)/.* . -v | true

      - name: Configure Base System
        run: |
          bash .github/workflows/configure-building-enviroment-base-system.sh amd64 bookworm $REPOSITORY https://github.com/$GITHUB_REPOSITORY  https://deb.debian.org/debian/ backport

      - name: Building ${{ GITHUB.repository }}
        run: |
          env gitPath=$(basename $GITHUB_REPOSITORY) bash .github/workflows/run-command-in-chroot.sh env GXDE_CODENAME=bixie GO111MODULE=off dpkg-buildpackage -b
          cp $BOTTLEPATH/*.deb . -rv

      - 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

      - 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 tianlu-bpo $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

  arm64:
    runs-on: ubuntu-24.04
    #runs-on: self-hosted
    steps:
      - name: Clone Repository To Get Script
        run: |
          git clone $REPOSITORY --depth=1
          mv $(basename $REPOSITORY)/* . -v
          mv $(basename $REPOSITORY)/.* . -v | true

      - name: Configure Base System
        run: |
          bash .github/workflows/configure-building-enviroment-base-system.sh arm64 bookworm $REPOSITORY https://github.com/$GITHUB_REPOSITORY https://deb.debian.org/debian/ backport

      - name: Building ${{ GITHUB.repository }}
        run: |
          env gitPath=$(basename $GITHUB_REPOSITORY) bash .github/workflows/run-command-in-chroot.sh env GXDE_CODENAME=bixie GO111MODULE=off dpkg-buildpackage -b
          cp $BOTTLEPATH/*.deb . -rv

      - 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

      - 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 tianlu-bpo $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

  mips64:
    runs-on: ubuntu-24.04
    steps:
      - name: Clone Repository To Get Script
        run: |
          git clone $REPOSITORY --depth=1
          mv $(basename $REPOSITORY)/* . -v
          mv $(basename $REPOSITORY)/.* . -v | true

      - name: Configure Base System
        run: |
          bash .github/workflows/configure-building-enviroment-base-system.sh mips64el bookworm $REPOSITORY https://github.com/$GITHUB_REPOSITORY https://deb.debian.org/debian/ backport

      - name: Building ${{ GITHUB.repository }}
        run: |
          env gitPath=$(basename $GITHUB_REPOSITORY) bash .github/workflows/run-command-in-chroot.sh env GXDE_CODENAME=bixie GO111MODULE=off dpkg-buildpackage -b
          cp $BOTTLEPATH/*.deb . -rv

      - 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

      - 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 tianlu-bpo $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