#!/bin/sh

# This is a shell script to set up a config file for the
# MIT LCS Interactive Network Map

# This is pretty much a brute force kludge for now.  I would eventually
# like to have something that runs from scripts (like the DEC frob)

if [ $# -gt 1 ]; then
	echo "Usage: `basename $0` [conf-name]"
	exit 1
fi

if [ $# -eq 0 ]; then
    if [ -r config.h ]; then
	ANS=`ls -l config.h |
		awk '{print $10}' |
		awk -F. 'BEGIN{OFS="."}{$1="";print substr($0,2)}'`
	if [ -r config.$ANS ]; then
		name=$ANS
	fi
    else
	echo ""
	echo "This script lets you set up a configuration for building the"
	echo "	MIT LCS Interactive Network Map program."
	echo ""
	echo "At any question you may type a question mark (?) to get a short"
	echo "help message.  Most questions have a default answer which will"
	echo "be indicated in square brackets ([...]) at the end.  If this is"
	echo "the value you want, just hit the return key.  Yes/no questions"
	echo "may be answered with a single letter."
	echo ""
	echo ""
	echo ""
	while true; do
	    echo -n "Are you ready to start [Yes] ? "
	    read ANS
	    if [ ! "$ANS" ]; then ANS=Yes; fi
	    case "`echo $ANS|tr A-Z a-z`" in
		    y | yes)	break ;;
		    n | no)	echo "Then why did you run it?"
				exit 0 ;;
		    "?" )	echo "Answer Yes (by just hitting <return>) to get started."
			    echo "You can type Control-C if you didn't really want to configure."
			    ;;
		    *)	echo "Don't understand your answer, try 'y'." ;;
	    esac
	done
	echo ""
	echo ""
	echo "You need to choose a short name for your configuration.  This"
	echo "will be used as part of a file name and can be used in future"
	echo "for referencing this configuration.  If you wish to modify a"
	echo "configuration you already made, enter the name you gave it here."
	echo "Note that if you want a new configuration that's like an"
	echo "existing one enter a new name here, the existing one can be"
	echo "given later.  In particular you should not modify the example"
	echo "configurations provided with the distribution.  One suggestion"
	echo "for names is to use an abbreviation for your organization."
	echo ""
    fi
    if [ ! "${name-}" ]; then
	#if there was a good way to determine it on all machines, a good
	#default would be the second component of the FQDN for the workstation
	name=`hostname`
    fi
    echo ""
    while true; do
	echo -n "Configuration name [$name] ? "
	read ANS
	if [ ! "$ANS" ]; then ANS="$name"; fi
	if [ "$ANS" = h -o "$ANS" = default -o "$ANS" = LCS ]; then
		echo "That is a reserved configuration name."
		echo "Please choose another."
		continue
	fi
	if [ ! "`echo $ANS|tr -d A-Z-a-z0-9.`" ]; then break; fi
	if [ "$ANS" = "?" ]; then
		echo "Enter a short name, instructions above."
	else
		echo "Only use letters, digits, dash and dot in the name."
	fi
    done
    name="$ANS"
else
    name=$1
fi


# Set up the default defaults...
hv_x=Yes
x_ver=11
hv_ps=Yes
hv_tek=No
if [ `grep -c "SunOS Release 4" /etc/motd` -gt 0 ]; then
	rawip=Headers
else
	rawip=Default
fi
if [ -d ../snmpdk ]; then
	hv_snmp=Yes
	snmpdir=../snmpdk
else
	hv_snmp=No
	snmpdir=.
fi
if [ -r /dev/chaos ]; then
	hv_chaos=Yes
else
	hv_chaos=No
fi

