#!/bin/ksh
#
#
# Copyright (C) Comunicacion Integral, Las Palmas de Gran Canaria, 1997
#
#
#
# jDiagnostics v0.1a
#
jLOCAL=/usr/local
jENV=$HOME/JALEO-ENV
jDEV=$jENV/etc/devices
jBIN=$jENV/bin

jRAWDEV=$jDEV/Raw.dev
jXFSDEV=$jDEV/Xfs.dev

jJALEORC=$jENV/.jaleorc

jDAEMON=/usr/lib/elm/jaleo/jaleolicd

jFILE=jDIAGNOSTICS-OUTPUT
jMAIL=support@jaleo.idecnet.com
jSEPARATOR="================================================================================"
jTMP=$HOME/jtmp
jTRASHFILE=jDiagnostics_Trash_File.tmp
WHOAMI=`whoami`
VERSION="0.3"

#
#	FUNCTIONS
#

if [[ -r $jXFSDEV ]]						# This test is to avoid a little
then								# error generated by the system
	getPath=`cat $jXFSDEV | grep PATH | awk '{ print $2 }'`	# if the Xfs.dev file doesn't exist.
fi

function printFiles
{
	/sbin/cat $jTMP/$jFILE | lp
}

function cleanUp
{
	rm -f $getPath/$jTRASHFILE
}

#--------------------------------------------------
#	Start here				  #
#--------------------------------------------------

clear

if [ "$1" = "-h" ]
then
    echo 
    echo "NAME"
    echo "        `basename $0`   Jaleo Diagnostics Tool $VERSION"
    echo 
    echo "SYNOPSIS"
    echo "\t`basename $0` [-h]"
    echo 
    echo "DESCRIPTION"
    echo "\tThis script diagnoses the system where Jaleo resides"
    echo "\tby collecting different information. Optionally, it allows "
    echo "\tsending the report to $jMAIL"
    echo 
    echo "\t`basename $0` must be executed as root."
    echo "\tPlease login as the Jaleo User and issue a 'su root' command.\n"
    echo "\tNote that this utility only makes sense for XFS systems "
    echo "\tand for Jaleo products from version 2.7"
    echo 
    echo "\tThe following information will be retrieved from your system:"
    echo
    echo "\tThe current date (date)"
    echo "\tHardware inventory (hinv)"
    echo "\tConfiguration status (chkconfig)"
    echo "\tComplete system identification (uname -aR)"
    echo "\tThe unique system identifier (sysinfo -s)"
    echo "\tInstalled versions by internal number (versions -nb)"
    echo "\tStatus of all the swap areas (swap -l)"
    echo "\tLicense daemon status (ps -elf | grep jaleolicd; jlicver /usr/lib/elm/jaleo/jaleolicd) "
    echo "\tCurrent Jaleo licenses (ls -l /usr/lib/elm/jaleo)"
    echo "\tContents of your .jaleorc file"
    echo "\tContents of all your files under '~/JALEO-ENV/config' directory"
    echo "\tContents of your Xfs.dev file"
    echo "\tIndex of $jLOCAL"
    echo "\tOutput of the following commands:"
    echo "\t    jaleo -V"
    echo "\t    xfstest (write)   | "
    echo "\t    xfstest (read)    | on Xfs Systems"
    echo "\tConsole output, if an error occurs"
    echo
    echo "OPTIONS"
    echo "        [-h]   Prints this help message and quits."
    echo
    exit 0
fi

if [ $WHOAMI != root ]
then
    echo "\n Please execute this utility as Super-User."
    echo " Login as the Jaleo User and issue a 'su root' command."
    echo " Type jDiagnostics -h for more detailed help.\n"
    exit 0
