#!/bin/bash

CONFIG_FILE=./config
CRONTAB_FILE=./crontab
CONTROL_FILE=./control
SYSCONFIG_FILE=/etc/sysconfig/kdump

declare -i ERRORS=0
declare -i NUM_ITERATIONS=1
declare -i CRASH_FOUND=0
declare -i DISTRO_SUSE=0
declare -i DISTRO_RH=0
# ARCH = 1 means it's ia32/x86_64 arch
# ARCH = 2 means it's ppc64 arch
declare -i ARCH

X86_CRASHKERNEL='crashkernel=128M@16M'
PPC_CRASHKERNEL='crashkernel=128M@32M'
USE_CRASH="n"
SCSI="n"
IDE="n"
USE_MOD="1"
VMLINUX_DEBUG=""
LINUX_2_6_16=2616

# Determine architecture we're running on
arch=$(uname -i)
if [ $arch = "i386" ]
then
        CRASHKERNEL=$X86_CRASHKERNEL
        ARCH=1
else if [ $arch = "x86_64" ]
then
        CRASHKERNEL=$X86_CRASHKERNEL
        ARCH=1
else if [ $arch = "ppc64" ]
then
        CRASHKERNEL=$PPC_CRASHKERNEL
        ARCH=2
else
        echo "Unsupported architecture... exiting"
        exit
fi
fi
fi
# Determine the Kernel Version
KERNEL_VERSION=$(uname -r | awk -F . '{ print $1 $2 $3}' | awk -F - '{ print $1 }' )
if [ $KERNEL_VERSION -lt $LINUX_2_6_16 ]; then
	echo "The tests requires kernel version to be 2.6.16 or greater"
	exit
fi
	
# Determine the distribution we are running on
if [ -f /etc/redhat-release ]; then
        DISTRO_RH=1
else if [ -f /etc/SuSE-release ]; then
        DISTRO_SUSE=1
else
        echo "Unsupported distribution ... exiting"
        exit
fi
fi

echo "DISTRO_RH=${DISTRO_RH}" > $CONFIG_FILE
echo "DISTRO_SUSE=${DISTRO_SUSE}" >> $CONFIG_FILE

echo "Using the crash command and the kernel debug info during results"
echo "verfication is optional. If you choose to do so, they must be"
echo "installed on the system before running this script."
echo ""
read -p "Do you want to use crash and the kernel debug info? (y/n): " USE_CRASH  
if [ $USE_CRASH = "y" ]; then
	read -p "Enter the location of the kernel debug binary: " VMLINUX_DEBUG
	if [ ! -f $VMLINUX_DEBUG ]; then
		echo "${VMLINUX_DEBUG} NOT FOUND... crash won't be used during verify"
		VMLINUX=""
	else
		# vmlinux found, now check for crash
		REQ_FOUND=$(which crash 2> /dev/null)
		if [ $REQ_FOUND ]; then
			CRASH_FOUND=1
		else
			echo "crash NOT FOUND... crash won't be used during verify"
		fi
	fi
fi
echo "CRASH_FOUND=${CRASH_FOUND}" >> $CONFIG_FILE

# How many times to iterate through the tests
read -p "How many interations of the the test set do you want to run? (default is 1): " NUM_ITERATIONS
if [ $NUM_ITERATIONS -le 0 ]; then
	NUM_ITERATIONS=1
fi
echo "NUM_ITERATIONS=${NUM_ITERATIONS}" >> $CONFIG_FILE

# Create testfile with the commands to be run depending on the module to be 
# used for testing

rm -f testfile
read -p "Use lkdtm or crasher module (1/2) :" USE_MOD
if [ $USE_MOD -eq 1 ]; then
	cp -f testlists/lkdtm.orig testlists/lkdtm
	ln -s testlists/lkdtm testfile
	
	read -p "Are SCSI disks present (y/n) :" SCSI
	if [ $SCSI = "y" ]; then
		echo -e "KPSB\nKPSE\nKPSL\nKPSP" >> testfile
	fi

	read -p "Are IDE disks present (y/n) :" IDE
	if [ $IDE = "y" ]; then
		echo -e "KPIB\nKPIE\nKPIL\nKPIP" >> testfile
	fi
else
	ln -s testlists/crasher testfile
fi
	
#
# Checks
#
echo "Checking other prerequisites..."

# Check for kexec-tools
if [ -f /sbin/kexec -a -f /etc/sysconfig/kdump ]; then
	echo "Found kexec-tools..."
	echo " "
else
	echo "kexec-tools NOT FOUND..."
	echo "Please install kexec-tools rpm and run the setup"
	echo " "
        let "ERRORS++"
fi

# Check for kdump kernel
REQ_FOUND=$(rpm -qa | grep kernel-kdump 2>/dev/null)
if [ $REQ_FOUND ]; then
        echo "Found kernel-kdump..."
	echo " "
else
	# Currently PPC64 arch on RHEL5 doesnot support relocatable kernel.
	# Hence a seperate rpm for kdump kernel is shipped.
	# For SuSE all arch do not have relocatable support hence require
	# kdump rpm to be shipped.

	if [ $DISTRO_RH -eq 1 ] && [ $ARCH -eq 1 ]; then
        	echo "Found kernel-kdump..."
	else
        	echo "kernel-kdump NOT FOUND..."
		echo "Please install kernel-kdump rpm and run the setup"
        	let "ERRORS++"
	fi
	echo " "
