:
# ncfg.sh
# Wed Oct 09 09:01:06 PDT 1991	barryf
#
mainwin=1
subwin=2
clrwin=3

# return codes for functions called from menu
M_READY=0;   M_STAY=1
M_UPONE=2;   M_CONT1=3
M_CONT2=4;   M_UPTWO=5 
M_UPTHREE=6; M_TOP=7

fnA() {
	CHAIN=""
	DCHAIN=""

	oadeinit
	netconfig -m ${ELEMENT:-""} > /tmp/fooper$$ 2>&1
	ret=$?
	sort /tmp/fooper$$ > /tmp/foop$$
	oainit
	if [ ${ret} -ne 0 ]
	then
		wmove $mainwin; werase
		[ -s /tmp/fooper$$ ] && error "`cat /tmp/fooper$$` - press <RETURN>"
		rm /tmp/fooper$$
		wrefresh $mainwin
		list_chains
		return $M_STAY
	fi
	rm /tmp/fooper$$
	wmopen $subwin 12 70 12 10
	while [ ${ret} -eq 0 ]
	do
		oainit
		wmove $mainwin; werase
		wmtitle "Add a chain"
		echo "\n\tSelect a product:   [\c"
		wattr -so
		echo "\t\t\t\c"
		wattr -se
		echo "]"
		echo "\n\tCurrent chain:      [\c"
		wattr -so
		echo "${DCHAIN:-""}\c"
		wattr -se
		echo "]"
		wrefresh $mainwin
		wmove $subwin; werase
		wrefresh $subwin
		pp -f /tmp/foop$$
		wmove $mainwin
		if [ $PP_NUM -ge 0 ]
		then
			echo "\n\tSelect a product:   [\c"
			wattr -so
			PP_BASE=`echo ${PP_ITEM} | sed -e "s/ .*//"`
			echo "${PP_BASE}\t\t\c"
			wattr -se
			echo "]"
			if [ ${CHAIN:-""} = "" ]
			then
				CHAIN=${PP_BASE}
				DCHAIN=${PP_BASE}
			else
				CHAIN="${CHAIN:-""}#${PP_BASE}"
				DCHAIN="${DCHAIN:-""}->${PP_BASE}"
			fi
		else
			# delete border of subwin
			newwin $clrwin 12 70 12 10
			wrefresh $clrwin
			wmove $mainwin; werase
			wmclose $clrwin
			break
		fi

		wrefresh $mainwin
		oadeinit
		netconfig -m "${CHAIN}" > /dev/null 2>&1
		ret=$?
		if [ $ret -eq 0 ]
		then
			netconfig -m ${CHAIN} | sort > /tmp/foop$$
		else
			break
		fi
	done

	oainit
	wmove $mainwin; werase
	rm -f /tmp/foop$$
	wmove $mainwin
	wmclose $subwin
	echo "\n\n\tAdd chain ${DCHAIN}? \c"
	wrefresh $mainwin
	newwin $subwin 1 16 8 60
	wmove $subwin
	pp Yes No
	wmove $mainwin; wclear
	wmclose $subwin
	if [ ${PP_ITEM:-""} = "Yes" ]
	then
		echo "\nAdding ${DCHAIN}..."
		ERROR=`netconfig -Ca ${CHAIN:-""} 2>&1`
		if [ $? -eq 0 ]
		then
			oadeinit
			netconfig -a ${CHAIN}
			oainit
		else
			wmove $mainwin; wclear
			error "${ERROR} - press <RETURN>"
		fi
	fi
	wrefresh $mainwin
	list_chains
	return $M_STAY
}

fnR() {
	oadeinit
	rm -f /tmp/foop$$
	for i in `netconfig -s | sort | sed -e "s/#/->/g"`
	do 
		echo $i >> /tmp/foop$$
	done
	oainit
	wmove $mainwin; werase
	wmtitle "Remove a chain"
	wrefresh $mainwin
	if [ -s /tmp/foop$$ ]
	then
		echo "\n\tSelect a chain to remove:   \c"
		wrefresh $mainwin
		wmopen $subwin 12 70 12 10
		wmove $subwin; werase
		wrefresh $subwin
		pp -f /tmp/foop$$
		newwin $clrwin 12 70 12 10
		wrefresh $clrwin
		wmove $mainwin; werase
		wmclose $clrwin
		echo "\n\n\tRemove chain ${PP_ITEM:-""}? \c"
		DCHAIN=${PP_ITEM:-""}
		wrefresh $mainwin
		newwin $subwin 1 16 8 60
		wmove $subwin
		pp Yes No
		wmove $mainwin; wclear
		wmclose $subwin
		if [ ${PP_ITEM:-""} = "Yes" ]
		then
			CHAIN=`echo ${DCHAIN} | sed -e "s/->/#/g"`
			echo "\nRemoving ${DCHAIN}..."
			ERROR=`netconfig -Cr ${CHAIN:-""} 2>&1`
			if [ $? -eq 0 ]
			then
				oadeinit
				netconfig -r ${CHAIN}
				oainit
			else
				wmove $mainwin; wclear
				error "${ERROR} - press <RETURN>"
			fi
		fi
	else
		error "No chains configured - press <RETURN>"
	fi
	rm -f /tmp/foop$$
	list_chains
	return $M_STAY
}

