#! /bin/bash
# SPDX-License-Identifier: GPL-2.0
# Copyright (c) 2026 Oracle.  All Rights Reserved.
#
# FS QA Test No. 677
#
# Functional testing for mkfs.xfs config file generation.
#
. ./common/preamble
_begin_fstest auto mkfs

# . ./common/filter

_require_scratch
_require_xfs_mkfs_cfgfile
_require_xfs_spaceman_command "makecfg"
_require_xfs_db_command "makecfg"
_require_command "$XFS_ADMIN_PROG" "xfs_admin"

# t1: Make sure all three tools generate the same config file
_scratch_mkfs >> $seqres.full
_scratch_mount
$XFS_SPACEMAN_PROG -c "makecfg $tmp.spaceman1" $SCRATCH_MNT
$XFS_ADMIN_PROG -C $tmp.onadmin1 $SCRATCH_MNT
_scratch_unmount

_scratch_mkfs -c makecfg=$tmp.mkfs1 >> $seqres.full
_scratch_xfs_db -c "makecfg $tmp.db1"
_scratch_xfs_admin -C $tmp.offadmin1

cmp -s $tmp.mkfs1 $tmp.spaceman1 || \
	echo "mkfs config1 does not match spaceman?"
cmp -s $tmp.db1 $tmp.spaceman1 || \
	echo "db config1 does not match spaceman?"
cmp -s $tmp.db1 $tmp.mkfs1 || \
	echo "db config1 does not match mkfs?"
cmp -s $tmp.onadmin1 $tmp.mkfs1 || \
	echo "online admin config1 does not match mkfs?"
cmp -s $tmp.offadmin1 $tmp.mkfs1 || \
	echo "offline admin config1 does not match mkfs?"

echo "*** spaceman config1" >> $seqres.full
cat $tmp.spaceman1 >> $seqres.full
echo "*** mkfs config1" >> $seqres.full
cat $tmp.mkfs1 >> $seqres.full
echo "*** db config1" >> $seqres.full
cat $tmp.db1 >> $seqres.full
echo "*** online admin config1" >> $seqres.full
cat $tmp.onadmin1 >> $seqres.full
echo "*** offline admin config1" >> $seqres.full
cat $tmp.offadmin1 >> $seqres.full

# t2: Make sure the output changes if we set a new rootdir inherit option
_scratch_mount
$XFS_IO_PROG -c 'chattr +P' -c 'chproj 33' $SCRATCH_MNT
$XFS_SPACEMAN_PROG -c "makecfg $tmp.spaceman2" $SCRATCH_MNT
_scratch_unmount
_scratch_xfs_db -c "makecfg $tmp.db2"

cmp -s $tmp.db2 $tmp.spaceman2 || \
	echo "db config2 does not match spaceman?"
cmp -s $tmp.db1 $tmp.db2 && \
	echo "db config1 should be different from db config2"

echo "*** spaceman config2" >> $seqres.full
cat $tmp.spaceman2 >> $seqres.full
echo "*** db config2" >> $seqres.full
cat $tmp.db2 >> $seqres.full

# t3: Format with t2 config file, make sure the results match t2 and not t1
_scratch_mkfs -c options=$tmp.db2 >> $seqres.full
_scratch_mount
$XFS_SPACEMAN_PROG -c "makecfg $tmp.spaceman3" $SCRATCH_MNT
_scratch_unmount
_scratch_xfs_db -c "makecfg $tmp.db3"

cmp -s $tmp.db3 $tmp.spaceman3 || \
	echo "db config3 does not match spaceman?"
cmp -s $tmp.db3 $tmp.db1 && \
	echo "db config3 should be different from db config1"
cmp -s $tmp.db3 $tmp.db2 || \
	echo "db config3 does not match db config2?"

echo "*** spaceman config3" >> $seqres.full
cat $tmp.spaceman3 >> $seqres.full
echo "*** db config3" >> $seqres.full
cat $tmp.db3 >> $seqres.full

# t4: Set autofsck filesystem property, make sure that gets reflected
autofsck="$(grep 'autofsck=' $tmp.spaceman3 | sed -e 's/^.*autofsck=//g')"
case "$autofsck" in
"")
	if grep -q 'crc=1' $tmp.spaceman3; then
		new_autofsck=check
	fi
	;;
repair) new_autofsck=check;;
*)	new_autofsck=repair;;
esac
if [ -n "$new_autofsck" ]; then
	$XFS_PROPERTY_PROG $SCRATCH_DEV set "autofsck=$new_autofsck" >> $seqres.full
	_scratch_mount
	$XFS_SPACEMAN_PROG -c "makecfg $tmp.spaceman4" $SCRATCH_MNT
	_scratch_unmount
	_scratch_xfs_db -c "makecfg $tmp.db4"

	cmp -s $tmp.db4 $tmp.spaceman4 || \
	echo "db config4 does not match spaceman?"
	cmp -s $tmp.db4 $tmp.db3 && \
		echo "db config4 should be different from db config3"

	echo "*** spaceman config4" >> $seqres.full
	cat $tmp.spaceman4 >> $seqres.full
	echo "*** db config4" >> $seqres.full
	cat $tmp.db4 >> $seqres.full

	_scratch_mkfs -c options=$tmp.db4 >> $seqres.full
	_scratch_mount
	$XFS_SPACEMAN_PROG -c "makecfg $tmp.spaceman4a" $SCRATCH_MNT
	_scratch_unmount
	_scratch_xfs_db -c "makecfg $tmp.db4a"

	cmp -s $tmp.db4a $tmp.spaceman4a || \
	echo "db config4a does not match spaceman?"
	cmp -s $tmp.db4a $tmp.db3 && \
		echo "db config4a should be different from db config3"
	cmp -s $tmp.db4a $tmp.db4 || \
	echo "db config4a does not match db config4?"

	echo "*** spaceman config4a" >> $seqres.full
	cat $tmp.spaceman4a >> $seqres.full
	echo "*** db config4a" >> $seqres.full
	cat $tmp.db4a >> $seqres.full

fi

if grep -q 'reflink=1' "$tmp.db3"; then
	# t5a: make sure that cli can override defaults
	_scratch_mkfs -c defaults=$tmp.db3 -m reflink=0 >> $seqres.full
	_scratch_xfs_db -c "makecfg $tmp.db5a"
	grep -q 'reflink=0' "$tmp.db5a" || \
		echo "db config5a should not have reflink"

	echo "*** db config5a" >> $seqres.full
	cat $tmp.db5a >> $seqres.full

	# t5b: make sure that cli cannot override config file
	_scratch_mkfs -c options=$tmp.db3 -m reflink=0 >> $seqres.full 2>> $tmp.fail
	grep -q 'reflink option respecified' $tmp.fail || \
		echo "mkfs should have failed on config5b"

	# t5c: make sure that config file can override defaults
	_scratch_mkfs -c options=$tmp.db3,defaults=$tmp.db3 >> $seqres.full
	_scratch_xfs_db -c "makecfg $tmp.db5c"
	grep -q 'reflink=1' "$tmp.db5c" || \
		echo "db config5c should have reflink"

	echo "*** db config5c" >> $seqres.full
	cat $tmp.db5c >> $seqres.full
fi

echo Silence is golden
_exit 0