if [ -f config.$name ]; then
	echo "There is already a configuration with that name."
	echo "This procedure will overwrite it, type quit to exit."
	while true; do
		echo -n "Use the existing values as defaults [Yes] ? "
		read ANS
		if [ ! "$ANS" ]; then ANS=Yes; fi
		case "`echo $ANS|tr A-Z a-z`" in
			q | quit | abort | exit)
				exit 0 ;;
			y | yes)
	eval `awk 'NF<2{next}
		   $1=="#define"{if ($2=="SNMPDK")v[$2]=$3;else v[$2]="Yes"}
	   END {	if (v["HAVE_X_WINDOWS"])
			{	print "hv_x=Yes"
				if (v["X_VERSION_10"])
					print "x_ver=10"
				else
					print "x_ver=11"
			    }
			else
			{	print "hv_x=No"
				print "x_ver=11"
			    }
			if (v["HARDCOPY_PS"])
				print "hv_ps=Yes"
			else
				print "hv_ps=No"
			if (v["HARDCOPY_TEK"])
				print "hv_tek=Yes"
			else
				print "hv_tek=No"
			if (v["SNMPDK"])
			{	print "hv_snmp=Yes"
				print "snmpdir=" v["SNMPDK"]
			    }
			else
				print "hv_snmp=No"
			if (v["RAW_IP_HEADERS"])
				print "rawip=Headers"
			else if (v["RAW_IP_NOHEADERS"])
				print "rawip=NoHeaders"
			else
				print "rawip=Default"
			if (v["USE_CHAOS"])
				print "hv_chaos=Yes"
			else
				print "hv_chaos=No"
		}' config.$name`
	break ;;
			n | no)		break ;;
			"?" )
	echo "If you answer yes, the settings in the existing config.$name file"
	echo "will be used as the defaults for configuration questions.  If you"
	echo "answer no, then the original defaults (the default defaults :-)"
	echo "will be used for each question.  If you have selected the wrong"
	echo "configuration name and wish to abort, enter quit."
				;;
			*)	echo "Don't understand your answer." ;;
		esac
	done
	echo ""
fi

while true; do
	#echo -n "Do you have the X window system [$hv_x] ? "
	#read ANS
	echo "X window system is required.  Setting option to 'Yes'."
	ANS=Yes
	if [ ! "$ANS" ]; then ANS="$hv_x"; fi
	case "`echo $ANS|tr A-Z a-z`" in
		y | yes)hv_x=Yes; break ;;
		n | no)	hv_x=No;  break ;;
		"?" )	echo "???"
			;;
		*)	echo "Don't understand your answer." ;;
	esac
done
echo ""


if [ $hv_x = Yes ]; then
    while true; do
	echo -n "Which version of X are you running [$x_ver] ? "
	read ANS
	if [ ! "$ANS" ]; then ANS="$x_ver"; fi
	case "`echo $ANS|tr A-Z a-z`" in
		10)	echo "WARNING:  The version 10 support has not been tested in years."
			echo "See Status file for more info."
			x_ver=10; break ;;
		11)	x_ver=11; break ;;

		"?" )	echo "Enter either 10 or 11 for which version of the"
			echo "X window System you are running.  Since Version"
			echo "10 support has not been tested in years, you"
			echo "Should probably be running version 11 or contact"
			echo "MAP-Request@LCS.MIT.Edu for help."  ;;

		*)	echo "Don't understand your answer." ;;
	esac
    done
    echo ""
fi


while true; do
	echo -n "Do you want to be able to write PostScript [$hv_ps] ? "
	read ANS
	if [ ! "$ANS" ]; then ANS="$hv_ps"; fi
	case "`echo $ANS|tr A-Z a-z`" in
		y | yes)	hv_ps=Yes; break ;;
		n | no)		hv_ps=No;  break ;;
		"?" )	echo "The answer to this question will determine whether the program will"
			echo "be able to write out a PostScript file to print black & white renditions"
			echo "of the maps.  Yes includes this, no excludes it."
			;;
		*)	echo "Don't understand your answer." ;;
	esac
done
echo ""