fnM() {
	oadeinit
	rm -f /tmp/foop$$
	for i in `netconfig -s | sort | sed -e "s/#/->/g"`
	do 
		echo $i >> /tmp/foop$$
	done
	oainit
	wmove $mainwin; werase
	wmtitle "Modify an element in a chain"
	wrefresh $mainwin
	if [ -s /tmp/foop$$ ]
	then
		echo "\n\tSelect a chain with the element to reconfigure:   \c"
		wrefresh $mainwin
		wmopen $subwin 12 70 12 10
		wmove $subwin; werase
		wrefresh $subwin
		pp -f /tmp/foop$$
		rm -f /tmp/foop$$
		DCHAIN=${PP_ITEM}
		CHAIN=`echo ${DCHAIN} | sed -e "s/->/#/g"`

		oadeinit
		netconfig -e ${CHAIN:-""} > /tmp/foop$$
		oainit
		wmove $mainwin; werase
		if [ -s /tmp/foop$$ ]
		then
			echo "\n\tSelect an element to reconfigure:   \c"
			wrefresh $mainwin
			wmove $subwin; werase
			wrefresh $subwin
			pp -f /tmp/foop$$
			ELEMENT=`echo ${PP_ITEM} | sed -e "s/ .*//"`

			newwin $clrwin 12 70 12 10
			wrefresh $clrwin
			wmove $mainwin; werase
			wmclose $clrwin

			echo "\n\n\tModify element ${ELEMENT}? \c"
			wrefresh $mainwin
			newwin $subwin 1 16 8 60
			wmove $subwin
			oadeinit
			echo "Yes\nNo" > /tmp/foop$$
			oainit
			pp -f /tmp/foop$$
			wmove $mainwin; wclear
			wmclose $subwin
			if [ ${PP_ITEM:-""} = "Yes" ]
			then
				echo "\nReconfiguring element ${ELEMENT}..."
				oadeinit
				netconfig -c ${CHAIN:-""} ${ELEMENT:-""} 2> /tmp/foop$$
				ret=$?
				oainit
				[ $ret -ne 0 ] && [ -s /tmp/foop$$ ] && error "`cat /tmp/foop$$` - press <RETURN>"
				list_chains
			fi
		else
			wmclose $subwin
			newwin $clrwin 12 70 12 10
			wrefresh $clrwin
			wmove $mainwin; werase
			wmclose $clrwin
			wmove $mainwin; werase
			wrefresh $mainwin
			newwin $subwin 2 80 22 0
			wmove $subwin; werase
			wattr -so
			echo "No elements in chain ${DCHAIN} support reconfigure - press <RETURN>\c"
			wattr -se
			read foo
			werase
			wrefresh $subwin
			wmove $mainwin; werase
			wmclose $subwin
			wrefresh $mainwin
		fi
	else
		error "No chains configured - press <RETURN>"
	fi
	rm -f /tmp/foop$$
	list_chains
	return $M_STAY
}

list_chains() {
	wmove $mainwin; werase
	wmtitle "Chains Currently Installed"
	oadeinit
	netconfig -s > /tmp/foop$$ 2>&1
	oainit
	wmove $mainwin; werase
	if [ -s /tmp/foop$$ ]
	then
		for i in `netconfig -s | sort | sed -e "s/#/->/g"`
		do 
			echo $i
		done
	else
		echo "No chains configured"
	fi
	rm -f /tmp/foop$$
	wrefresh $mainwin
	return $M_STAY
}

fnL() {
	oadeinit
	rm -f /tmp/foop$$
	for i in `netconfig -s | sort | sed -e "s/#/->/g"`
	do 
		echo $i >> /tmp/foop$$
	done
	oainit
	wmove $mainwin; werase
	if [ -s /tmp/foop$$ ]
	then
		wmtitle "List Elements in a chain"
		echo "\n\tSelect a chain to list:   \c"
		wrefresh $mainwin
		wmopen $subwin 12 70 12 10
		wmove $subwin; werase
		wrefresh $subwin
		pp -f /tmp/foop$$
		newwin $clrwin 12 70 12 10
		wrefresh $clrwin
		wmove $mainwin; werase
		wmclose $clrwin

		if [ $PP_NUM -ge 0 ]
		then
			CHAIN=`echo ${PP_ITEM} | sed -e "s/->/#/g"`
			oadeinit
			netconfig -L ${CHAIN:-""} > /tmp/foop$$
			echo "\n\nPress the <RETURN> key to continue..." >> /tmp/foop$$
			oainit
			scan /tmp/foop$$
			oainit
		fi
	else
		error "No chains configured - press <RETURN>"
	fi
	newwin $clrwin 24 80 0 0
	wrefresh $clrwin
	wmove $mainwin; werase
	wmclose $clrwin
	dateline netconfig
	wrefresh $mainwin
	rm -f /tmp/foop$$
	list_chains
	return $M_STAY
}

fnQ() {
	wmove $mainwin; werase
	return $M_READY
}

# main()
#

netconfig -v | grep 1.0.0 > /dev/null 2>&1 && {
 	echo "Wrong version of netconfig installed, `netconfig -v`"
 	exit 1
}

if [ "$1" != "-second" ]
then
	if [ -x /bin/oash ]
	then
		if [ "$OALIB" = "" ]
		then
			OALIB=/usr/lib/scosh
		fi
		export OALIB
		exec /bin/oash /etc/ncfg -second
	else
		exec netconfig
	fi
else
	eval oainit 2>/dev/null || {
		exec netconfig
	}
fi


wclear
wrefresh

wmopen $mainwin 16 80 5 0
dateline netconfig
list_chains
oahelp /usr/lib/netconfig/bin/ncfghelp
menu /usr/lib/netconfig/bin/ncfgmenu "netconfig"

oadeinit
rm -f /tmp/foop$$
