name: OrangeFox - Build for ASUS Zenfone 8 with LineageOS

on:
  workflow_dispatch:
    inputs:
      MANIFEST_BRANCH:
        description: 'OrangeFox branch'
        required: true
        default: '12.1'
        type: choice
        options:
        - 14.1
        - 12.1
        - 11.0
      DEVICE_TREE:
        description: 'Custom Recovery Tree (URL)'
        required: true
        default: 'https://github.com/TeamWin/android_device_asus_I006D'
      DEVICE_TREE_BRANCH:
        description: 'Custom Recovery Tree Branch'
        required: true
        default: 'android-12.1'
      DEVICE_PATH:
        description: 'Specify your device path.'
        required: true
        default: 'device/asus/I006D'
      LOS_BOOT_IMAGE:
        description: 'LineageOS Boot Image file (URL)'
        required: true
        default: 'https://mirrorbits.lineageos.org/full/sake/20250812/boot.img'
      LOS_VERSION:
        description: 'LineageOS version'
        required: true
        default: 'LineageOS_22.2'
      DEVICE_NAME:
        description: 'Specify your Device Codename.'
        required: true
        default: 'I006D'
      BUILD_TARGET:
        description: 'Specify your Build Target'
        required: true
        default: 'boot'
        type: choice
        options:
        - boot
        - recovery
        - vendorboot
      MAGISK_VERSION:
        description: 'Magisk version to use:'
        required: true
        default: 'v29.0'      
      LDCHECK:
        description: 'Path of blobs to check' 
        required: true
        default: 'system/bin/qseecomd'

