#!/bin/bash
#
#****************************************************************************
#
#  @(#) $Id: dgnc_view,v 1.15 2004/06/24 20:00:17 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/dgnc:$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

# 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/dgnc.tmp2

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

	read rest

	while read n rx par brk frm ovr tx xon xoff rest
	do
		n=`expr $n + 1`
		printf "%5d %10d %6d %5d %5d %5d %10d %5d %5d\n" \
			$n $rx $par $brk $frm $ovr $tx $xon $xoff >> /tmp/dgnc.tmp2
	done
}


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

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

	read rest

	while read n cd id od ld dd b open rts cts dtr dsr dcd ri rest
	do
		let "c=0x$cd"
		let "i=0x$id"
		let "o=0x$od"
		let "l=0x$ld"
		let "d=0x$dd"
		port=`expr $n + 1`

		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/dgnc.tmp

		partext=
		bitstext=
		cstopbtext=
		breakbtext=

	done
}


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


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

	dialog --backtitle "DGNC 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/dgnc.ans
	if [ $? = 1 ]
	then
		return
	else
		ans=`tail -1 /tmp/dgnc.ans`
		rm -f /tmp/dgnc.ans
	fi

	case $ans in
	1)
		while [ 0 ]; do
			cat /proc/dgnc/$BOARD/stats >/tmp/dgnc.out
			display_stats < /tmp/dgnc.out
			new_stats=`cat /tmp/dgnc.tmp2`
			if [ $collapse_supported -eq 1 ]
			then
				dialog --backtitle "Display Hardware Statistics" --no-collapse --timeout $REFRESH_RATE --msgbox "$new_stats" 15 $detected_cols 2>/dev/null
			else
				dialog --backtitle "Display Hardware Statistics" --timeout $REFRESH_RATE --msgbox "$new_stats" 15 $detected_cols 2>/dev/null
			fi
			if [ $? = 0 ]
			then
				break
			fi
		done
		;;
	2)
		while [ 0 ]; do
			cat /proc/dgnc/$BOARD/flags > /tmp/dgnc.out
			display_flags < /tmp/dgnc.out
			new_flags=`cat /tmp/dgnc.tmp`
			if [ $collapse_supported -eq 1 ]
			then
				dialog --backtitle "Display HardWare Settings" --no-collapse --timeout $REFRESH_RATE --msgbox "$new_flags" 15 $detected_cols 2>/dev/null
			else
				dialog --backtitle "Display HardWare Settings" --timeout $REFRESH_RATE --msgbox "$new_flags" 15 $detected_cols 2>/dev/null
			fi
			if [ $? = 0 ]
			then
				break
			fi
		done

		;;
	R)
		dialog --backtitle "DGNC 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/dgnc.ans

			if [ $? != 1 ]
			then
				ans=`tail -1 /tmp/dgnc.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/dgnc.ans
		;;
	G|*)
		return
		;;
	esac

	done
}


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


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

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

		printf " $tmpi %s..(ttyn$tmpb*)" $name >> /tmp/dgnc.out
		tmpi=`expr $tmpi + 1`
	done
}



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

dialog --help 2>/tmp/dgnc.tmp
mytest=`cat /tmp/dgnc.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/dgnc.tmp

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

	rm -f /tmp/dgnc.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/dgnc.tmp
mytest=`cat /tmp/dgnc.tmp | grep no-collapse | wc -l`
if [ $mytest -eq 0 ]
then
	collapse_supported=0
else
	collapse_supported=1
fi

rm -f /tmp/dgnc.tmp

if [ -f /proc/dgnc/info ]
then
	grep "Total Boards:" /proc/dgnc/info >/tmp/dgnc.tmp
	TOTAL_BOARDS=`get_number_of_boards < /tmp/dgnc.tmp`
	rm -f /tmp/dgnc.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/dgnc.out

create_board_strings

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

while [ 0 ]; do


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

	BOARD=$ans

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

	display_screen

done
