# POLYFILLS FOR MISSING FUNCTIONS
set_perm() {
  chown $1:$2 "$4"
  chmod $3 "$4"
}

run_program() {
  "$@"
}

show_progress() {
  :
}

ui_print " "
ui_print "========================="
ui_print "Vbmeta, Orangefox & Fenrir flasher"
ui_print "For Itel RS4 XOS 13 Renuked Edition V3"
ui_print "Orangefox by @gustmynuts"
ui_print "Fenrir by @Shirayuki39"
ui_print "Flasher Script by: @danda"
ui_print "Flasher Script Template by: @rianixia"
ui_print "Thanks to @shr174 for support"
ui_print "========================="
ui_print " "

ui_print "Verifying Device Compatibility..."

getVolumeKey() {
    ui_print "- Listening to volume keys. Press [+] for 'Yes' and [-] for 'No'"
    keyInfo=true
    while $keyInfo; do
        keyInfo=$(getevent -qlc 1 | grep KEY_VOLUME)
        [ -z "$keyInfo" ] && continue
        isUpKey=$(echo $keyInfo | grep KEY_VOLUMEUP)
        [ -n "$isUpKey" ] && return 0 || return 1
        break
    done
}

checkDevice() {
    myDevice=$(getprop ro.product.name)
    romDevice="S666LN"
    if [ "$myDevice" != "$romDevice" ]; then
        ui_print "- Device code verification failed. Please double-check if this package matches your device model."
        ui_print "- Flashing the wrong package may cause bricking, and you will bear the consequences. Do you want to continue flashing?"
        if ! getVolumeKey; then
            ui_print "- You chose to abort flashing."
            exit 1
        else
            ui_print "- You chose to continue flashing."
        fi
    fi
}
checkExit() {
    if [ $? -ne 0 ]; then
        ui_print "Error: Exit status $? detected. There may be an issue with your super partition. Flash the stock super.img first, then retry. Exiting..."
        exit 1
    fi
}

unmountPartitions() {
    umount system_root vendor product system_ext vendor_dlkm odm_dlkm \
           tr_carrier tr_company tr_mi tr_preload tr_product tr_region tr_theme 2>/dev/null
}

checkDevice

unmountPartitions

ui_print "Preparing A/B slot..."
package_extract_file "images/lk.img" "/dev/block/by-name/lk_a"
package_extract_file "images/lk.img" "/dev/block/by-name/lk_b"
package_extract_file "images/vendor_boot.img" "/dev/block/by-name/vendor_boot_a"
package_extract_file "images/vendor_boot.img" "/dev/block/by-name/vendor_boot_b"

S=$(getprop ro.boot.slot_suffix)
ui_print "Active Slot: $S"

if ! [ -e "/system/bin/avbctl" ]; then
  ui_print "avbctl binary not found. Extracting from zip..."
  package_extract_file "META-INF/bin/avbctl" "/system/bin/"
  chmod +x "/system/bin/avbctl"
else
  ui_print "avbctl binary found. Using from system..."
fi

sleep 1
ui_print " "

VERITY=$(avbctl get-verity)
VERIFICATION=$(avbctl get-verification)
DISABLE_VERITY=$(avbctl --force enable-verity)
DISABLE_VERIFICATION=$(avbctl --force enable-verification)

# Print avb vbmeta status
ui_print "Checking avb vbmeta status"
ui_print "- $VERITY"
ui_print "- $VERIFICATION"
ui_print "- ${DISABLE_VERITY%%.*}"
ui_print "- ${DISABLE_VERIFICATION%%.*}"
ui_print " "
ui_print "Done, Don't forget to format data !!! Incase it still bootlooped, try fastboot -w"