jobs:
  build:
    name: Build OFRP for ASUS Zenfone 8 with LOS by ${{ github.actor }}
    runs-on: ubuntu-latest
    if: github.event.repository.owner.id == github.event.sender.id
    env:
      GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
    permissions:
      contents: write
    steps:
    - name: Checkout
      uses: actions/checkout@v3
      
    - name: Display Inputs
      run: |
        echo "::group::User Environment Variables"
        echo "Displaying variables/inputs."
        echo "Include Recovery Installer: ${{ github.event.inputs.RECOVERY_INSTALLER }}"
        echo "Release recovery.tar For Samsung devices: ${{ github.event.inputs.RECOVERY_TAR }}"
        echo "LineageOS Boot image used: ${{ github.event.inputs.LOS_BOOT_IMAGE }}"
        echo "LineageOS version: ${{ github.event.inputs.LOS_VERSION }}"
        echo "::endgroup::"

    - name: Clean-up
      uses: rokibhasansagar/slimhub_actions@main

    - name: Swap Space
      uses: pierotofy/set-swap-space@master
      with:
        swap-size-gb: 24
      
    - name: Build Environment
      run: |
        sudo apt update
        sudo apt -y upgrade
        git clone https://gitlab.com/OrangeFox/misc/scripts
        cd scripts
        sudo bash setup/android_build_env.sh
      
    - name: Set-up Manifest
      if: github.event.inputs.MANIFEST_BRANCH == '11.0' || github.event.inputs.MANIFEST_BRANCH == '12.1' || github.event.inputs.MANIFEST_BRANCH == '14.1'
      run: |
        mkdir -p ${GITHUB_WORKSPACE}/OrangeFox
        cd ${GITHUB_WORKSPACE}/OrangeFox
        git config --global user.name "Bernd Schemmer"
        git config --global user.email "bnsmb01@gmail.com"
        git clone https://gitlab.com/OrangeFox/sync.git
        cd sync
        ./orangefox_sync.sh --branch ${{ github.event.inputs.MANIFEST_BRANCH }} --path ${GITHUB_WORKSPACE}/OrangeFox/fox_${{ github.event.inputs.MANIFEST_BRANCH }}

    - name: Clone Device Tree
      run: |
        cd ${GITHUB_WORKSPACE}/OrangeFox/fox_${{ github.event.inputs.MANIFEST_BRANCH }}
        git clone ${{ github.event.inputs.DEVICE_TREE }} -b ${{ github.event.inputs.DEVICE_TREE_BRANCH }} ./${{ github.event.inputs.DEVICE_PATH }}
        cd ${{ github.event.inputs.DEVICE_PATH }}
        echo "COMMIT_ID=$(git rev-parse HEAD)" >> $GITHUB_ENV

    - name: Ensure TW_MAX_BRIGHTNESS is set
      run: |
        pwd ; ls -l && \
        grep -q 'TW_MAX_BRIGHTNESS' ${GITHUB_WORKSPACE}/OrangeFox/fox_12.1/device/asus/I006D/BoardConfig.mk || \
        echo 'TW_MAX_BRIGHTNESS := 255' >> ${GITHUB_WORKSPACE}/OrangeFox/fox_12.1/device/asus/I006D/BoardConfig.mk

    - name: Check Manifest Branch
      uses: haya14busa/action-cond@v1
      id: fox_branch
      with:
        cond: ${{ github.event.inputs.MANIFEST_BRANCH == '11.0' || github.event.inputs.MANIFEST_BRANCH == '12.1' }}
        if_true: lunch twrp_${{ github.event.inputs.DEVICE_NAME }}-eng && make clean && mka adbd ${{ github.event.inputs.BUILD_TARGET }}image
        if_false: lunch omni_${{ github.event.inputs.DEVICE_NAME }}-eng && make clean && mka ${{ github.event.inputs.BUILD_TARGET }}image
      
    - name: Building OrangeFox
      run: |
        cd ${GITHUB_WORKSPACE}/OrangeFox/fox_${{ github.event.inputs.MANIFEST_BRANCH }}
        set +e
        source build/envsetup.sh
        export ALLOW_MISSING_DEPENDENCIES=true
        set -e
        ${{ steps.fox_branch.outputs.value }}

    - name: Set Build Date # Output will be use in Release
      run: |
        echo "BUILD_DATE=$(date +%Y%m%d)" >> $GITHUB_ENV
        DEVICE_TREE_URL="${{ github.event.inputs.DEVICE_TREE }}"
        if [[ "${DEVICE_TREE_URL}" == *.git ]]; then
          DEVICE_TREE_URL="${DEVICE_TREE_URL%.git}"
        fi
        echo "DEVICE_TREE_URL=${DEVICE_TREE_URL}" >> $GITHUB_ENV

    - name: Check if Recovery Exist
      run: |
        pwd
        cd ${GITHUB_WORKSPACE}/OrangeFox/fox_${{ github.event.inputs.MANIFEST_BRANCH }}
        pwd
        if [ -f out/target/product/${{ github.event.inputs.DEVICE_NAME }}/OrangeFox*.img ]; then
            echo "CHECK_IMG_IS_OK=true" >> $GITHUB_ENV
            echo "FILE_PATH=${GITHUB_WORKSPACE}/OrangeFox/fox_${{ github.event.inputs.MANIFEST_BRANCH }}/out/target/product/${{ github.event.inputs.DEVICE_NAME }}" >> $GITHUB_ENV
            echo "MD5_IMG=$(md5sum out/target/product/${{ github.event.inputs.DEVICE_NAME }}/OrangeFox*.img | cut -d ' ' -f 1)" >> $GITHUB_ENV
        elif [ -f out/target/product/${{ github.event.inputs.DEVICE_NAME }}/target/product/${{ github.event.inputs.DEVICE_NAME }}/OrangeFox*.img ]; then
            echo "CHECK_IMG_IS_OK=true" >> $GITHUB_ENV
            echo "FILE_PATH=${GITHUB_WORKSPACE}/OrangeFox/fox_${{ github.event.inputs.MANIFEST_BRANCH }}/out/target/product/${{ github.event.inputs.DEVICE_NAME }}/target/product/${{ github.event.inputs.DEVICE_NAME }}" >> $GITHUB_ENV
            echo "MD5_IMG=$(md5sum out/target/product/${{ github.event.inputs.DEVICE_NAME }}/target/product/${{ github.event.inputs.DEVICE_NAME }}/OrangeFox*.img | cut -d ' ' -f 1)" >> $GITHUB_ENV
        else
            echo "Recovery out directory is empty."
            cd Exit
        fi

    - name: Rename the Recovery
      run: |
        cd ${{ env.FILE_PATH }}/
        if [ "${{ github.event.inputs.BUILD_TARGET }}" = "boot" ]; then
        mv OrangeFox*.img boot.img
        echo "RECOVERY_TYPE=boot" >> $GITHUB_ENV
        echo "boot.img file found!"
        elif [ "${{ github.event.inputs.BUILD_TARGET }}" = "recovery" ]; then
        mv OrangeFox*.img recovery.img
        echo "RECOVERY_TYPE=recovery" >> $GITHUB_ENV
        echo "recovery.img file found!"
        elif [ "${{ github.event.inputs.BUILD_TARGET }}" = "vendorboot" ]; then
        mv OrangeFox*.img vendor_boot.img
        echo "RECOVERY_TYPE=vendor_boot" >> $GITHUB_ENV
        echo "vendor_boot.img file found!"
        else
        echo "No Recovery file found!"
        fi
      continue-on-error: true

    - name: Create Recovery for LineageOS
      run: |
        set -x
        pwd ; ls -l
        cd ${{ env.FILE_PATH }}/
        pwd ; ls -l
        echo "
        OrangeFox recovery for ${{ github.event.inputs.DEVICE_NAME }} ${{ github.event.inputs.LOS_VERSION }} created at $( date ) with these values

        DEVICE_TREE:         ${{ github.event.inputs.DEVICE_TREE}}
        DEVICE_TREE_BRANCH:  ${{ github.event.inputs.DEVICE_TREE_BRANCH }}
        DEVICE_PATH:         ${{ github.event.inputs.DEVICE_PATH }}
        LOS_BOOT_IMAGE:      ${{ github.event.inputs.LOS_BOOT_IMAGE }}
        LOS_VERSION:         ${{ github.event.inputs.LOS_VERSION }}
        DEVICE_NAME:         ${{ github.event.inputs.DEVICE_NAME }}
        BUILD_TARGET:        ${{ github.event.inputs.BUILD_TARGET }}
        MAGISK_VERSION:      ${{ github.event.inputs.MAGISK_VERSION }}
        LDCHECK:             ${{ github.event.inputs.LDCHECK }}
        "   >${{ env.FILE_PATH }}//OrangeFox_recovery_version.txt
        test -r ./boot.img  && \
           echo "Creating an ugly boot image for ${{ github.event.inputs.LOS_VERSION }} using the boot image ${{ github.event.inputs.LOS_BOOT_IMAGE }} " >./OrangeFox_recovery.txt && \
           /usr/bin/wget -q https://github.com/topjohnwu/Magisk/releases/download/${{ github.event.inputs.MAGISK_VERSION }}/Magisk-${{ github.event.inputs.MAGISK_VERSION }}.apk -O Magisk.apk && \
           ls -l Magisk.apk && \
           /usr/bin/unzip -p Magisk.apk  lib/x86/libmagiskboot.so >magiskboot && \
           chmod 755 magiskboot && \
           ls -l magiskboot && \
           echo "### Unpack the boot.img file from OrangeFox ...." && \
           mkdir -p work && \
           cd work && \
           ${{ env.FILE_PATH }}/magiskboot unpack -h ../boot.img && \
           TWRP_CMDLINE="$( grep cmdline header | cut -f2- -d"=" )" && \
           pwd && \
           ls -l && \
           cat header && \
           cd .. && \
           echo "### Retrieve and unpack the boot.img file from LineageOS ...." && \
           /usr/bin/wget -q ${{ github.event.inputs.LOS_BOOT_IMAGE }} -O ./los_boot.img && \
           mkdir -p los && \
           cd los && \
           ${{ env.FILE_PATH }}/magiskboot unpack -h ../los_boot.img && \
           pwd && \
           ls -l && \
           cat header  && \
           echo "### Copy the ramdisk with OrangeFox ..." && \
           cp ../work/ramdisk.cpio . && \
           echo  "### Correct the header ..." && \
           sed -i -e "s#^cmdline=#cmdline=${TWRP_CMDLINE} #g" header && \
           cat header && \
           echo "### Create the new OrangeFox Recovery image for the LineageOS ..." && \
           ${{ env.FILE_PATH }}/magiskboot repack ../los_boot.img ../OrangeFox_recovery_${{ github.event.inputs.LOS_VERSION }}.img && \
           pwd && \
           ls -l && \
           cd ..
      continue-on-error: true
    
    - name: Include Recovery Installer
      run: |
        cd ${{ env.FILE_PATH }}/
        wget https://github.com/kinguser981/recovery-installer/releases/download/11256427880/recovery-installer.zip
        zip -ur recovery-installer.zip ${{ env.RECOVERY_TYPE }}.img
      continue-on-error: true
        
    - name: Upload to Release
      if: env.CHECK_IMG_IS_OK == 'true'
      uses: softprops/action-gh-release@v1
      with:
        files: |
          ${{ env.FILE_PATH }}/${{ env.RECOVERY_TYPE }}.img        
          ${{ env.FILE_PATH }}/recovery-installer.zip
          ${{ env.FILE_PATH }}/OrangeFox*.zip
          ${{ env.FILE_PATH }}/OrangeFox*.img
          ${{ env.FILE_PATH }}/OrangeFox*.txt
        name: Unofficial OrangeFox for ${{ github.event.inputs.DEVICE_NAME }} // ${{ env.BUILD_DATE }}
        tag_name: ${{ github.run_id }}
        body: |
          Build: ${{ github.event.inputs.MANIFEST_BRANCH }}
          Device: [Device Tree/Branch](${{ github.event.inputs.DEVICE_TREE }}/tree/${{ github.event.inputs.DEVICE_TREE_BRANCH }})
          Commit: Most recent [commit](${{ github.event.inputs.DEVICE_TREE }}/commit/${{ env.COMMIT_ID }}) during building.
          
    - name: Release ramdisk-recovery
      if: env.CHECK_IMG_IS_OK == 'true'
      uses: softprops/action-gh-release@4634c16e79c963813287e889244c50009e7f0981
      with:
        files: |
          ${{ env.FILE_PATH }}/obj/PACKAGING/vendor_ramdisk_fragments_intermediates/recovery.*
          ${{ env.FILE_PATH }}/ramdisk-recovery.*
        name: Unofficial ugly OrangeFox for ${{ github.event.inputs.LOS_VERSION }} on ${{ github.event.inputs.DEVICE_NAME }} // ${{ env.BUILD_DATE }}
        tag_name: ${{ github.run_id }}
        body: |
          Target: ${{ env.RECOVERY_TYPE }}.img
          Build: ${{ github.event.inputs.MANIFEST_BRANCH }}
          Device: [Device Tree/Branch](${{ env.DEVICE_TREE_URL }}/tree/${{ inputs.DEVICE_TREE_BRANCH }})
          Commit: Most recent [commit](${{ env.DEVICE_TREE_URL }}/commit/${{ env.COMMIT_ID }}) during building.
          MD5 (img): ${{ env.MD5_IMG }}

    - name: Run LDCheck
      run: |
        cd tools
        mv -n libneeds ${{ env.FILE_PATH }}/recovery/root/
        mv -n ldcheck ${{ env.FILE_PATH }}/recovery/root/
        cd ${{ env.FILE_PATH }}/recovery/root/
        python3 ldcheck -p system/lib64:vendor/lib64:system/lib:vendor/lib -d ${{ github.event.inputs.LDCHECK }}
        echo "Done checking missing dependencies. Review, and reconfigure your tree."
      continue-on-error: true
    
    - name: Update Release with Log URL
      uses: actions/github-script@v7
      with:
        github-token: ${{ secrets.GITHUB_TOKEN }}
        script: |
          const { owner, repo } = context.repo;
          const tag = "${{ github.run_id }}"; // gleiche tag_name wie oben
          const log_url = `https://github.com/${owner}/${repo}/actions/runs/${{ github.run_id }}`;

          // Release anhand des Tags holen
          const release = await github.rest.repos.getReleaseByTag({
            owner,
            repo,
            tag
          });

          const newBody = (release.data.body || "") + `\n\n🔗 [View Build Logs](${log_url})`;

          await github.rest.repos.updateRelease({
            owner,
            repo,
            release_id: release.data.id,
            body: newBody
          });

