#!/bin/bash
#
#****************************************************************************
#
#  @(#) $Id: dgap_view,v 1.6 2004/06/24 19:09:36 scottk Exp $
#
# 
#****************************************************************************
#
#  Copyright (c) 2003 Digi International Inc., All Rights Reserved
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2, or (at your option)
# any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY, EXPRESS OR IMPLIED; without even the
# implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
# PURPOSE.  See the GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License  
# along with this program; if not, write to the Free Software
# Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
# 
# 
#****************************************************************************

# Uncomment the next line if this script is ever needed to be debugged.
#set -x

# Force path to include our directory, in case we need our "dialog" binary.
PATH=/etc/dgap:$PATH

# Point to our empty dialogrc file
DIALOGRC=/etc/dgap/dialogrc
export DIALOGRC

# Capture original directory
INSTALLDIR=`pwd`

# Default refresh rate is 5 seconds.
REFRESH_RATE=5

# Default board is 0
BOARD=0

# Number of boards is 0
TOTAL_BOARDS=0

# Display Port offset starts at 0
PORT_OFFSET=0

# Display Page Max Number of Ports
DISPLAY_PAGE_MAX=16

# Display Page Size starts at 0
DISPLAY_PAGE_SIZE=0


# Various hex values of c/i/o/l flags
let "CSIZE = 0x60"
let "CS5 = 0x00"
let "CS6 = 0x10"
let "CS7 = 0x20"
let "CS8 = 0x30"
let "CSTOPB = 0x40"
let "PARENB = 0x100"
let "PARODD = 0x200"
let "HUPCL = 0x400"
let "IGNBRK = 0x1"
let "BRKINT = 0x2"

display_stats()
{
	rm -f /tmp/dgap.tmp2

	printf "%30s              %25s\n" "Received Statistics" "Transmitted Statistics" >> /tmp/dgap.tmp2
	printf "%5s %10s %6s %5s %5s %5s %10s\n\n" "Port" "Total" "Parity" "Break" "Frame" "Orun" "Total" >> /tmp/dgap.tmp2

	read rest

	while read n rx par brk frm ovr tx rest
	do
		if [ $n -lt $PORT_OFFSET ]
		then
			continue
		fi
		n=`expr $n + 1`

		DISPLAY_PAGE_SIZE=`expr $DISPLAY_PAGE_SIZE + 1`
		if [ $DISPLAY_PAGE_SIZE -gt $DISPLAY_PAGE_MAX ]
		then
			DISPLAY_PAGE_SIZE=`expr $DISPLAY_PAGE_SIZE - 1`
			continue
		fi

		printf "%5d %10d %6d %5d %5d %5d %10d\n" \
			$n $rx $par $brk $frm $ovr $tx >> /tmp/dgap.tmp2
	done

}


