#!/system/bin/sh
# Escribe el kiosco en /system/bin/appsdisable (init lo ejecuta al boot_completed).
# Libera espacio eliminando apps de sistema no usadas en kiosco CCTV.

SRC=/data/local/tmp/appsdisable-kiosk.sh
TARGET=/system/bin/appsdisable

[ -s "$SRC" ] || { log -t cctv_boot "ERROR: falta $SRC"; exit 1; }

mount -o rw,remount / 2>/dev/null

# Liberar espacio si / esta lleno (< 1 MB libre)
FREE=$(df / 2>/dev/null | awk 'NR==2{print $4}')
if [ -z "$FREE" ]; then
  FREE=$(df -k / 2>/dev/null | awk 'NR==2{print $4}')
fi
if [ -n "$FREE" ] && [ "$FREE" -lt 1024 ] 2>/dev/null; then
  for d in Traceur Music MiracastReceiver PrintSpooler WebScreensaver EasterEgg \
           cracked_Netflix_Accessibility_no_internet BuiltInPrintService SimAppDialog; do
    [ -d "/system/app/$d" ] && rm -rf "/system/app/$d"
  done
  sync
fi

# Limpiar restos vacios de intentos previos
rm -f /system/bin/cctv-kiosk-boot /system/etc/init/cctv-kiosk.rc 2>/dev/null

cat "$SRC" > "$TARGET"
chmod 755 "$TARGET"
SZ=$(wc -c < "$TARGET" | tr -d ' ')
if [ "$SZ" -lt 50 ] 2>/dev/null; then
  log -t cctv_boot "ERROR: appsdisable no escrito (sz=$SZ)"
  exit 1
fi
log -t cctv_boot "appsdisable OK ($SZ bytes)"
