#!/bin/sh

show_cmd()
{
	echo
	echo "=================================="
	echo "CMD: $*"
	$* 

}

banner()
{
	if [ "X$1" == "Xstart" ]
	then
		echo "*************************************************"
		echo "Output of $2"
		echo "*************************************************"
	else
		echo -e "\n\n"
	fi
}

run()
{
	fct=$1
	banner "start" $fct
	$fct
	banner "end"
}

linux_query()
{
	show_cmd cat /proc/version
	show_cmd cat /proc/cmdline
	[ -e /proc/config.gz ] && show_cmd gzip -d -c /proc/config.gz
	show_cmd cat /proc/meminfo
}

sysctl_query()
{
	show_cmd sysctl -a
}

modules_query()
{
	show_cmd lsmod
	[ -e /etc/modules ] &&  show_cmd cat /etc/modules

	show_cmd ls /sys/bus/platform/devices
	show_cmd ls /sys/bus/platform/drivers
	show_cmd ls /sys/bus/amba/devices
	show_cmd ls /sys/bus/amba/drivers

	echo
	echo "=================================="
	echo -e "Modules details:\n"

	lsmod | \
	awk '{print $1}' | \
	while read module; do 
		find /lib/modules -name "$module.ko"; 
	done | \
	while read path; do
		details=`ls -l $path`
		md5=`md5sum $path| awk '{print $1}'`
		module=`basename $path .ko`
		echo "$module"
		echo -e "\t$details"
		echo -e "\tMD5: $md5"
		if [ -d /sys/module/$module/parameters ]; then
			ls /sys/module/$module/parameters | \
			while read param; do
				echo -e "\t$param: `cat /sys/module/$module/parameters/$param`"
			done
		fi
		echo
	done
}

config_query()
{
	show_cmd cat /etc/sysconfig/config
	[ -e /etc/sysconfig/devices ] &&  show_cmd cat /etc/sysconfig/devices

	show_cmd chkconfig

	echo
	echo "=================================="
	echo -e "Config options:\n"

	ls /var/config | grep -v "options" | \
	while read item; do
		if [ -e /var/config/$item.options ]; then
			echo "$item"
			cat /var/config/$item.options | \
			while read line; do echo -e "\t$line"; done
		fi
	done

}

mboot_query()
{
	true
	# figure out where the bootloader is and then could
	# dump and get the env.  would need to update mboot_envrw
	# compile for arm and add to install
}

mtd_query()
{
	[ -e /proc/mtd ] && show_cmd cat /proc/mtd
	[ -e /bin/mtdinfo ] && show_cmd mtdinfo -a
}

nand_device_query()
{
	[ -e /proc/driver/nand/info ] && show_cmd cat /proc/driver/nand/info
}

ioctl_device_query()
{
	show_cmd cat /sys/bus/platform/drivers/m_ioctrl/function
	show_cmd cat /sys/bus/platform/drivers/m_ioctrl/drivestrength 
	show_cmd cat /sys/bus/platform/drivers/m_ioctrl/slewrate 
	show_cmd cat /sys/bus/platform/drivers/m_ioctrl/gpio 
	show_cmd cat /sys/bus/platform/drivers/m_ioctrl/macro 
}

device_query()
{
	show_cmd ls -l /dev
	show_cmd cat /proc/devices
	show_cmd cat /proc/partitions

	run mtd_query
	run nand_device_query
	run ioctl_device_query
}

interrupts_query()
{
	show_cmd cat /proc/interrupts
}

log_query()
{
	[ -x /bin/dmesg ] && show_cmd /bin/dmesg
	[ -x /sbin/logread ] && show_cmd /sbin/logread
}

run linux_query
run modules_query
run config_query
# run specific device queries from this function
run device_query
#run interrupts_query
run log_query
run sysctl_query