fi

	echo "\nJaleo Diagnostics Tool $VERSION"
	echo "---------------------------"
	echo "\nNote: This utility only makes sense for XFS systems"
	echo "      and for Jaleo products from version 2.7"
	echo "      Type 'jDiagnostics -h' for more detailed information.\n"
	echo "\nWith this script, the following information will be retrieved"
	echo "from your system:\n"
	echo "\t- The current date (date)"
	echo "\t- Hardware inventory (hinv)"
	echo "\t- Configuration status (chkconfig)"
	echo "\t- Complete system identification (uname -aR)"
	echo "\t- The unique system identifier (sysinfo -s)"
	echo "\t- Installed versions by internal number (versions -nb)"
	echo "\t- Status of all the swap areas (swap -l)"
	echo "\t- License daemon status (ps -elf | grep jaleolicd; jlicver /usr/lib/elm/jaleo/jaleolicd)"
	echo "\t- Current Jaleo licenses (ls -l /usr/lib/elm/jaleo)"
	echo "\t- Contents of your .jaleorc file"
	echo "\t- Contents of all your files under ~/JALEO-ENV/config/ directory"
	echo "\t- Contents of your Xfs.dev file"
	echo "\t- Index of $jLOCAL"
	echo "\t- Output of the following commands:"
	echo "\t   * jaleo -V"
	echo "\t   * xfstest (write)   | "
	echo "\t   * xfstest (read)    | on Xfs Systems"
	echo "\t- Console output, if an error occurs\n"
	echo "Please hit <Return> to continue or <Ctrl-C> to stop\c"
	read dummy < /dev/tty

	if [ ! -r $jTMP ]
	then
		echo "\nCreating temporary working directory..."
		mkdir $jTMP
		chmod 777 $jTMP
	else
		echo "\nTemporary working directory already exists..."
	fi

	echo "\nGetting the current date... (date)"
	echo "\nDATE\n" > $jTMP/$jFILE 
	/bin/date >> $jTMP/$jFILE
	echo $jSEPARATOR >> $jTMP/$jFILE

	echo "Getting hardware inventory... (hinv)"
	echo "\nHARDWARE INVENTORY\n" >> $jTMP/$jFILE 
	/bin/hinv >> $jTMP/$jFILE
	echo $jSEPARATOR >> $jTMP/$jFILE

	echo "Getting configuration status... (chkconfig)"
	echo "\nSYSTEM CONFIGURATION\n" >> $jTMP/$jFILE
	/sbin/chkconfig >> $jTMP/$jFILE
	echo $jSEPARATOR >> $jTMP/$jFILE

	echo "Identifying your system... (uname -aR)"
	echo "\nSYSTEM ID\n" >> $jTMP/$jFILE
	/bin/uname -aR >> $jTMP/$jFILE
	echo $jSEPARATOR >> $jTMP/$jFILE

	echo "Getting the unique system identifier... (sysinfo -s)"
	echo "\nUNIQUE SYSTEM IDENTIFIER\n" >> $jTMP/$jFILE 
	/sbin/sysinfo -s >> $jTMP/$jFILE
	echo $jSEPARATOR >> $jTMP/$jFILE

	echo "Creating list of installed versions... (versions -nb)"
	echo "\nINSTALLED VERSIONS\n" >> $jTMP/$jFILE
	/usr/sbin/versions -nb >> $jTMP/$jFILE
	echo $jSEPARATOR >> $jTMP/$jFILE

	echo "Checking swap areas... (swap -l)"
	echo "\nSWAP AREAS\n" >> $jTMP/$jFILE
	/sbin/swap -l >>  $jTMP/$jFILE
	echo $jSEPARATOR >> $jTMP/$jFILE

	echo "Getting the license daemon status... (ps -elf | grep jaleolicd; jlicver /usr/lib/elm/jaleo/jaleolicd)"
	echo "\nLICENSE DAEMON STATUS\n" >> $jTMP/$jFILE 
	ps -el | grep jaleolicd >> /dev/null
	if [[ $? -eq 0 ]]
	then
	    echo "The license daemon is running on this machine.">> $jTMP/$jFILE
	else
	    echo "The license daemon is not running on this machine.">> $jTMP/$jFILE
	fi
	if [[ -r $jDAEMON ]]
	then
	    echo "The license daemon file was found in this system.">> $jTMP/$jFILE
	    echo "The license daemon version is:">> $jTMP/$jFILE
	    ~/JALEO-ENV/extern/license/jlicver $jDAEMON >> $jTMP/$jFILE
	else
	    echo "The license daemon file was not found in this system.">> $jTMP/$jFILE
	fi
	echo $jSEPARATOR >> $jTMP/$jFILE

	echo "Getting Jaleo current licenses... (ls -l /usr/lib/elm/jaleo)"
	echo "\nCURRENT LICENSES:\n" >> $jTMP/$jFILE 
	ls -l /usr/lib/elm/jaleo >> $jTMP/$jFILE
	echo $jSEPARATOR >> $jTMP/$jFILE

	echo "Processing $jJALEORC..."
	echo "\nJALEORC FILE\n" >> $jTMP/$jFILE
	if [[ -r $jJALEORC ]]
	then
		cat $jJALEORC  >> $jTMP/$jFILE
	else
		echo "$jJALEORC was not found on this system (default configuration)."
		echo "$jJALEORC was not found on this system (default configuration)." >> $jTMP/$jFILE
	fi

	# Note that in this case the ls command shows all the path from current point.
	ls $jENV/config/.??* |
	while read file
	do
	    echo $jSEPARATOR >> $jTMP/$jFILE
	    echo "Processing ${file}..."
	    echo "\n'${file}' FILE\n" >> $jTMP/$jFILE
	    if [[ -r ${file} ]]
	    then
		cat ${file} >> $jTMP/$jFILE
	    else
		echo "${file} was not found on this system (default configuration)."
		echo "${file} was not found on this system (default configuration)." >> $jTMP/$jFILE
	    fi
	done

	# Note that in this case the ls command shows only the files inside this directory. This directory must not  keep directories.
	ls $jENV/config/ |
	while read file
	do
	    echo $jSEPARATOR >> $jTMP/$jFILE
	    echo "Processing ${jENV}/config/${file}..."
	    echo "\n${file} FILE\n" >> $jTMP/$jFILE
	    if [[ -r ${jENV}/config/${file} ]]
	    then
		cat ${jENV}/config/${file} >> $jTMP/$jFILE
	    else
		echo "${jENV}/config/${file} was not found on this system (default configuration)."
		echo "${jENV}/config/${file} was not found on this system (default configuration)." >> $jTMP/$jFILE
	    fi
	done

	echo $jSEPARATOR >> $jTMP/$jFILE
	echo "Processing $jXFSDEV..."
	echo "\nXFS_DEVICE SETTINGS\n" >> $jTMP/$jFILE
	cat $jXFSDEV >> $jTMP/$jFILE

	echo $jSEPARATOR >> $jTMP/$jFILE
	echo "Processing $jLOCAL..."
	echo "\nCONTENTS OF $jLOCAL \n" >> $jTMP/$jFILE
	/bin/ls -l $jLOCAL  >> $jTMP/$jFILE
	echo $jSEPARATOR >> $jTMP/$jFILE

