#!/bin/sh

if ! modprobe -a hellosysfs; then
	echo "Could not load module hellosysfs.ko!"
	exit
fi

if ! [ -f /sys/kernel/hello ] ; then
	echo "sysfs file not created!"
	exit
fi

if ! hello=$(cat /sys/kernel/hello); then
	echo "can't read from sysfs!"
	exit
fi
echo "$hello"

if [ "$hello" != "Hello sysfs!" ]; then
	echo "wrong string in sysfs, it should return exactly \"Hello sysfs!\""
	exit
fi

addedstr="Romeo"
printf "%s" "$addedstr" > /sys/kernel/hello

hello=$(cat /sys/kernel/hello)
echo "$hello"

if [ "$hello" != "Hello $addedstr!" ]; then
	echo "file or string not found!"
	exit
fi

addedstr="$(tr -dc A-Za-z0-9 </dev/urandom | head -c 5)"
printf "%s" "$addedstr" > /sys/kernel/hello

hello=$(cat /sys/kernel/hello)
echo "$hello"

if [ "$hello" != "Hello $addedstr!" ]; then
	echo "file or string not found!"
	exit
fi

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