#!/bin/sh

# dnetsetup version 1.0
# Copyright (c)1998 by Art Cancro / All rights reserved.
# This program is distributed under the terms of the GNU General Public
# License.  See copyright.txt for more information.
#
# dnetsetup requires Savio Lam's "dialog" program to perform its interactive
# dialogs.  This program is standard on most Linux systems, and should be
# easy enough to get on other systems.

cleanup() {
	rm -f $tempfile $tempfile2 $menucmd 2>/dev/null
	exit
	}


roomlist() {
	node=$1
	
	./netsetup roomlist $1 >$tempfile 2>&1
	$DIALOG --textbox $tempfile $menuheight $menuwidth
	}


unshare() {
	node=$1

	./netsetup roomlist $node >$tempfile
	numrooms=`cat $tempfile |wc -l`
	sed s/\ /\\\\\ /g <$tempfile | sed s/$/\ \"\"/g >$tempfile2
	echo $DIALOG --menu \"Select the room you wish to stop sharing\" \
		$menuheight $menuwidth $numrooms \
		`cat $tempfile2` >$menucmd
	if sh $menucmd 2>$tempfile; then
		room_to_delete=`cat $tempfile`

		if $DIALOG --yesno "Are you sure you wish to stop sharing $room_to_delete ?" 5 $menuwidth ; then
			if ./netsetup unshare $node "$room_to_delete" 2>$tempfile; then
				$DIALOG --msgbox "$room_to_delete has been unshared." 5 $menuwidth
			else
				$DIALOG --textbox $tempfile $menuheight $menuwidth
				fi
			fi
		fi

	}

do_share() {
	node=$1

	if $DIALOG --inputbox "Enter name of room to share:" \
		$menuheight $menuwidth 2>$tempfile ; then
	
		room_to_share=`cat $tempfile`	
		if ./netsetup share $node "$room_to_share" 2>$tempfile; then
			$DIALOG --msgbox "$room_to_share has been added." 5 $menuwidth
		else
			$DIALOG --textbox $tempfile $menuheight $menuwidth
			fi

		fi
	}


edit_this_node() {
	node=$1
	choice=nothing_yet

	while [ $choice != EXIT ]
	do
		if $DIALOG --menu "Editing $node" $menuheight $menuwidth 4 \
			LIST	"List currently shared rooms" \
			SHARE	"Add a shared room" \
			UNSHARE	"Stop sharing a room" \
			EXIT	"Return to main menu" 2>$tempfile; then
				choice=`cat $tempfile`
			else
				choice="EXIT"
				fi

		[ $choice = "LIST" ] && roomlist $node
		[ $choice = "SHARE" ] && do_share $node
		[ $choice = "UNSHARE" ] && unshare $node

		done
	}


edit_a_node() {

	./netsetup nodelist >$tempfile
	numnodes=`cat $tempfile |wc -l`
	sed s/$/\ \"\"/g <$tempfile >$tempfile2
	echo $DIALOG --menu \"Select the node you wish to edit\" \
		$menuheight $menuwidth $numnodes \
		`cat $tempfile2` >$menucmd
	if sh $menucmd 2>$tempfile; then
		system_to_edit=`cat $tempfile`
		edit_this_node $system_to_edit
	else
		true
		fi
	}






delete_network_nodes() {

	./netsetup nodelist >$tempfile
	numnodes=`cat $tempfile |wc -l`
	sed s/$/\ \"\"/g <$tempfile >$tempfile2
	echo $DIALOG --menu \"Select the node you wish to delete\" \
		$menuheight $menuwidth $numnodes \
		`cat $tempfile2` >$menucmd
	if sh $menucmd 2>$tempfile; then
		system_to_delete=`cat $tempfile`

		if $DIALOG --yesno "Are you sure you wish to delete $system_to_delete ?" 5 $menuwidth ; then
			if ./netsetup deletenode $system_to_delete 2>$tempfile; then
				$DIALOG --msgbox "$system_to_delete has been deleted." 5 $menuwidth
			else
				$DIALOG --textbox $tempfile $menuheight $menuwidth
				fi
			fi
		fi

	}


add_a_node() {
	node=$1

	if $DIALOG --inputbox "Enter name of new node:" \
		$menuheight $menuwidth 2>$tempfile ; then
	
		new_node=`cat $tempfile`	
		if ./netsetup addnode $new_node 2>$tempfile; then
			$DIALOG --msgbox "$new_node has been created." 5 $menuwidth
		else
			$DIALOG --textbox $tempfile $menuheight $menuwidth
			fi

		fi
	}


# Main loop...
clear
tempfile=/tmp/dnetsetup.$$.1
tempfile2=/tmp/dnetsetup.$$.2
menucmd=/tmp/dnetsetup.$$.3
menuheight=20
menuwidth=72
DIALOG=dialog
while true
do
	if $DIALOG --menu "Citadel/UX Network Setup" $menuheight $menuwidth 4 \
		EDIT	"View or change a network node" \
		ADD	"Add a new network node" \
		DELETE	"Delete a network node" \
		EXIT	"Exit from Network Setup" \
		2>$tempfile; then
			choice=`cat $tempfile`
		else
			choice=EXIT
		fi
	rm -f $tempfile

	[ $choice = "EXIT" ] && cleanup
	[ $choice = "DELETE" ] && delete_network_nodes
	[ $choice = "EDIT" ] && edit_a_node
	[ $choice = "ADD" ] && add_a_node

	done
