#!/bin/bash

[ $# -ne 2 ] && { echo "$0 <dev> <dax/nodax>"; exit 1; }

DEV="$1"
DAX="$2"
MNT=/tdmsck

mkfs_mount()
{
	local fst="$1"
	local dev="$2"
	local mnt="$3"
	local dax="$4"
	local BLKSZ=$(getconf PAGE_SIZE)

	umount $dev > /dev/null 2>&1
	umount $mnt > /dev/null 2>&1
	mkdir -p $mnt
	wipefs -af $dev > /dev/null 2>&1
	if [[ $fst =~ ext ]] ; then
		if ! mkfs.${fst} -Fq -b ${BLKSZ} $dev ; then
			echo "mkfs $fst $dev failed"
			return 1
		fi
	fi

	if [[ $fst =~ xfs ]] ; then
		if ! mkfs.${fst} -fq -b size=${BLKSZ} $dev ; then
			echo "mkfs $fst $dev failed"
			return 1
		fi
	fi

	if [ -n "$dax" -a $dax -eq 1 ] ; then
		if ! mount -o dax $dev $mnt ; then
			echo "mount $dev $mnt dax failed"
			return 1
		fi
	else
		if ! mount $dev $mnt ; then
			echo "mount $dev $mnt failed"
			return 1
		fi
	fi
	echo -e "\n==== Testing on $fst ===="
}

do_test()
{
	mkfs_mount $1 $DEV $MNT $DAX
	cc diotest4.c -o diot4 || { echo "cc dio failed"; exit 1; }
	cc msync03.c -o msync3 || { echo "cc msync failed"; exit 1; }
	echo -e "\nTesting dio"
	./diot4 $MNT/tfile1
	echo -e "\nTesting msync"
	./msync3 $MNT/tfile2
}

do_test xfs
do_test ext4
