#!/system/bin/sh
# After boot: rotate Settings.Secure.android_id once (global, affects all apps).

MODDIR=${0%/*}
LOGTAG="maribank_device_spoof"

log() {
  echo "[$LOGTAG] $*" >> /cache/maribank_device_spoof.log 2>/dev/null
}

# Wait for SettingsProvider
i=0
while [ "$(getprop sys.boot_completed)" != "1" ] && [ "$i" -lt 120 ]; do
  sleep 1
  i=$((i + 1))
done
sleep 3

if [ ! -f "$MODDIR/android_id.txt" ]; then
  # 16 hex chars (standard ANDROID_ID format)
  AID=$(cat /proc/sys/kernel/random/uuid 2>/dev/null | tr -d '-' | cut -c1-16)
  [ -z "$AID" ] && AID="$(date +%s | md5sum 2>/dev/null | cut -c1-16)"
  echo "$AID" > "$MODDIR/android_id.txt"
fi
AID=$(cat "$MODDIR/android_id.txt")

settings put secure android_id "$AID" 2>/dev/null
log "android_id=$AID"

# Clear MariBank cache so SHPSSDK re-collects with new props (optional, user can disable)
PKG="sg.com.maribankmobile.digitalbank"
if [ -f "$MODDIR/clear_maribank_on_boot" ]; then
  pm clear "$PKG" 2>/dev/null
  log "pm clear $PKG"
fi

log "service.sh done"
