#!/system/bin/sh
# Set up overlay mount for CamSwapper priv-app

MODDIR=/data/adb/modules/camswapper-vdm

# Mount tmpfs for our overlay upperdir
mount -t tmpfs tmpfs /mnt/camswapper_overlay

# Create directory structure
mkdir -p /mnt/camswapper_overlay/upper/system/priv-app/CamSwapper
mkdir -p /mnt/camswapper_overlay/work

# Copy APK from module (it's already there from module packaging)
# No, we need to copy it - but the module file is readable by root
cp "$MODDIR/system/priv-app/CamSwapper/CamSwapper.apk" /mnt/camswapper_overlay/upper/system/priv-app/CamSwapper/

# Set proper permissions and SELinux context
chmod 755 /mnt/camswapper_overlay/upper/system/priv-app/CamSwapper
chmod 644 /mnt/camswapper_overlay/upper/system/priv-app/CamSwapper/CamSwapper.apk
chcon -R u:object_r:system_file:s0 /mnt/camswapper_overlay/upper/system/priv-app

# Mount overlay on /system/priv-app
mount -t overlay overlay \
  -o lowerdir=/system/priv-app,upperdir=/mnt/camswapper_overlay/upper/system/priv-app,workdir=/mnt/camswapper_overlay/work \
  /system/priv-app

echo "Overlay mount result: $?"
ls /system/priv-app/CamSwapper/ 2>&1
