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

name: slitaz-5.0
#on:
#  push:
#    branches: 
#      - main
#    schedule:
#      - cron: 0 20 1 * *
#    release:
#      types: [published]
on:
  push:
    # Sequence of patterns matched against refs/tags
    tags:
      - '*'
    release:
      types: [published]

jobs:

  build:

    name: 自动汉化并封装每周官方映像

    runs-on: ubuntu-20.04

    if: github.event.repository.owner.id == github.event.sender.id

    steps:

      - name: Checkout
        uses: actions/checkout@v2

      - name: Space cleanup
        env:
          DEBIAN_FRONTEND: noninteractive
        run: |
          docker rmi `docker images -q`
          sudo -E apt-get update
          sudo -E apt-get -y install zip unzip genisoimage p7zip-full p7zip-rar
          sudo -E apt-get -y autoremove --purge
          sudo -E apt-get clean

      - name: download iso and unzip iso
        run: |
          wget http://ftp.uni-stuttgart.de/slitaz/iso/rolling/slitaz-rolling-core64.iso
          mkdir iso-old
          7z x slitaz-rolling-core64.iso -y -r -o./iso-old
          wget http://ecoo.top:8083/dl/slitaz/iso/rolling/slitaz5-core64-cn-with-docker.iso
          wget -O ./packages/teasiu-5.0.tazpkg http://ecoo.top:8083/dl/slitaz/teasiu-5.0.tazpkg
          wget -O ./packages/updatetime.txt http://ecoo.top:8083/dl/slitaz/iso/rolling/slitaz-rolling-core64-chinese-updatetime.txt
          

      - name: download iso32 and unzip iso32
        run: |
          wget http://distro.ibiblio.org/slitaz/iso/rolling/slitaz-rolling-core.iso
          mkdir iso-old32
          7z x slitaz-rolling-core.iso -y -r -o./iso-old32
                              
      - name: docker build a new rootfs64
        run: |
          docker build -t newrootfs -f ./dockerfile-rootfs64 .
          
      - name: copy rootfs.gz outside of docker
        run: |
          docker run --name myrootfs newrootfs
          docker cp myrootfs:/tmp/rootfs-new.gz ./iso64/mkiso/rootfs.gz
          cd ./iso64
          sh iso.sh
          cd ../

      - name: docker build a new rootfs32
        run: |
          docker build -t newrootfs32 -f ./dockerfile-rootfs32 .
          
      - name: copy rootfs32.gz outside of docker
        run: |
          docker run --name myrootfs32 newrootfs32
          docker cp myrootfs32:/tmp/rootfs-new.gz ./iso32/mkiso/rootfs.gz
          cd ./iso32
          sh iso.sh

      - name: Prepare artifact
        run: |
          mkdir -p slitaz-artifact
          cp -a ./slitaz5.0-rolling-core64-cn.iso  slitaz-artifact/
          cp -a ./slitaz5.0-rolling-core32-cn.iso slitaz-artifact/
          cp -a ./slitaz5-core64-cn-with-docker.iso slitaz-artifact/

      - name: Deliver package
        uses: actions/upload-artifact@v2
        with:
          name: slitaz5-rolling-core64
          path: slitaz-artifact/

      - name: Create Release
        id: create_release
        uses: actions/create-release@v1
        env:
          GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
        with:
          tag_name: ${{ github.ref }}
          release_name: Release ${{ github.ref }}
          draft: false
          prerelease: false
          
      - name: Upload Release Asset2
        id: upload-release-asset2 
        uses: actions/upload-release-asset@v1
        env:
          GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
        with:
          upload_url: ${{ steps.create_release.outputs.upload_url }}
          asset_path: ./slitaz5.0-rolling-core32-cn.iso
          asset_name: slitaz5.0-rolling-core32-cn.iso
          asset_content_type: application/iso
          
      - name: Upload Release Asset3
        id: upload-release-asset3
        uses: actions/upload-release-asset@v1
        env:
          GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
        with:
          upload_url: ${{ steps.create_release.outputs.upload_url }}
          asset_path: ./slitaz5-core64-cn-with-docker.iso
          asset_name: slitaz5-core64-cn-with-docker.iso
          asset_content_type: application/iso

      - name: Upload Release Asset
        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 }}
          asset_path: ./slitaz5.0-rolling-core64-cn.iso
          asset_name: slitaz5.0-rolling-core64-cn.iso
          asset_content_type: application/iso
                 
  slitaz:
    runs-on: ubuntu-20.04
    needs: [build]
    steps:
      - name: Download a single artifact
        uses: actions/download-artifact@v2
        with:
          name: slitaz5-rolling-core64
          path: slitaz-artifact/
      - name: Upload to SourceForge
        run: |
          echo "$DEPLOY_KEY" > $HOME/deploy_key
          cat $HOME/deploy_key
          set -ex
          chmod 600 $HOME/deploy_key
          Localslitaz=`ls -d slitaz-artifact`
          Remoteslitaz=teasiu@frs.sourceforge.net:/home/frs/project/slitaz/slitaz5-rolling-core64
          rsync -e "ssh -i $HOME/deploy_key -oStrictHostKeyChecking=no -oBatchMode=yes" -va --delete $Localslitaz/*.iso $Remoteslitaz
        env:
          DEPLOY_KEY: ${{ secrets.TEASIU }}
