#!/bin/sh
# Function to kill a telnet session
LOC_FILE="/tmp/wout.`date +\"%s\"`"
TR=/usr/bin/tr
CUT=/usr/bin/cut
killSession() {
	idx=$1
	printf "Collecting process information... "
	/sbin/fuser -v /dev/` echo ${a2[idx]} |  /bin/sed 's/ //g'`>${LOC_FILE}
	fuserResult=$?
	if [ $fuserResult -ne 0 ]
	then
		printf "\nPermission Denied! Bailing Out!\n"
		exit
	fi
	printf "Done.\n"
	printf "
	You have opted to terminate the telnet session:-
		logged in as \"%s\", from \"%s\"
		since \"%s\" and has been inactive for \"%s\",
		the current command executed being: \"%s\".
		The device entry is: \"%s\".
" "${a1[$idx]}"  "${a3[$idx]}" "${a4[$idx]}"  "${a5[$idx]}" "${a8[$idx]}" "${a2[$idx]}"
#	printf "
#%s=%s %s=%s %s=%s %s=%s
#%s=%s %s=%s %s=%s %s=%s
#	" $h1 ${a1[$idx]}   $h2 ${a2[$idx]}   $h3 ${a3[$idx]}   $h4 ${a4[$idx]}   $h5 ${a5[$idx]}   $h6 ${a6[$idx]}   $h7 ${a7[$idx]}   $h8 ${a8[$idx]}
	printf "

	This action will effectively kill these process(es):-"
#
#	ps -lt /dev/` echo ${a2[idx]} |  /bin/sed 's/ //g'`
#
#	/sbin/fuser -v /dev/` echo ${a2[idx]} |  /bin/sed 's/ //g'`
	/bin/cat ${LOC_FILE}
	if [ $fuserResult -ne 0 ]
	then
		printf "\nPermission Denied! Bailing Out!\n"
		exit
	else
		printf "Please Ensure (Y/[N]): "
		read choice
		if [ X${choice} = Xy -o \
			X${choice} = XY -o \
			X${choice} =  Xyes -o \
			X${choice} =  XYes -o \
			X${choice} = XYES ]
		then
			printf "killing session...."
			/sbin/fuser -k -9 /dev/` echo ${a2[idx]} |  /bin/sed 's/ //g'` 1>/dev/null 2>/dev/null
			printf "  Done!\n"
		else
			printf "NOT confirmed - No action taken\n"
		fi
	fi
}

# Function to list a list of telnet sessions showing marks, if any
listTelnetSessions() {
	if [ $telnetSessionsCount -eq 0 ]
	then
		printf "No telnet sessions found!\n"
		exit
	fi
	printf "\n\n                   List of telnet sessions (%d found)\n" \
		$telnetSessionsCount
	printf "______________________________________________________________________________\n"
	printf " Session No   %-12.12s %-12.12s %-12.12s %-16.16s %-6.12s\n"  \
			"${h1}" \
			"${h2}" \
			"${h5}" \
			"${h3}" \
			"${h4}"
	printf "~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~\n"
	idx=0
	while [ $idx -lt $telnetSessionsCount ]
	do
		printf " %5d        %-12.12s %-12.12s %-12.12s %-16.16s %-6.12s\n" \
			 $idx  \
			"${a1[idx]}" \
			"${a2[idx]}" \
			"${a5[idx]}" \
			"${a3[idx]}" \
			"${a4[idx]}"
		#printf "	[%s]%d: %s\n" "${a_sel[${idx}]}" $idx ${telnetSessionArry[$idx]}
		idx=`/fabos/bin/expr ${idx} + 1`
	done
	printf "~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~\n"
}

#
# Function to get the telnet sessions and store them in an arry
#

getTelnetSessions() {
printf "\nCollecting login information...."
idx=0
skip=1
/usr/bin/w > ${LOC_FILE}

IFS=
while [ 1 ]
do
	# earlier it was a simple read: read w1 w2 w3 w4 w5 w6 w7 w8 w9 w10
	read W
	w1=`echo ${W} | ${CUT} -c1-9   | ${TR} -s ' '`
	w2=`echo ${W} | ${CUT} -c10-17 | ${TR} -s ' '`
	w3=`echo ${W} | ${CUT} -c19-34 | ${TR} -s ' '`
	w4=`echo ${W} | ${CUT} -c36-42 | ${TR} -s ' '`
	w5=`echo ${W} | ${CUT} -c44-50 | ${TR} -s ' '`
	w6=`echo ${W} | ${CUT} -c52-57 | ${TR} -s ' '`
	w7=`echo ${W} | ${CUT} -c59-64 | ${TR} -s ' '`
	w8=`echo ${W} | ${CUT} -c66-              `
	if [ $skip -eq 1 ]
	then
		skip=2
		continue
	fi
	if [ $skip -eq 2 ]
	then
		skip=3
		h1="$w1"  h2="$w2"  h3="$w3"  h4="$w4"  h5="$w5"  h6="$w6"  h7="$w7"  h8="$w8"  h9="$w9"  h10="$w10"  
		continue
	fi
	if [ -z "$w1" ]
	then
		break
	fi      
	a1[idx]="$w1"  a2[idx]="$w2"  a3[idx]="$w3"  a4[idx]="$w4"  a5[idx]="$w5"  a6[idx]="$w6"  a7[idx]="$w7"  a8[idx]="$w8"  a9[idx]="$w9"  a10[idx]="$w10"  
	a_sel[idx]=" "
	idx=`/fabos/bin/expr ${idx} + 1`
done < ${LOC_FILE}
	telnetSessionsCount=$idx
printf "Done\n"
}


# main starts here


# keep bugging the user until he decides to quit
while [ 1 ]
do
	getTelnetSessions
	listTelnetSessions
	printf "
 Enter Session Number to terminate (q to quit) "
	read choice
	if [ "$choice" = "q" -o  "$choice" = "Q" ]
	then
		exit
	fi
	if [ -z "$choice" ]
	then
		choice=invalid
	fi
	nchoice=`echo $choice | ${TR} -cd "[0-9]\n"`
	if [ "$nchoice" != "$choice" ]
	then
		printf "Please enter a number\n"
	else
		if [ $nchoice -lt 0 -o $nchoice -ge $telnetSessionsCount ]
		then
			printf "Please enter a value between 0 and `/fabos/bin/expr $telnetSessionsCount - 1`\n"
		else
			killSession $nchoice
		fi
	fi
done # main while
