#!/system/bin/sh

# Dolby Atmos™ partition file(s) restoration


mount_filesystems() {
  mount -o rw,remount / 2>/dev/null
  if [ "$BOOTMODE" ]; then
    mirror=/sbin/.magisk/mirror
    sysroot=$mirror/system_root
    if [ -d "$sysroot" ]; then
      mount -o rw,remount $sysroot
      sys=$(realpath $mirror/system)
      ven=$(realpath $mirror/vendor)
    else
      sys=$mirror/system
      mount -o rw,remount $sys
      if [ -L "$mirror/vendor" ]; then
        ven=$sys/vendor
      else
        mount -o rw,remount $mirror/vendor
        ven=$mirror/vendor
      fi
      sys=$(realpath $mirror/system)
      ven=$(realpath $mirror/vendor)
    fi
  else
    mount -o rw,remount /system
    sys=/system
    if [ -L /vendor ]; then
      ven=$sys/vendor
    else
      ven=/vendor
      mount -o rw,remount $ven
    fi
  fi
}

backup=.bak
filecontexts=$ven/etc/selinux/vendor_file_contexts
servicecontexts=$ven/etc/selinux/vendor_hwservice_contexts
manifest=$sys/etc/vintf/manifest.xml
for file in $filecontexts $servicecontexts $manifest; do
  if [ -e "$file$backup" ]; then
    mv -f $file$backup $file
  fi
done

mount -o ro,remount system 2/dev/null
mount -o ro,remount /vendor 2>/dev/null
