#!/sbin/sh

. "$env"

fs_type="f2fs"

fs_system_opts="ro"
fs_system_flags="wait"

fs_data_opts="nosuid,nodev,noatime,nodiratime,discard,inline_xattr"
fs_data_flags="wait,encryptable=footer"

fs_cache_opts="nosuid,nodev,noatime,nodiratime,discard,inline_xattr"
fs_cache_flags="wait"

# fstab_add <fstab> <partition> <fs type> <mount opts> <mount flags>
fstab_add() {
	awk -vpart="/$2" -vfs="$3" -vopts="$4" -vflags="$5" '
	$2 == part && $3 == fs {
		if (found) next
		if ($4 != opts) { $4 = opts; modified++ }
		if ($5 != flags) { $5 = flags; modified++ }
		found++
	}
	$2 == part && $3 != fs && !found {
		print $1 " " $2 " " fs " " opts " " flags
		found = ++modified
	}
	{ print }
	END { exit !modified }
	' "$1" > "$1-"
	[ $? = 0 ] && cat "$1-" > "$1" && print "Added $3 /$2 to $1"
	rm -f "$1-"
}

print "Patching the fstab for $fs_type support..."

found_fstab=false

for fstab in fstab.*; do
	[ -f "$fstab" ] || continue
	found_fstab=true

	echo "Found fstab: $fstab"

	# Uncomment these if you wish to use them, but I do not recommend it!
	# f2fs has no advantages for a read-only file system, and it may reduce
	# stability. Cache partitions are not used often, and other things that
	# rely on it are not prepared for f2fs.

	#fstab_add "$fstab" system "$fs_type" "$fs_system_opts" "$fs_system_flags"
	fstab_add "$fstab" data "$fs_type" "$fs_data_opts" "$fs_data_flags"
	#fstab_add "$fstab" cache "$fs_type" "$fs_cache_opts" "$fs_cache_flags"
done

$found_fstab || {
	print "Unable to find an fstab!"
}

exit 0
