#!/sbin/sh

# ============================================================
# TWRP Flashable Zip — CB-C6s-STU-GSI-Fixes
#
# This is a shell-script update-binary. TWRP extracts the zip,
# sets the working dir to that temp location, and runs this script.
# The updater-script is a dummy — all logic lives here.
#
# What it does:
#   - Appends build.prop entries to /system/build.prop (not replace!)
#   - Installs goodix_ts.idc to /system/usr/idc/
#   - Installs sensor_config.xml to /vendor/etc/
# ============================================================

OUTFD=$2
ZIPFILE=$3

# ----- helpers -----
ui_print() {
  echo -n -e "ui_print $1\n" > /proc/self/fd/$OUTFD
  echo -n -e "ui_print\n" > /proc/self/fd/$OUTFD
}

abort() {
  ui_print " "
  ui_print "ERROR: $1"
  ui_print "Installation aborted."
  exit 1
}

# ----- locate zip root -----
# The script lives at:  META-INF/com/google/android/update-binary
# Zip root is 3 levels up from the script's directory.
SCRIPT_DIR=$(cd "$(dirname "$0")" && pwd)
ZIPROOT=$(cd "$SCRIPT_DIR/../../.." && pwd)

ui_print "========================================"
ui_print "  CB-C6s-STU-GSI-Fixes"
ui_print "  Tablet GSI tweaks"
ui_print "========================================"

# ----- mount partitions -----
ui_print " "
ui_print "Mounting partitions..."

mount /system 2>/dev/null
mount /vendor 2>/dev/null

# Verify /system is accessible
if [ ! -d /system ] || [ ! -f /system/build.prop ]; then
  abort "Cannot access /system. Is the system partition mounted?"
fi

ui_print "Partitions ready."

# ----- 1. APPEND build.prop (关键：追加，不覆盖) -----
ui_print " "
ui_print "[1/3] Appending entries to /system/build.prop..."

BUILDPROP_SRC="$ZIPROOT/system/build.prop"

if [ ! -f "$BUILDPROP_SRC" ]; then
  abort "build.prop not found inside zip."
fi

# Backup original before modifying
cp /system/build.prop /system/build.prop.bak 2>/dev/null
chmod 644 /system/build.prop.bak 2>/dev/null

# APPEND — 使用 >> 追加而非覆盖
cat "$BUILDPROP_SRC" >> /system/build.prop
chmod 644 /system/build.prop

ui_print "build.prop: entries appended. Backup at build.prop.bak"

# ----- 2. Install touchscreen IDC -----
ui_print " "
ui_print "[2/3] Installing goodix_ts.idc..."

IDC_SRC="$ZIPROOT/system/usr/idc/goodix_ts.idc"
IDC_DST="/system/usr/idc/goodix_ts.idc"

if [ -f "$IDC_SRC" ]; then
  mkdir -p /system/usr/idc 2>/dev/null
  cp -f "$IDC_SRC" "$IDC_DST"
  chmod 644 "$IDC_DST"
  ui_print "goodix_ts.idc installed."
else
  ui_print "goodix_ts.idc not found — skipped."
fi

# ----- 3. Install sensor config -----
ui_print " "
ui_print "[3/3] Installing sensor_config.xml..."

SENSOR_SRC="$ZIPROOT/vendor/etc/sensor_config.xml"

if [ -f "$SENSOR_SRC" ]; then
  # /vendor may be a separate partition OR a symlink under /system
  if [ -d /vendor/etc ]; then
    cp -f "$SENSOR_SRC" /vendor/etc/sensor_config.xml
    chmod 644 /vendor/etc/sensor_config.xml
    ui_print "sensor_config.xml → /vendor/etc/"
  elif [ -d /system/vendor/etc ]; then
    cp -f "$SENSOR_SRC" /system/vendor/etc/sensor_config.xml
    chmod 644 /system/vendor/etc/sensor_config.xml
    ui_print "sensor_config.xml → /system/vendor/etc/"
  else
    ui_print "WARNING: /vendor/etc not found — skipped."
  fi
else
  ui_print "sensor_config.xml not found — skipped."
fi

# ----- done -----
ui_print " "
ui_print "========================================"
ui_print "  Installation complete!"
ui_print "  Reboot to apply changes."
ui_print "========================================"

exit 0
