#!/bin/bash

echo ""
echo "This script will do the following:

- Create a command set json file.
- import the file into opConfig. (optional)
- Run the command set. (optional)"
echo ""
echo -n "Input a command set name (do not use spaces): "
read cName
echo ""
echo "Enter the node name, or if more than one comma sperated."
echo "Example: node1,node2,node3"
read nodeNames
echo ""
echo "Input all the commands that should be executed in configuration mode. Once all the commands have been entered use '<ctrl> + d' to continue."

while read line; do
	ARRAY=("${ARRAY[@]}" "$line")
done

arrayLen=$(echo ${#ARRAY[@]})

echo "{" > /tmp/$cName.json
echo "  \"name\": \""$cName"\"," >> /tmp/$cName.json
echo "  \"commands\": [" >> /tmp/$cName.json
zulu=0
for i in "${ARRAY[@]}"; do
	zulu=$((zulu+1))
	if [ $zulu -eq $arrayLen ]; then
		echo "    \"$i\"" >> /tmp/$cName.json
	else
		echo "    \"$i\"," >> /tmp/$cName.json
	fi
done
echo "  ]," >> /tmp/$cName.json
echo "  \"post-commands\": [\"write mem\"]" >> /tmp/$cName.json
echo "}" >> /tmp/$cName.json
echo ""
echo "configuration set /tmp/$cName.json has been created."
echo "Press enter to review the command set..."
read CONTINUE
cat /tmp/$cName.json
echo ""
echo -n "Would you like to import the command set into opConfig? (yes/no): "
read CONTINUE
if [ $CONTINUE == "yes" ]; then
	/usr/local/omk/bin/opconfig-cli.exe act=import_configset file=/tmp/$cName.json
else
	exit
fi
echo ""
echo -n "Would you like to run the command set? (yes/no): "
read CONTINUE
if [ $CONTINUE == "yes" ]; then
	/usr/local/omk/bin/opconfig-cli.exe act=push_configset name=$cName at='now + 1 minutes' nodes=$nodeNames
else
	exit
fi
echo ""
