#!/bin/bash
#swconf.sh v0.2 - a bash script to range-configure XorPlus-powered switches
#fixed some bugs related to not having a switch to test it on... 

declare -a CMD	#array of commands, primary context
declare -a VARR #array of member VLANs, for trunks

#DEV is usually /dev/ttyS0 on a machine with a serial port. Assume USB converter
DEV=/dev/ttyUSB0

#show help prompt and exit
if [ "-h" = "$1" ]; then
	echo "swconf.sh - a hacky bash script to range-configure XorPlus-powered switches"
	echo "-h : show this prompt"
	echo
else
#options: config mgmt. port, range ports - vlan, port modes (trunk or access), save, exit.
#enter configure mode
	echo "configure" >> $DEV

	echo -n "> "
	read -a CMD

	while [ "${CMD[0]}" != "exit" ]; do
		case "${CMD[0]}" in
			"help" | "?" )
				echo "iface [eth0|eth1] [IP addr] - Configure management interface address. Address is in CIDR form (e.g 172.16.0.30/16)"
				echo "vlan [VLAN ID] - instantiate a VLAN interface of tag [VLAN ID]"
				echo "ports [min] [max] - Configure VLAN and mode of ports between [min] and [max] inclusive"
				echo "save - Commit changes to memory"
				echo "exit - exit script"
				echo "help - display this list (equivalent to '?')"
				echo "clear - clear the screen"
			;;
			"iface" )	#[iface] [if] [addr]
				if [ -z "${CMD[1]}" ]; then
					echo "iface [eth0|eth1] [IP addr/mask]"
				else
					echo "set interface management-ethernet ${CMD[1]} address ${CMD[2]}" >> $DEV
				fi	
			;;
			"vlan" )	#instantiate a new vlan 
				if [ -z "${CMD[1]}" ]; then
					echo "vlan [VLAN ID]"
				else
					echo "set vlans vlan-id ${CMD[1]}" >> $DEV
				fi
			;;
			"ports" )	#range configure ports
				if [ -z "${CMD[1]}" ]; then
					echo "ports [min] [max]"	
				else
				echo -n "("${CMD[0]}")# "	#change prompt to show new context
				read OPT PAR1 PAR2
				while [ "$OPT" != "exit" ]; do	#stay in ports context until given 'exit' 
					case "$OPT" in
					"delete")
						for i in `seq ${CMD[1]} ${CMD[2]}`; do
							sleep 0.2
							echo "delete interface gigabit-ethernet ge-1/1/$i family ethernet-switching" 
						done >> $DEV
					;;
					"vlan") 	#set vlan association 
						for i in `seq ${CMD[1]} ${CMD[2]}`; do
							sleep 0.2
							echo "set interface gigabit-ethernet ge-1/1/$i family ethernet-switching native-vlan-id $PAR1" 
						done >> $DEV
					;;
					"mode")		#set port mode 
						if [ "$PAR1" = "access" ]; then
							for i in `seq ${CMD[1]} ${CMD[2]}`; do
								sleep 0.2
								echo "set interface gigabit-ethernet ge-1/1/$i family ethernet-switching port-mode access"
							done >> $DEV
						elif [ "$PAR1" = "trunk" ]; then	
							echo -n "trunk member vlans (X Y Z...): "
							read -a VARR	
							for i in `seq ${CMD[1]} ${CMD[2]}`; do
								sleep 0.2
								for j in ${VARR[@]}; do
									echo "set interface gigabit-ethernet ge-1/1/$i family ethernet-switching vlan members $j" 
								done
								echo "set interface gigabit-ethernet ge-1/1/$i family ethernet-switching port-mode trunk" 
							done >> $DEV
						else
							echo "Unknown command"
						fi
					;;	
					"range")	#set new ports range 
						CMD[1]=$PAR1
						CMD[2]=$PAR2
					;;
					"help" | "?" )
						echo "vlan [1..1020]: set native vlan for ports"
						echo "mode [access|trunk]"
						echo "delete: wipe out port settings"
						echo "range [min] [max]: configure new range of ports"
						echo "exit: exit from port context"
					;;
					* ) echo "Unknown command" ;;	
					esac	
				echo -n "("${CMD[0]}")# "
				read OPT PAR1 PAR2
				done	
				fi
			;;
			"save" )
				echo "saving settings..."
				echo "commit" >> $DEV
				sleep 5		#save takes a while
				echo "save running-to-startup" >> $DEV
			;;
			"clear" ) clear ;;
			* ) echo "Unknown command" ;;
		esac
		echo -n "> "
		read -a CMD
	done
#escape configure mode
	sleep 0.2
	echo "exit" >> $DEV
fi
exit 0
