name: updateimages

on:
  push:
#    branches:
#      - main
    tags:
      - v*

  workflow_dispatch:
  
jobs:
  build:
    runs-on: ubuntu-latest
    steps:
      - uses: actions/checkout@v2
      - name: Install packages
        run: |
          sudo apt-get update
          sudo apt-get install -y libelf-dev qemu qemu-block-extra qemu-utils
      - name: Bring over config files from redpill-load
        run: |
          mkdir /opt/config-files
          cd /opt/config-files
          git clone -b develop https://github.com/pocopico/redpill-load.git
          ls -ltr /opt/config-files/redpill-load/config 
      - name: Update Tinycore Redpill Images
        id: updateimages
        run: |
          mkdir /opt/tinycore-redpill
          cd /opt/tinycore-redpill
          cp -rfp /home/runner/work/tinycore-redpill/tinycore-redpill/* .
          cp -rfp /opt/config-files/redpill-load/config /opt/tinycore-redpill
          sudo find . -type f -name "*.sh" -exec chmod +x {} \;
          sudo chmod +x /opt/tinycore-redpill/tools/*
          VERSION="`grep "rploaderver=" rploader.sh | awk -F= '{print "v"$2}' | sed -e 's/"//g'`"
          echo "Version: ${VERSION}"
          echo "::set-output name=VERSION::${VERSION}"
          echo "/opt/tinycore-redpill files" 
          ls -ltr /opt/tinycore-redpill
          echo "/opt/config-files files" 
          ls -ltr /opt/tinycore-redpill/config
          echo '---start Update---'
          mkdir /opt/images 
          cd /opt/images/
          URLS="`curl -s https://api.github.com/repos/pocopico/tinycore-redpill/releases/tags/v0.9.2.8 | jq -r -e .assets[].browser_download_url`"
          for file in $URLS; do curl --insecure --silent --location "$file" -O ; done
          ls -ltr 
          gunzip *.gz
          ls -ltr 
          
          mkdir /opt/workdir

          workdir="/opt/workdir"
          srcdir="/opt/images"
          tgtdir="/opt/newimages"
          
          ###########################################################################

          mkdir $tgtdir

          cd $workdir
          
          workfile="`ls  $srcdir/*uefi*`"
          mv $workfile $workdir
          workfile="`ls *uefi*`"
          imgversion="`ls $workfile | cut -c 23-30`"
          imgfile="`echo $workfile | sed -e 's/.gz//g'`"
          echo "Found workfile : $workfile , imgversion : $imgversion copying from $srcdir to $workdir"
          sudo losetup -fP ./$imgfile
          loopdev=$(losetup -j $imgfile| awk '{print $1}' | sed -e 's/://')
          echo "Creating mountpoint on $workdir/"
          mkdir $workdir/partition1
          mkdir $workdir/partition2
          mkdir $workdir/partition3
          sudo mount ${loopdev}p1 $workdir/partition1
          sudo mount ${loopdev}p2 $workdir/partition2
          sudo mount ${loopdev}p3 $workdir/partition3
          
          mkdir $workdir/tempmydata
          cd $workdir/tempmydata
          sudo tar xvfz $workdir/partition3/mydata.tgz
          
          sudo chown -R 1001:50 $workdir/tempmydata/home/tc/
          sudo chown -R 1001:50 $workdir/tempmydata/opt
          cd $workdir/tempmydata/home/tc/
          
          cp -rf /opt/tinycore-redpill/* .
                    
          rm modules.alias.3.json ; gunzip modules.alias.3.json.gz
          rm modules.alias.4.json ; gunzip modules.alias.4.json.gz
          
          cd $workdir/tempmydata
          
          sudo tar cvfz $workdir/partition3/mydata.tgz *
          cp $workdir/partition3/mydata.tgz $workdir
          
          cd $workdir
          
          sudo umount ${loopdev}p1
          sudo umount ${loopdev}p2
          sudo umount ${loopdev}p3
          
          sudo fsck.msdos ${loopdev}p1
          sudo fsck.msdos ${loopdev}p2
          sudo fsck.msdos ${loopdev}p3

          sudo losetup -d $loopdev
          
          newimgfile="`echo $imgfile | sed -e "s/$imgversion/$VERSION/g"`"
          
          mv $imgfile $newimgfile
          
          gzip -9 $newimgfile
          
          rm -rf $workdir/tempmydata
          tar tvfz mydata.tgz > tar1.output
          rm $workdir/mydata.tgz
          echo "Moving file ${newimgfile}.gz"
          
          mv ${newimgfile}.gz $tgtdir
          
          ###########################################################################
          
          cd $workdir
          
          workfile="`ls $srcdir/tinycore-redpill.*.img*`"
          mv $workfile $workdir
          workfile="`ls tinycore-redpill.*.img*`"
          imgversion="`ls $workfile | cut -c 18-25`"
          imgfile="`echo $workfile | sed -e 's/.gz//g'`"
          echo "Found workfile : $workfile , imgversion : $imgversion copying from $srcdir to $workdir"
          sudo losetup -fP ./$imgfile
          loopdev=$(losetup -j $imgfile| awk '{print $1}' | sed -e 's/://')
          echo "Creating mountpoint on $workdir/"
          [ ! -d $workdir/partition1 ] && mkdir $workdir/partition1
          [ ! -d $workdir/partition2 ] && mkdir $workdir/partition2
          [ ! -d $workdir/partition3 ] && mkdir $workdir/partition3
          sudo mount ${loopdev}p1 $workdir/partition1
          sudo mount ${loopdev}p2 $workdir/partition2
          sudo mount ${loopdev}p3 $workdir/partition3
          
          mkdir $workdir/tempmydata
          cd $workdir/tempmydata
          sudo tar xvfz $workdir/partition3/mydata.tgz
          
          sudo chown -R 1001:50 $workdir/tempmydata/home/tc/
          sudo chown -R 1001:50 $workdir/tempmydata/opt
          cd $workdir/tempmydata/home/tc/
          
          cp -rf /opt/tinycore-redpill/* .
                    
          rm modules.alias.3.json ; gunzip modules.alias.3.json.gz
          rm modules.alias.4.json ; gunzip modules.alias.4.json.gz
          
          cd $workdir/tempmydata
          
          sudo tar cvfz $workdir/partition3/mydata.tgz *
          cp $workdir/partition3/mydata.tgz $workdir
          
          cd $workdir
          
          sudo umount ${loopdev}p1
          sudo umount ${loopdev}p2
          sudo umount ${loopdev}p3
          
          sudo fsck.msdos ${loopdev}p1
          sudo fsck.msdos ${loopdev}p2
          sudo fsck.msdos ${loopdev}p3
          
          sudo losetup -d $loopdev
          
          newimgfile="`echo $imgfile | sed -e "s/$imgversion/$VERSION/g"`"
          
          mv $imgfile $newimgfile

          echo "New Image file name : $newimgfile"
 

          vmdkfile=`echo $newimgfile | sed -e 's/.img/.vmdk/g'`
          #qemu-img convert -O vmdk -o adapter_type=lsilogic $imgfile -o subformat=monolithicFlat $vmdkfile
          qemu-img convert -O vmdk -o adapter_type=lsilogic $newimgfile -o compat6 $vmdkfile

          echo "VMDK File $vmdkfile"

          gzip -9 $newimgfile
          gzip -9 $vmdkfile

          rm -rf $workdir/tempmydata
          tar tvfz mydata.tgz > tar2.output
          rm $workdir/mydata.tgz
          echo "Moving file ${newimgfile}.gz"
          
          mv ${newimgfile}.gz $tgtdir

          echo "Moving file ${vmdkfile}.gz"
          mv ${vmdkfile}.gz $tgtdir

          
          ###########################################################################
      - name: Upload tinycore-redpill-${{ steps.updateimages.outputs.VERSION }}
        uses: actions/upload-artifact@v3
        with:
          name: tinycore-redpill-${{ steps.updateimages.outputs.VERSION }}
          path: /opt/newimages         
             
  # Publish a release if is a tag
      - name: Release
        uses: softprops/action-gh-release@v1
        if: startsWith(github.ref, 'refs/tags/')
        with:
          files: |
            /opt/newimages/tinycore-redpill-uefi.${{ steps.updateimages.outputs.VERSION }}.img.gz
            /opt/newimages/tinycore-redpill.${{ steps.updateimages.outputs.VERSION }}.img.gz
            /opt/newimages/tinycore-redpill.${{ steps.updateimages.outputs.VERSION }}.vmdk.gz