display_flags()
{
	rm -f /tmp/dgap.tmp

	printf "%5s %6s %8s %6s %5s %10s %24s\n\n" "Port" "Speed" "Parity" "Chars" "Stop" "Break" "RTS CTS DTR DSR DCD RI" >> /tmp/dgap.tmp

	read rest

	while read n cd id od ld dd b open rts cts dtr dsr dcd ri rest
	do
		if [ $n -lt $PORT_OFFSET ]
		then
			continue
		fi

		n=`expr $n + 1`

		DISPLAY_PAGE_SIZE=`expr $DISPLAY_PAGE_SIZE + 1`
		if [ $DISPLAY_PAGE_SIZE -gt $DISPLAY_PAGE_MAX ]
		then
			DISPLAY_PAGE_SIZE=`expr $DISPLAY_PAGE_SIZE - 1`
			continue
		fi

		let "c=0x$cd"
		let "i=0x$id"
		let "o=0x$od"
		let "l=0x$ld"
		let "d=0x$dd"
		port=$n

		if [ "$open" = "op" ]
		then
			let "par=$c & PARENB"

			if [ $par -eq 0 ]
			then
				partext="None"
			else
				let "par=$c & PARODD"
				if [ $par -eq 0 ]
				then
					partext="Even"
				else
					partext="Odd"
				fi
			fi

			let "csize=$c & CSIZE"
			let "cbits=$csize & CS8"

			if [ $cbits -ne 0 ]
			then
				bitstext="8"
			fi
			let "cbits=$csize & CS7"
			if [ -z "$bitstext" -a $cbits -ne 0 ]
			then
				bitstext="7"
			fi
			let "cbits=$csize & CS6"
			if [ -z "$bitstext" -a $cbits -ne 0 ]
			then
				bitstext="6"
			fi
			if [ -z "$bitstext" ]
			then
				bitstext="5"
			fi

			let "cstopb=$c & CSTOPB"
			if [ $cstopb -ne 0 ]
			then
				cstopbtext="2"
			else
				cstopbtext="1"
			fi

			let "breakb=$i & IGNBRK"
			if [ $breakb -ne 0 ]
			then
				breakbtext="Disabled"
			fi

			let "breakb=$i & BRKINT"
			if [ -z "$breakbtext" -a $breakb -ne 0 ]
			then
				breakbtext="Enabled"
			else
				if [ -z "$breakbtext" ]
				then
					breakbtext="Disabled"
				fi
			fi

			if [ "$rts" = "--" ]
			then
				rts=" _ "
			else
				rts=" # "
			fi
			if [ "$cts" = "--" ]
			then
				cts=" - "
			else
				cts=" # "
			fi
			if [ "$dtr" = "--" ]
			then
				dtr=" - "
			else
				dtr=" # "
			fi
			if [ "$dsr" = "--" ]
			then
				dsr=" - "
			else
				dsr=" # "
			fi
			if [ "$dcd" = "--" ]
			then
				dcd=" - "
			else
				dcd=" # "
			fi
			if [ "$ri" = "--" ]
			then
				ri=" - "
			else
				ri=" # "
			fi

		else
			partext="None"
			bitstext="0"
			cstopbtext="1"
			breakbtext="Disabled"
			b=0
			rts=" - "
			cts=" - "
			dtr=" - "
			dsr=" - "
			dcd=" - "
			ri=" - "
		fi

		printf "%5d %6s %8s %6s %5s %10s  %3s %3s %3s %3s %3s %3s\n" \
$port $b $partext $bitstext $cstopbtext $breakbtext $rts $cts $dtr $dsr $dcd $ri >> /tmp/dgap.tmp

		partext=
		bitstext=
		cstopbtext=
		breakbtext=

	done
}


get_number_of_boards()
{
	while read a b boards rest
	do
		printf $boards
		return
	done
}

get_number_of_ports()
{
	while read a b c d ports rest
	do
		printf $ports
		return
	done
}