#
# Test Jaleo itself...
#
	echo "Identifying your Jaleo version..."
	echo "\nJALEO VERSION\n" >> $jTMP/$jFILE 
	${jBIN}/jaleo -V >> $jTMP/$jFILE
	echo $jSEPARATOR >> $jTMP/$jFILE

#
#	XFS Tests take place...
#
	if [[ -r $jXFSDEV ]]
	then
		echo "XFS detected. Running tests..."
		echo "XFS detected. Running tests..." >> $jTMP/$jFILE
		echo "\nDo you have a PAL or NTSC system ?"
		echo "Please type:\n"
		echo "\tP - for PAL Systems."
		echo "\tN - for NTSC Systems."
		echo "\nEnter your choice > \c"
		read system < /dev/tty
	
		echo "\nThe $getPath directory must be local to the system where Jaleo is installed."
		echo "Do not mount it as a Network File System (NFS)\n"

		case $system in
		"N" | "n" | "NTSC" | "ntsc")
		    echo "NTSC System selected"
		    echo "NTSC System selected" >> $jTMP/$jFILE
		    if [ ! -r $jENV/utils/xfstest ]
		    then
			ls -l $jENV/utils >> $jTMP/$jFILE
		    fi
		    echo "Writing 1000 images to temporary file on $getPath..."
		    $jENV/utils/xfstest -x 720 -y 486 -b 2 -n 1000 -w -d $getPath/$jTRASHFILE
		    $jENV/utils/xfstest -x 720 -y 486 -b 2 -n 1000 -w -d $getPath/$jTRASHFILE >> $jTMP/$jFILE
		    echo "Reading 1000 images from temporary file on $getPath..."
		    $jENV/utils/xfstest -x 720 -y 486 -b 2 -n 1000 -d $getPath/$jTRASHFILE
		    $jENV/utils/xfstest -x 720 -y 486 -b 2 -n 1000 -d $getPath/$jTRASHFILE >> $jTMP/$jFILE
		    echo $jSEPARATOR >> $jTMP/$jFILE ;;

		"P" | "p" | "PAL" | "pal" )
		    echo "PAL System selected"
		    echo "PAL System selected" >> $jTMP/$jFILE
		    if [ ! -r $jENV/utils/xfstest ]
		    then
			ls -l $jENV/utils >> $jTMP/$jFILE
		    fi
		    echo "Writing 1000 images to temporary file $getPath..."
		    $jENV/utils/xfstest -x 720 -y 576 -b 2 -n 1000 -w -d $getPath/$jTRASHFILE
		    $jENV/utils/xfstest -x 720 -y 576 -b 2 -n 1000 -w -d $getPath/$jTRASHFILE >> $jTMP/$jFILE
		    echo "Reading 1000 images from temporary file on $getPath..."
		    $jENV/utils/xfstest -x 720 -y 576 -b 2 -n 1000 -d $getPath/$jTRASHFILE
		    $jENV/utils/xfstest -x 720 -y 576 -b 2 -n 1000 -d $getPath/$jTRASHFILE >> $jTMP/$jFILE
		    echo $jSEPARATOR >> $jTMP/$jFILE ;;
		*)
		    echo "\aWARNING - XFS tests skipped. You need to enter a system type [$system]." 
		    echo "\aWARNING - XFS tests skipped. You need to enter a system type [$system].\n" >> $jTMP/$jFILE ;;
		esac
	else
		echo "$jXFSDEV was not found on this system."
		echo "\n$jXFSDEV was not found on this system." >> $jTMP/$jFILE
	fi

	echo "\nIf you have an e-mail system you may wish to send the"
	echo "resulting file jDIAGNOSTICS-OUTPUT now.\n"
	echo "Do you want to send now the file ([y]/n) ? \c"
	read yesno < /dev/tty
	if [ ${yesno} = n ]
	then
		echo "\nThe resulting file $jFILE will remain under $jTMP.\n"
		echo "Please type:\n"
		echo "\tP - to print jDIAGNOSTICS-OUTPUT"
		echo "\tV - to view jDIAGNOSTICS-OUTPUT "
		echo "\tQ - to quit the application."
		echo "\nEnter your choice > \c"
		read choice < /dev/tty
		case $choice in
		"p" | "p")
				echo "\nPrinting diagnostic file. Please wait...\n"
				printFiles ;;
		"V" | "v")
				echo "\nContents of jDIAGNOSTICS-OUTPUT: "
				cat $jTMP/$jFILE | more ;;
		"Q" | "q")
				echo "\nCleaning up and exiting..."
				cleanUp
				exit 0 ;;
		*)
				echo "\nCleaning up and exiting..."
				cleanUp
				exit 0 ;;
		esac
				cleanUp
				exit 0
	else
		echo "\nThe default destination email address is support@jaleo.idecnet.com"
		echo "Press [RETURN] once more to confirm and send the file or press CTRL-C"
		echo "to interrupt the application"
		read emailme < /dev/tty
		/usr/bsd/uuencode $jTMP/$jFILE $jTMP/$jFILE | 
		Mail -s "jDiagnostics Results" $jMAIL

		echo "\nThe resulting file $jFILE will remain under $jTMP.\n"
		echo "E-Mail on the way!." 
		echo "jDiagnostics $VERSION finished."
		echo "Exiting...\n"
		cleanUp
		exit 0
	fi
