#!/bin/sh
#
# Name: compile_debug.sh
# Copyright: (c)2004 Hewlett-Packard Company
#
# Description: toggles the debug flag in the kernel sources
#
# CD		09/14/04 Initial Devlopment
# CD		12/06/04 Add check for 2.6 kernel
# CD		02/10/05 Fix bug where script was wiping
#                        out qla_settings.h

# defines

SRCDIR=/opt/hp/src/hp_qla2x00src
DRVDIR=qla2xxx-8.00.02
SETFILE=$SRCDIR/$DRVDIR/qla_settings.h

# check to make sure we are using a 2.6 kernel

if [ "`uname -r | cut -c 1-3`" != "2.6" ]
then
 echo "This script is only supported on systems running a 2.6.x kernel"
 exit 1
fi

# determine if driver is in debug or non debug mode

if test -f $SETFILE
then
 OLDDEBUG=`cat $SETFILE | grep DEBUG_QLA2100 | awk '{print $3}'`

 if [ $OLDDEBUG -eq 0 ]
 then
	DEBUG=1
 else
	DEBUG=0
 fi
else
 echo "Cannot find $SETFILE"
 exit 1
fi

if [ $OLDDEBUG -eq 0 ]
then
 echo -n "Would you like to turn debug on (Y/n)? "
 read ANS
else
 echo -n "Would you like to turn debug off (Y/n)? "
 read ANS
fi

if [ "$ANS" = "n" ] || [ "$ANS" = "N" ]
then
 exit 0
fi

# make change to qla_settings.h

echo ""
echo "Changing DEBUG_QLA2100 to $DEBUG"
mv $SETFILE $SETFILE.old
cat $SETFILE.old | sed "s/DEBUG_QLA2100\t\t${OLDDEBUG}/DEBUG_QLA2100\t\t${DEBUG}/g" > $SETFILE

# compile new driver

echo "Compiling new driver"
cd $SRCDIR

if test -f /etc/SuSE-release
then
 ./build_driver.suse 1>/dev/null 2>/dev/null
elif test -f /etc/redhat-release
then
 ./build_driver.rh 1>/dev/null 2>/dev/null
fi
 
if [ $? -eq 0 ]
then
 echo "Driver build succeeded"
else
 echo "Driver build failed"
 exit 1
fi

# create new initrd if needed

echo ""
echo -n "Would you like to create a new initrd (Y/n)? "
read ANS
echo ""

if [ "$ANS" != "n" ] && [ "$ANS" != "N" ]
then
 echo "Building new initrd"
 ./edit_conf 1>/dev/null 2>/dev/null
 ./make_initrd 1>/dev/null 2>/dev/null

 if [ $? -ne 0 ]
 then
	echo "Initrd build failed.  Check configuration files and then rerun the $SRCDIR/make_initrd"
 fi
fi

# Reload modules

echo ""
echo "You will have to reload the QLogic driver modules for the changes to"
echo "take effect!"