# The display hardware screen
display_screen()
{
	while [ 0 ]; do

	dialog --backtitle "DGAP VIEW" --menu "View Screen" 15 $detected_cols 4 \
	1 "View Hardware Statistics" \
	2 "View Hardware Settings" \
	R "Select Refresh Rate for Dialogs" \
	G "Go Back" \
	2>/tmp/dgap.ans
	if [ $? = 1 ]
	then
		return
	else
		ans=`tail -1 /tmp/dgap.ans`
		rm -f /tmp/dgap.ans
	fi

	case $ans in
	1)
		in_refresh=0
		while [ 0 ]; do

			if [ -f /proc/dgap/${BOARD}/info ]
			then
				grep "Number of Ports" /proc/dgap/${BOARD}/info >/tmp/dgap.tmp
				TOTAL_PORTS=`get_number_of_ports < /tmp/dgap.tmp`
				rm -f /tmp/dgap.tmp
			fi

			if [ $TOTAL_PORTS -gt 16 -a $in_refresh -ne 1 ]
			then
                	        rm -f /tmp/dgap.tmp
				tmpi=1
				tmpcount=1
				while [ $tmpi -le $TOTAL_PORTS ]
				do
					tmpj=`expr $tmpj + 16`
					if [ $tmpj -gt $TOTAL_PORTS ]
					then
						tmpj=$TOTAL_PORTS
					fi
					printf " $tmpcount Ports_${tmpi}-${tmpj}" >> /tmp/dgap.tmp
					tmpi=`expr $tmpi + 16`
					tmpcount=`expr $tmpcount + 1`
				done

				printf " G Go_Back" >> /tmp/dgap.tmp

                	        tmpstring=`cat /tmp/dgap.tmp`
                	        rm -f /tmp/dgap.tmp

				dialog --backtitle "Select Port Range" --nocancel --menu "Select Port Range" 15 $detected_cols 4 \
				$tmpstring 2> /tmp/dgap.ans

				if [ $? = 1 ]
				then
					return
				else
					ans=`tail -1 /tmp/dgap.ans`
					rm -f /tmp/dgap.ans
				fi

				if [ "$ans" = "G" ]
				then
					break
				fi

				ans=`expr $ans - 1`
				PORT_OFFSET=`expr $ans \* 16`
			fi

			DISPLAY_PAGE_SIZE=0
			cat /proc/dgap/$BOARD/stats >/tmp/dgap.out
			display_stats < /tmp/dgap.out
			new_stats=`cat /tmp/dgap.tmp2`
			DISPLAY_PAGE_SIZE=`expr $DISPLAY_PAGE_SIZE + 7`
			if [ $collapse_supported -eq 1 ]
			then
				dialog --backtitle "Display Hardware Statistics" --no-collapse --timeout $REFRESH_RATE --msgbox "$new_stats" $DISPLAY_PAGE_SIZE $detected_cols 2>/dev/null
			else
				dialog --backtitle "Display Hardware Statistics" --timeout $REFRESH_RATE --msgbox "$new_stats" $DISPLAY_PAGE_SIZE $detected_cols 2>/dev/null
			fi

			if [ $? = 0 ]
			then
				if [ $TOTAL_PORTS -gt 16 ]
				then
					in_refresh=0
					continue
				else
					in_refresh=0
					break
				fi
			fi
			in_refresh=1
		done
		;;
	2)
		in_refresh=0
		while [ 0 ]; do

			if [ -f /proc/dgap/${BOARD}/info ]
			then
				grep "Number of Ports" /proc/dgap/${BOARD}/info >/tmp/dgap.tmp
				TOTAL_PORTS=`get_number_of_ports < /tmp/dgap.tmp`
				rm -f /tmp/dgap.tmp
			fi

			if [ $TOTAL_PORTS -gt 16 -a $in_refresh -ne 1 ]
			then
                	        rm -f /tmp/dgap.tmp
				tmpi=1
				tmpcount=1
				while [ $tmpi -le $TOTAL_PORTS ]
				do
					tmpj=`expr $tmpj + 16`
					if [ $tmpj -gt $TOTAL_PORTS ]
					then
						tmpj=$TOTAL_PORTS
					fi
					printf " $tmpcount Ports_${tmpi}-${tmpj}" >> /tmp/dgap.tmp
					tmpi=`expr $tmpi + 16`
					tmpcount=`expr $tmpcount + 1`
				done

				printf " G Go_Back" >> /tmp/dgap.tmp

                	        tmpstring=`cat /tmp/dgap.tmp`
                	        rm -f /tmp/dgap.tmp

				dialog --backtitle "Select Port Range" --nocancel --menu "Select Port Range" 15 $detected_cols 4 \
				$tmpstring 2> /tmp/dgap.ans

				if [ $? = 1 ]
				then
					return
				else
					ans=`tail -1 /tmp/dgap.ans`
					rm -f /tmp/dgap.ans
				fi

				if [ "$ans" = "G" ]
				then
					break
				fi

				ans=`expr $ans - 1`
				PORT_OFFSET=`expr $ans \* 16`

			fi

			DISPLAY_PAGE_SIZE=0
			cat /proc/dgap/$BOARD/flags > /tmp/dgap.out
			display_flags < /tmp/dgap.out
			new_flags=`cat /tmp/dgap.tmp`
			DISPLAY_PAGE_SIZE=`expr $DISPLAY_PAGE_SIZE + 7`
			if [ $collapse_supported -eq 1 ]
			then
				dialog --backtitle "Display HardWare Settings" --no-collapse --timeout $REFRESH_RATE --msgbox "$new_flags" $DISPLAY_PAGE_SIZE $detected_cols 2>/dev/null
			else
				dialog --backtitle "Display HardWare Settings" --timeout $REFRESH_RATE --msgbox "$new_flags" $DISPLAY_PAGE_SIZE $detected_cols 2>/dev/null
			fi
			if [ $? = 0 ]
			then
				if [ $TOTAL_PORTS -gt 16 ]
				then
					in_refresh=0
					continue
				else
					in_refresh=0
					break
				fi
			fi
			in_refresh=1
		done

		;;
	R)
		dialog --backtitle "DGAP VIEW" --menu "Select Refresh Rate" 21 $detected_cols 14 \
			A "Never" \
			B "1 Second" \
			C "2 Seconds" \
			D "5 Seconds" \
			E "10 Seconds" \
			F "15 Seconds" \
			G "30 Seconds" \
			H "1 Minute" \
			I "2 Minutes" \
			J "5 Minutes" \
			K "10 Minutes" \
			L "30 Minutes" \
			M "1 Hour" \
			N "Go back" \
			2>/tmp/dgap.ans

			if [ $? != 1 ]
			then
				ans=`tail -1 /tmp/dgap.ans`
				case $ans in
				A)
					REFRESH_RATE=0
					;;
				B|b)
					REFRESH_RATE=1
					;;
				C)
					REFRESH_RATE=2
					;;
				D)
					REFRESH_RATE=5
					;;
				E)
					REFRESH_RATE=10
					;;
				F)
					REFRESH_RATE=15
					;;
				G)
					REFRESH_RATE=30
					;;
				H)
					REFRESH_RATE=60
					;;
				I)
					REFRESH_RATE=120
					;;
				J)
					REFRESH_RATE=300
					;;
				K)
					REFRESH_RATE=600
					;;
				L)
					REFRESH_RATE=1800
					;;
				M)
					REFRESH_RATE=3600
					;;
				N|*)
					;;
				esac
			fi

			rm -f /tmp/dgap.ans
		;;
	G|*)
		return
		;;
	esac

	done
}


