#!/bin/sh

module_name=show_sb

if ! modprobe -a $module_name; then
	echo "Could not load module!"
	exit 1
fi

for type in tmpfs rootfs bdev devtmpfs debugfs dax dmabuf iomem tracefs pipefs aio mqueue virtiofs proc sysfs tmpfs securityfs cgroup2 overlay devpts tmpfs; do
	if ! dmesg | grep -q -P "uuid=[a-z0-9]{8}-[a-z0-9]{4}-[a-z0-9]{4}-[a-z0-9]{4}-[a-z0-9]{12} type=$type"; then
		echo "Failed to grep $type from module output"
		exit 1
	fi
done

if ! rmmod $module_name; then
	echo "Fail while unloading module"
	exit 1
fi

module_name=update_sb
arg="type=tracefs"

if ! modprobe $module_name $arg; then
	echo "Could not load module!"
	exit 1
fi

first=$(dmesg | tac | grep -m 1 tracefs | awk -F"time=" '{print $2}')

if ! rmmod $module_name; then
	echo "Fail while unloading module"
	exit 1
fi

if ! modprobe $module_name $arg; then
	echo "Could not load module!"
	exit 1
fi

second=$(dmesg | tac | grep -m 1 tracefs | awk -F"time=" '{print $2}')

if ! rmmod $module_name; then
	echo "Fail while unloading module"
	exit 1
fi

if ! modprobe $module_name $arg; then
	echo "Could not load module!"
	exit 1
fi

third=$(dmesg | tac | grep -m 1 tracefs | awk -F"time=" '{print $2}')

if ! rmmod $module_name; then
	echo "Fail while unloading module"
	exit 1
fi

arg="type=tmpfs"
if ! modprobe $module_name $arg; then
	echo "Could not load module!"
	exit 1
fi

fourth=$(dmesg | tac | grep -m 1 tmpfs | awk -F"time=" '{print $2}')

if ! rmmod $module_name; then
	echo "Fail while unloading module"
	exit 1
fi

if [ -z "$first" ]  ||[ -z "$second" ]  ||[ -z "$third" ]  ||[ -z "$fourth" ]; then
	echo "Failed to get once time value"
	exit 1
fi

if [ "$first" -ne 0 ]; then
	echo "Wrong value for time: $first"
	exit 1
fi

if [ "$fourth" -ne 0 ]; then
	echo "Wrong value for time: $fourth"
	exit 1
fi


if [ "$first" -ge "$second" ]; then
	echo "Inconsistent value for time: $first and $second"
	exit 1
fi

if [ "$second" -ge "$third" ]; then
	echo "Inconsistent value for time: $second and $third"
	exit 1
fi
