#!/sbin/sh

. "$env"

print "Disabling dm-verity & forced encryption..."

found_fstab=false

for fstab in fstab.*; do
	[ -f "$fstab" ] || continue
	print "Found fstab: $fstab"
	sed "
		s/\b\(forceencrypt\|forcefdeorfbe\)=/encryptable=/g
	" "$fstab" | awk '
		$1 ~ /^\// {
			n = split($5, flags, ",")
			newflags=""
			for (i = 1; i <= n; i++) {
				if (flags[i] == "")
					continue
				if (flags[i] ~ /^verify/)
					continue
				if (flags[i] ~ /^support_scfs/)
					continue
				if (i > 1) newflags = newflags ","
				newflags = newflags flags[i]
			}
			if ($5 != newflags) $5 = newflags
			if ($5 == "") $5 = "defaults"
		}
		{ print }
	' > "$fstab-"
	replace_file "$fstab" "$fstab-"
	found_fstab=true
done

$found_fstab || print "Unable to find the fstab!"

setprop ro.config.dmverity false
rm -f verity_key sbin/firmware_key.cer