get_board_name()
{
	while read a b c rest
	do
		printf "%s_%s_%s_%s" ${rest} >/tmp/dgap.tmp2
		return
	done
}


# Create board strings
create_board_strings()
{
	tmpi=0
	rm -f /tmp/dgap.out

	while [ $tmpi -lt $TOTAL_BOARDS ]
	do
		grep "Board Name" /proc/dgap/$tmpi/info > /tmp/dgap.tmp
		get_board_name < /tmp/dgap.tmp
		rm -f /tmp.dgap.tmp
		name=`cat /tmp/dgap.tmp2`
		rm -f /tmp.dgap.tmp2
		tmpb=`expr $tmpi + 1`

		printf " $tmpi %s" $name >> /tmp/dgap.out
		tmpi=`expr $tmpi + 1`
	done
}



termtype=${TERM:-"linux"}
tput -T${termtype} clear

dialog --help 2>/tmp/dgap.tmp
mytest=`cat /tmp/dgap.tmp | grep print-maxsize | wc -l`
if [ $mytest -eq 0 ]
then
	detected_lines=23
	detected_cols=`tput cols`
	detected_cols=`expr "$detected_cols" - 4`
else
        
	# Find out how big we can make our dialog boxes
	dialog --print-maxsize 2>/tmp/dgap.tmp

	# Blah, doing --print-maxsize breaks stdin's tty settings...
	stty sane
        
	detected_lines=`cat /tmp/dgap.tmp | cut -f2 -d ' ' | tr -d ","`
	detected_cols=`cat /tmp/dgap.tmp | cut -f3 -d ' ' | tr -d ","`

	rm -f /tmp/dgap.tmp

	if [ -z $detected_lines ]
	then
		detected_lines=23
	else
		detected_lines=`expr "$detected_lines" - 2`
	fi

	if [ -z $detected_cols ]
	then
		detected_cols=`tput cols`
		detected_cols=`expr "$detected_cols" - 4`
	else
		detected_cols=`expr "$detected_cols" - 2`
	fi
fi

# Find out if our dialogs can have the --no-collapse option...
dialog --help 2>/tmp/dgap.tmp
mytest=`cat /tmp/dgap.tmp | grep no-collapse | wc -l`
if [ $mytest -eq 0 ]
then
	collapse_supported=0
else
	collapse_supported=1
fi

rm -f /tmp/dgap.tmp

if [ -f /proc/dgap/info ]
then
	grep "Total Boards:" /proc/dgap/info >/tmp/dgap.tmp
	TOTAL_BOARDS=`get_number_of_boards < /tmp/dgap.tmp`
	rm -f /tmp/dgap.tmp
fi

if [ $TOTAL_BOARDS = "0" ]
then
	if [ $collapse_supported -eq 1 ]
	then
		dialog --backtitle "No boards found" --no-collapse --msgbox "No boards found" 10 $detected_cols
	else
		dialog --backtitle "No boards found" --msgbox "No boards found" 10 $detected_cols
	fi
	tput -T${termtype} clear
	exit
fi

rm -f /tmp/dgap.out

create_board_strings

string=`cat /tmp/dgap.out`
rm -f /tmp/dgap.out

while [ 0 ]; do


	dialog --backtitle "DGAP VIEW" --menu "Select Board" 18 $detected_cols 10 $string E Exit 2>/tmp/dgap.ans
	if [ $? = 1 ]
	then
		rm -f /tmp/dgap.ans
		tput -T${termtype} clear
		exit
	else
		ans=`tail -1 /tmp/dgap.ans`
		rm -f /tmp/dgap.ans
	fi

	BOARD=$ans

	if [ "$BOARD" = "E" ]
	then
		tput -T${termtype} clear
		exit
	fi

	display_screen

done
