#!/bin/bash

VTYSH_INPUT=

function runVtysh()
{
	vtysh -c "${VTYSH_INPUT}"
#	echo "vtysh -c ${VTYSH_INPUT}"
}

function addVtyshInput()
{
	local lines=("${@}")
	for line in "${lines[@]}"
	do
#		VTYSH_INPUT=$(printf "%s\n%s" "${VTYSH_INPUT}" "${line}")
		VTYSH_INPUT+="${line}"$'\n'
	done
}

function addEndWrMemClear()
{
	local vrfs=("${@}")

	addVtyshInput \
		"end" \
		"wr mem" \
		"clear ip bgp * soft"

	for vrf in "${vrfs[@]}"
	do
		addVtyshInput "clear ip bgp vrf ${vrf} * soft"
	done
}

function main()
{
	local routerId
	routerId=$(grep -m 1 "router-id" /etc/frr/frr.conf | sed 's/.*router-id //')
	read -rp "What is the BGP router-id? [${routerId}] "
	if [[ -n "${REPLY}" ]]
	then
		routerId="${REPLY}"
	fi

	addVtyshInput "conf t"
	addVtyshInput "router-id ${routerId}"

 #   addVtyshInput "ip prefix-list EVX-ROUTE seq 11 permit 10.222.0.0/16 le 32"
 #   addVtyshInput "ip prefix-list EVX-ROUTE seq 12 permit 10.223.0.0/16 le 32"
 #   addVtyshInput "ip prefix-list EVX-ROUTE seq 13 permit 10.224.0.0/16 le 32"
 #   addVtyshInput "ip prefix-list EVX-ROUTE seq 14 permit 10.4.0.0/16 le 32"
 #   addVtyshInput "ip prefix-list EVX-ROUTE seq 15 permit 10.251.0.0/16 le 32"

	addVtyshInput "router bgp 65000"
	addVtyshInput "bgp router-id ${routerId}"


	local managementPeers=()
#	local runningManagement
#	while [[ -z "${runningManagement}" ]]
	while true
	do
		local peerIp
		read -rp "Enter the starting IP in the team range (71 or 72 for NFR1/2) or press enter to move on : " peerIp
		if [[ -z "${peerIp}" ]]
		then
			break
		fi

		local peerNfrPref=1
		read -rp "Set NFR preference [${peerNfrPref}]: "
		if [[ -n "${REPLY}" ]]
		then
			peerNfrPref="${REPLY}"
		fi

		local peerLpgPref=1
#		read -rp "Set LPG preference [${peerLpgPref}]: "
#		if [[ -n "${REPLY}" ]]
#		then
#			peerLpgPref="${REPLY}"
#		fi

		managementPeers+=("${peerIp}:${peerNfrPref}:${peerLpgPref}")

		addVtyshInput "neighbor ${peerIp} peer-group UPSTREAM"
	done

	addVtyshInput "address-family ipv4 unicast"

	for managementPeer in "${managementPeers[@]}"
	do
		local peerIp
		local peerNfrPref
		local peerLpgPref

		IFS=':' read -r peerIp peerNfrPref peerLpgPref <<< "${managementPeer}"

		addVtyshInput "neighbor ${peerIp} route-map NFR-${peerNfrPref}-PREF-${peerLpgPref} in"
		addVtyshInput "neighbor ${peerIp} route-map NFR-${peerNfrPref}-PREF-${peerLpgPref} out"
	done

	# Exiting address family
	addVtyshInput "exit"

	# Exiting default router block
	addVtyshInput "exit"

	local vrfs
	vrfs=$(ip -br link show type vrf | awk '$1 != "NULL" { printf "%s ", $1 } END { printf "\n" }')
	read -rp "Provide a space-separated list of VRFs to configure service peers for [${vrfs}]: "
	if [[ -n "${REPLY}" ]]
	then
		vrfs="${REPLY}"
	fi
	vrfs=($vrfs)

	for vrf in "${vrfs[@]}"
	do
		local asn=65000
		read -rp "What is the BGP ASN for VRF ${vrf}? [${asn}] "
		if [[ -n "${REPLY}" ]]
		then
			asn="${REPLY}"
		fi

        read -rp "What is the source address of this VRF (Public NAT IP from earlier)? [] "
 		if [[ -n "${REPLY}" ]]
 		then
 		    addVtyshInput "ip prefix-list EVX-ROUTE seq 1 permit ${REPLY}/32"
		    addVtyshInput "route-map ${vrf}-SRC permit 1"
		    addVtyshInput "match ip address prefix-list DEFAULT-ROUTE"
		    addVtyshInput "set src ${REPLY}"
		    addVtyshInput "exit"
		fi

		addVtyshInput "router bgp ${asn} vrf ${vrf}"

		local servicePeers=()
		while true
		do
			local peerIp
			read -rp "Enter the starting IP in the team range +2 (71 or 72 for NFR1/2) or press enter to move on : " peerIp
			if [[ -z "${peerIp}" ]]
			then
				break
			fi

			local peerNfrPref=1
			read -rp "Set NFR preference [${peerNfrPref}]: "
			if [[ -n "${REPLY}" ]]
			then
				peerNfrPref="${REPLY}"
			fi

			local peerLpgPref=1
#			read -rp "Set LPG preference [${peerLpgPref}]: "
#			if [[ -n "${REPLY}" ]]
#			then
#				peerLpgPref="${REPLY}"
#			fi

			servicePeers+=("${peerIp}:${peerNfrPref}:${peerLpgPref}")

			addVtyshInput "neighbor ${peerIp} peer-group ${vrf}-UPSTREAM"
		done

		addVtyshInput "address-family ipv4 unicast"

		for servicePeer in "${servicePeers[@]}"
		do
			local peerIp
			local peerNfrPref
			local peerLpgPref

			IFS=':' read -r peerIp peerNfrPref peerLpgPref <<< "${servicePeer}"

			addVtyshInput "neighbor ${peerIp} route-map NFR-${peerNfrPref}-PREF-${peerLpgPref} in"
			addVtyshInput "neighbor ${peerIp} route-map NFR-${peerNfrPref}-PREF-${peerLpgPref} out"
		done

		# Exiting address family
		addVtyshInput "exit"

		addVtyshInput "exit"
	done

	addEndWrMemClear "${vrfs[@]}"

	runVtysh
}

main