while true; do
	echo -n "Do you want to be able to print on a Tektronix 4317 [$hv_tek] ? "
	read ANS
	if [ ! "$ANS" ]; then ANS="$hv_tek"; fi
	case "`echo $ANS|tr A-Z a-z`" in
		y | yes)	hv_tek=Yes; break ;;
		n | no)		hv_tek=No;  break ;;
		"?" )	echo "The answer to this question will determine whether the program will"
			echo "be able to write out a file that can be printed (in color) on a"
			echo "Tektronix 4317 ink jet printer.  Yes includes this, no excludes it."
			;;
		*)	echo "Don't understand your answer." ;;
	esac
done
echo ""


while true; do
	echo -n "Do you have the MIT SNMP Development Kit [$hv_snmp] ? "
	read ANS
	if [ ! "$ANS" ]; then ANS="$hv_snmp"; fi
	case "`echo $ANS|tr A-Z a-z`" in
		y | yes)	hv_snmp=Yes; break ;;
		n | no)		hv_snmp=No;  break ;;
		"?" )	echo "You need the MIT SNMP Development Kit to support sending SNMP queries."
			echo "See installation instructions for more on using this."
			;;
		*)	echo "Don't understand your answer." ;;
	esac
done
echo ""


if [ $hv_snmp = Yes ]; then
    while true; do
	echo -n "Where is the base dir of the SNMPDK installed [$snmpdir] ? "
	read ANS
	if [ ! "$ANS" ]; then ANS="$snmpdir"; fi
	if [ "$ANS" = "?" ]; then
		echo "Give a pathname to the SNMP Development Kit."
		echo "A relative path is probably best."
		continue
	fi
	if [ -d $ANS ]; then
		snmpdir=$ANS
		break
	fi
	echo "That's not a directory."
    done
    echo ""
fi


while true; do
	echo -n "What style of raw IP do you want [$rawip] ? "
	read ANS
	if [ ! "$ANS" ]; then ANS="$rawip"; fi
	case "`echo $ANS|tr A-Z a-z`" in
		h | header*)	rawip=Headers;   break ;;
		d | default)	rawip=Default;   break ;;
		n | noheader*)	rawip=NoHeaders; break ;;
		"?" )	echo "For the ICMP module to operate it needs to be compiled differently"
			echo "depending on whether raw IP sockets return packets with just the"
			echo "data or with the header followed by data.  The source has"
			echo "conditionals to figure this out in many cases.  In some cases it"
			echo "just gets the wrong answer.  I suggest you try whatever is"
			echo "being offered as default.  Then if ping does not work, try"
			echo "forcing it."
			echo "The three answers that you can give here are:"
			echo "    Default    Let the conditionals guess"
			echo "    Headers    Raw IP socket returns packets with headers"
			echo "    NoHeaders  Raw IP socket returns just data"
			echo "You can give just the first letter."
			;;
		*)	echo "Don't understand your answer." ;;
	esac
done
echo ""


while true; do
	echo -n "Do you operate CHAOS network protocols [$hv_chaos] ? "
	read ANS
	if [ ! "$ANS" ]; then ANS="$hv_chaos"; fi
	case "`echo $ANS|tr A-Z a-z`" in
		y | yes)
			hv_chaos=Yes; break ;;
		n | no)	hv_chaos=No;  break ;;
		"?" )	echo "If you don't understand, answer no."
			;;
		*)	echo "Don't understand your answer." ;;
	esac
done
echo ""



## #  #   #    #     #      #       #       #      #     #    #   #  # ##
#
#	All the options are selected, build the file now...
#
## #  #   #    #     #      #       #       #      #     #    #   #  # ##
cat >config.$name.new <<EOF
/**** Configuration file for the MIT LCS Interactive Network Map.  ****/


EOF

