#!/usr/bin/bash
# 
# cfgedit - Configuration file editor
#
# Usage : ./cfgedit <domain> <probe> <context>
#         ./cfgedit <context>
#         ./cfgedit 
#       
#
# Trisul Network Analytics
# 
# (c) 2017, Unleash Networks http://www.unleashnetworks.com 
#

#Usage 
Usage(){
	echo -e " cfgedit: Trisul Probe Config File editor "
	echo -e " Usage:  ./cfgedit <domain> <probe> <context>"
	echo -e "         ./cfgedit <context>"
	echo -e "         ./cfgedit "
}

#check argument list
if [[ ( "$#" -gt 3 ) || ( "$#" -eq 2 ) ]]; then
	Usage
	exit
fi

#defaults
DOMAIN="domain0"
NODE="probe0"
CONTEXT="context0"
BASENODE="probe"
if [[ $# -eq 1 ]]; then
	CONTEXT=$1
else
	if [ ! -z "$1" ]; then
	  DOMAIN=$1
	fi 
	if [ ! -z "$2" ]; then
	  NODE=$2
	fi
	if [ ! -z "$3" ]; then
	  CONTEXT=$3
	fi
fi 

# handling the context 
if [[ ( $CONTEXT == "context0" ) || ( $CONTEXT == "default") ]]; then
  CONTEXT="context0"
else
  CONTEXT="context_$CONTEXT"
fi

# get the hub/probe path
ENVFILE=env.eff
if [ ! -e $ENVFILE ]; then
source /usr/local/share/trisul-probe/env.eff 
else
source env.eff 
fi

NODEPATH="$INSTALL_PREFIX/etc/trisul-probe/$DOMAIN/$NODE/$CONTEXT"

IFS='%' 
clear
echo
echo  '  Trisul Network Analytics - Config File Editor'
echo  '  ---------------------------------------------'
echo  "  $CONTEXT "
echo
echo  '  Available config files are shown below. X indicates plugin not installed'
echo  


if [[ -z $EDITOR ]]; then
	if [[ -f /usr/bin/vim ]]; then 
		EDITOR=/usr/bin/vim
	elif [[ -f /bin/vi ]]; then 
		EDITOR=/bin/vi
	elif [[ -f /bin/nano ]]; then 
		EDITOR=/bin/nano 
	fi 
fi 

declare -a CFGFILES=(
'trisulProbeConfig.xml                          | TRISULCONFIG     - The main application config file '
'PI-01010101-125E-48D0-8AC9-A7E3AD2F60FD.xml    | HOSTS            - IP metering '
'PI-081336B3-5AF1-4D4F-B850-BA3CAEEB8A2C.xml    | BASE DOMAINS     - DNS Base Domains metering (rules, regexes, etc) ' 
'PI-7CA09636-02D4-45E7-AA00-BE0D49B94E26.xml    | NETFLOW          - NETFLOW/IPFIX policy (static templates etc)'
'PI-819CEF8E-B6DB-431B-97D0-7DB2A5EA5392.xml    | SFLOW            - SFLOW policy' 
'PI-88384B31-883A-8383-1278-0F3A11CEEE90.xml    | TCP FLOWS        - TCP Flow metering, subnets, etc' 
'PI-8CDE9B94-57C8-4014-981F-2E1DA44DE469.xml    | PDP              - Mobility '
'PI-09809283-125E-48D0-8AC9-A7E3AD2F60FD.xml    | UDP PROTO        - UDP Protocol parsing '
'PI-923740FF-125E-48D0-8AC9-A7E3AD2F60FD.xml    | TCP PROTO        - TCP Protocol parsing '
'PI-9999CCCC-125E-48D0-8AC9-A7E3AD2F60FD.xml    | APP PORTS        - Application counting, special server ports ' 
'PI-ABBBBBCC-125E-48D0-8AC9-A7E3AD2F60FD.xml    | MAC              - MAC layer, directional, broadcast metering etc' 
'PI-AFFFFFFF-125E-48D0-8AC9-A7E3AD2F60FD.xml    | IP TUNNELS       - Used for mobility ' 
'PI-CCCBBBB3-125E-48D0-8AC9-A7E3AD2F60FD.xml    | DNS              - Control DNS extraction, resource, fts, etc'
'PI-D2AAD7C6-E129-4366-A2AD-A8CB9AA4C2F4.xml    | HTTP HOST        - HTTP Host header depth setting' 
'PI-55885818-125E-48D0-8AC9-A7E3AD2F60FD.xml    | GEO              - Controls Geo plugin, country and ASN metering rules'
'PI-9FE3C6BC-BEB6-4320-A5BA-7993286D70DF.xml    | BADFELLAS        - Controls Badfellas lists, refreshes, custom rules'
'PI-21AA9300-A03A-45C9-94D6-0B62BD8313F6.xml    | URLFILTER        - URL Filter database and rules' 
)





CNT=0
check_char=' '
echo -e "\e[32m"
for line  in "${CFGFILES[@]}"
do
pif1=`echo $line | cut -f 1 -d '|'`
pif2=`echo $line | cut -f 2 -d '|'`
cf=$NODEPATH/$pif1
nm=$pif2

IFS=' ' 
if test -f $cf; then
check_char=' '
else 
check_char='x'
fi 
IFS='%' 

printf "  %-2s %s %s\n"  $CNT $check_char $nm 


let CNT++
done
echo -e "\e[0m"

echo  '  ---------------------------------------------'
echo  
echo  -n '  Enter the number of the file you wish to edit (Ctrl+C to quit) : ' 
read n

#Input must be number or 'a'
RE='^[0-9]+$'
if ! [[ $n =~ $RE ]] ; then
  echo "Invalid config file. Enter the number shown in the first column against the file you want to edit"
  exit 1
fi

if [ "$n" -gt "${#CFGFILES[@]}" ]; then 
	echo "Invalid config file. Enter the number shown in the first column against the file you want to edit"
	exit 
else
	line="${CFGFILES[$n]}"
	pif1=`echo $line | cut -f 1 -d '|'`
	cf=$(echo -e $NODEPATH/$pif1 | tr -d ' ') 
	echo "  Editing $cf "
	$EDITOR $cf 
	echo
	$INSTALL_PREFIX/bin/trisulctl_probe checkxml $cf 
	
fi 