fi

# Make sure kernel was booted with proper crashkernel values
STR=$(cat /proc/cmdline | grep -c $CRASHKERNEL 2>/dev/null)
if [ $STR -gt 0 ]; then
        echo "Kernel has reserved memory for kdump kernel..."
	echo " "
else
        echo "Kernel DOES NOT have reserved memory for kdump kernel..."
	echo "Please add appropriate crashkernel parameter to kernel cmdline"
	echo " "
        let "ERRORS++"
fi

if [ $ERRORS -gt 0 ]; then
	echo "ERRORS FOUND. Fix them and run setup again."	
	exit
fi 

echo "Prequisites are MET..."

#
# Create the config file
#

echo "Creating config file..."

# Base test directory
TEST_BASE_DIR=`pwd`
echo "TEST_BASE_DIR=$TEST_BASE_DIR" >> $CONFIG_FILE

# Where to put the results
echo "RESULTS_DIR=\$TEST_BASE_DIR/results" >> $CONFIG_FILE

# What crash commands to run during verify
echo "CRASH_CMDS=\$TEST_BASE_DIR/crash_cmds" >> $CONFIG_FILE

# Where vmlinux with debug symbols is located
echo "VMLINUX_DEBUG=$VMLINUX_DEBUG" >> $CONFIG_FILE

# Where to put vmcore
if [ $DISTRO_RH -eq 1 ]; then
echo "VMCORE=/var/crash" >> $CONFIG_FILE
fi

if [ $DISTRO_SUSE -eq 1 ]; then
echo "VMCORE=\$TEST_BASE_DIR/vmcore" >> $CONFIG_FILE
fi

# What tests to run
echo "TESTFILE=\$TEST_BASE_DIR/testfile" >> $CONFIG_FILE

# Scripts
echo "VERIFY_SCR=\$TEST_BASE_DIR/verify" >> $CONFIG_FILE
echo "TEST_SCR=\$TEST_BASE_DIR/test" >> $CONFIG_FILE
echo "SYSINFO_SCR=\$TEST_BASE_DIR/sysinfo" >> $CONFIG_FILE

# Tools
echo "CRASHER_MOD=\$TEST_BASE_DIR/tools/crasher_mod/crasher.ko" >> $CONFIG_FILE
if [ $DISTRO_SUSE -eq 1 ]; then
echo "TTUTILS_BIN=\$TEST_BASE_DIR/tools/dtt_tools/ttutils" >> $CONFIG_FILE
echo "MEMDRAIN_BIN=\$TEST_BASE_DIR/tools/dtt_tools/helper/memdrain" >> $CONFIG_FILE
echo "SETUID_BIN=\$TEST_BASE_DIR/tools/dtt_tools/helper/setuid" >> $CONFIG_FILE
echo "ALARM_BIN=\$TEST_BASE_DIR/tools/dtt_tools/helper/alarm" >> $CONFIG_FILE
echo "BRK_BIN=\$TEST_BASE_DIR/tools/dtt_tools/helper/brk" >> $CONFIG_FILE
fi

if [ $DISTRO_RH -eq 1 ]; then
echo "LKDTM=\$TEST_BASE_DIR/tools/lkdtm_mod/lkdtm.ko" >> $CONFIG_FILE
fi

#
# Create the control file
#
echo "Creating control file..."
echo "CUR_TEST=0" > $CONTROL_FILE
echo "PREV_TEST=-1" >> $CONTROL_FILE
echo "ITERATION=0" >> $CONTROL_FILE

#
# Create the crontab file
#
echo "Creating crontab file..."
echo "SHELL=/bin/sh" > $CRONTAB_FILE
echo "PATH=/usr/bin:/usr/sbin:/sbin:/bin" >> $CRONTAB_FILE
echo "MAILTO=root" >> $CRONTAB_FILE
echo "@reboot cd $TEST_BASE_DIR; ./master run" >> $CRONTAB_FILE
crontab $CRONTAB_FILE

if [ $DISTRO_SUSE -eq 1 ]; then
#
# Editing /etc/sysconfig/kdump
#
	echo "Updating $SYSCONFIG_FILE with values required for test..."
	if [ ! -f ${SYSCONFIG_FILE}.orig ]; then
        	cp $SYSCONFIG_FILE ${SYSCONFIG_FILE}.orig
	fi
	echo "" >> $SYSCONFIG_FILE
	echo "#" >> $SYSCONFIG_FILE
	echo "# Adding values required by test" >> $SYSCONFIG_FILE
	echo "#" >> $SYSCONFIG_FILE
	echo "KDUMP_TRANSFER=\"cp /proc/vmcore $TEST_BASE_DIR/vmcore\"" >> $SYSCONFIG_FILE
	echo "KDUMP_RUNLEVEL=\"2\"" >> $SYSCONFIG_FILE
	echo "KDUMP_IMMEDIATE_REBOOT=\"yes\"" >> $SYSCONFIG_FILE
fi

# Make sure the kdump service is on and
# the kdump kernel is loaded
echo "Activating kdump service..."
chkconfig kdump on
echo "Making sure kdump kernel can be loaded..."
service kdump restart

#echo "Activating cron..."
if [ $DISTRO_SUSE -eq 1 ]; then
chkconfig cron on
service cron start
fi

if [ $DISTRO_RH -eq 1 ]; then
chkconfig crond on
service crond restart
fi