flg=true
if [ -f config.$name ]; then
    while true; do
	echo -n "Copy comment from original [Yes] ? "
	read ANS
	if [ ! "$ANS" ]; then ANS=Yes; fi
	case "`echo $ANS|tr A-Z a-z`" in
		y | yes)
			tail +2 config.$name | awk '/^.\*\*/,/^$/' >>config.$name.new
			flg=false
			break ;;
		n | no)		break ;;
		"?" )	echo "Answer yes to have the descriptive comment retained"
			echo "from the previous config with same name.  Answer no"
			echo "to be prompted for a replacement comment."
			echo ""
			echo "The comment presently reads:"
			tail +2 config.$name | awk '/^.\*\*/,/^$/'
			;;
		*)	echo "Don't understand your answer." ;;
	esac
    done
fi

if $flg ; then
	echo "Enter a one line comment for config, or <return> to start multi-liner."
	read ANS
	if [ "$ANS" ]; then
		echo "/** Configuration for $ANS **/" >>config.$name.new
	else
		echo "Enter comment, end with Control-D."
		echo "/** Configuration for:" >>config.$name.new
		awk '{print " **",$0}' >>config.$name.new
		echo " **/" >>config.$name.new
	fi
	echo "" >>config.$name.new
fi

echo "" >>config.$name.new
echo "" >>config.$name.new

if [ $hv_x = "Yes" ]; then
	echo "/* Include support for X window system */" >>config.$name.new
	echo "#define HAVE_X_WINDOWS" >>config.$name.new
	if [ $x_ver = 10 ]; then
		echo "/* Obsolete version 10 */" >>config.$name.new
		echo "#define X_VERSION_10"  >>config.$name.new
	else
		echo "/* Use version 11 (default) */" >>config.$name.new
	fi
else
	echo "/* Don't have X window system */" >>config.$name.new
fi
if [ $hv_ps = "Yes" ]; then
	echo "/* Include support for PostScript output */" >>config.$name.new
	echo "#define HARDCOPY_PS"  >>config.$name.new
else
	echo "/* Don't support PostScript output */" >>config.$name.new
fi
if [ $hv_tek = "Yes" ]; then
	echo "/* Include support for Tektronix 4317 */" >>config.$name.new
	echo "#define HARDCOPY_TEK"  >>config.$name.new
else
	echo "/* Don't support Tektronix 4317 */" >>config.$name.new
fi
if [ $hv_snmp = "Yes" ]; then
	echo "/* Include support for SNMP */" >>config.$name.new
	echo "#define SNMPDK $snmpdir"  >>config.$name.new
else
	echo "/* Don't have SNMP */" >>config.$name.new
fi
case $rawip in
Default)	echo "/* Raw IP gets defaulted. */" >>config.$name.new
		;;
Headers)	echo "/* Raw IP has headers. */" >>config.$name.new
		echo "#define RAW_IP_HEADERS" >>config.$name.new
		;;
NoHeaders)	echo "/* Raw IP returns just data. */" >>config.$name.new
		echo "#define RAW_IP_NOHEADERS" >>config.$name.new
		;;
esac
if [ $hv_chaos = "Yes" ]; then
	echo "/* Include support for using CHAOS protocols */" >>config.$name.new
	echo "#define USE_CHAOS"  >>config.$name.new
else
	echo "/* Don't have CHAOS protocols */" >>config.$name.new
fi



## #  #   #    #     #      #       #       #      #     #    #   #  # ##
if [ -f config.$name ]; then
	rm -f config.$name.bak
	mv config.$name config.$name.bak
fi
mv config.$name.new config.$name



## #  #   #    #     #      #       #       #      #     #    #   #  # ##
if [ -f config.h ]; then
    while true; do
	echo -n "Replace existing config.h with link to this file [Yes] ? "
	read ANS
	if [ ! "$ANS" ]; then ANS=Yes; fi
	case "`echo $ANS|tr A-Z a-z`" in
		y | yes)
			rm -f config.h
			break ;;
		n | no)		exit 0 ;;
		"?" )	echo "Answer yes to have the configuration you just defined set as the one"
			echo "from which builds are done.  Answer no to leave existing config alone."
			;;
		*)	echo "Don't understand your answer." ;;
	esac
    done
fi

ln -s config.$name config.h