#!/sbin/sh
# TWRP-12.1_A-Team_genevn-XT2315 installer (recovery)
DEVICE="genevn"
MODEL="XT2315"
TYPE="recovery"
NAME="TWRP-12.1_A-Team_genevn-XT2315"
OUTFD="/proc/self/fd/$2"
ZIPFILE="$3"
ui_print() { echo "ui_print $1" > "$OUTFD"; echo "ui_print" > "$OUTFD"; }
abort()    { ui_print "ERROR: $1"; exit 1; }

ui_print "--------------------------------"
ui_print " $NAME"
ui_print " target: $TYPE"
ui_print "--------------------------------"

DEV="$(getprop ro.product.device)"
[ -z "$DEV" ] && DEV="$(getprop ro.build.product)"
case "$DEV" in
  "$DEVICE"|"$DEVICE"_*|"$MODEL"|"$MODEL"-*) ;;
  *) abort "This zip is for $DEVICE ($MODEL), this device is '$DEV'" ;;
esac
ui_print "- Device check OK: $DEV"

TMP=/tmp/twrp-install
rm -rf "$TMP"; mkdir -p "$TMP"
unzip -o "$ZIPFILE" twrp.img twrp.img.sha256 -d "$TMP" >/dev/null || abort "Could not extract image"
( cd "$TMP" && sha256sum -c twrp.img.sha256 >/dev/null 2>&1 ) || abort "Image checksum mismatch"

BYNAME=/dev/block/bootdevice/by-name
[ -d "$BYNAME" ] || BYNAME=/dev/block/by-name

# partition path(s): suffixed slots if they exist, else the plain name
slots_for() {
  if [ -e "$BYNAME/${1}_a" ]; then echo "_a _b"; else echo "none"; fi
}
part_path() { [ "$2" = "none" ] && echo "$BYNAME/$1" || echo "$BYNAME/$1$2"; }
write_img() { dd if="$1" of="$2" bs=1M 2>/dev/null || abort "Write to $2 failed"; }

case "$TYPE" in
  recovery)
    N=0
    for S in $(slots_for recovery); do
      P="$(part_path recovery "$S")"; [ -e "$P" ] || continue
      ui_print "- Flashing $(basename "$P")"; write_img "$TMP/twrp.img" "$P"; N=$((N + 1))
    done
    [ "$N" -gt 0 ] || abort "No recovery partition found"
    ;;

  vendor_boot)
    CUR="$(getprop ro.boot.slot_suffix)"
    if [ -e "$BYNAME/vendor_boot$CUR" ]; then P="$BYNAME/vendor_boot$CUR"
    elif [ -e "$BYNAME/vendor_boot" ]; then P="$BYNAME/vendor_boot"
    else abort "No vendor_boot partition found"; fi
    ui_print "- Flashing $(basename "$P") (current slot only)"
    write_img "$TMP/twrp.img" "$P"
    ;;

  boot)
    MB="$(command -v magiskboot || true)"
    [ -z "$MB" ] && [ -x /system/bin/magiskboot ] && MB=/system/bin/magiskboot
    [ -n "$MB" ] || abort "magiskboot not available in this recovery - cannot inject ramdisk"
    mkdir -p "$TMP/new"
    ( cd "$TMP/new" && "$MB" unpack -h "$TMP/twrp.img" >/dev/null 2>&1 ) || abort "Could not unpack TWRP image"
    [ -f "$TMP/new/ramdisk.cpio" ] || abort "TWRP image has no ramdisk"
    N=0
    for S in $(slots_for boot); do
      P="$(part_path boot "$S")"; [ -e "$P" ] || continue
      ui_print "- Injecting TWRP into $(basename "$P")"
      W="$TMP/work$S"; rm -rf "$W"; mkdir -p "$W"
      dd if="$P" of="$W/boot.img" bs=1M 2>/dev/null || abort "Could not read $P"
      ( cd "$W" && "$MB" unpack -h boot.img >/dev/null 2>&1 ) || abort "Could not unpack $(basename "$P")"
      cp -f "$TMP/new/ramdisk.cpio" "$W/ramdisk.cpio"
      ( cd "$W" && "$MB" repack boot.img new-boot.img >/dev/null 2>&1 ) || abort "Repack failed for $(basename "$P")"
      write_img "$W/new-boot.img" "$P"; N=$((N + 1))
    done
    [ "$N" -gt 0 ] || abort "No boot partition found"
    ;;
esac

sync
rm -rf "$TMP"
ui_print "- Done. Reboot to recovery to use $NAME."
exit 0
