# trisbashrc - useful shortcuts for working with trisul hub and probe nodes 
# 
# usage :  source /usr/local/share/trisul-probe/trisbashrc domainX probeX contextX
# 		   default is domain0 probe0 context0 
#

#Usage 
Usage(){
 echo -e  " Usage   : source trisulbashrc <domain> <hub/probe> context\n"
 echo -e  " Defaults: domain0 hub0 context0\n"
 echo -e  " Example : source trisulbashrc domain0 hub0 context0"
 echo -e  "           source trisulbashrc subnet1"
 
}
# get datadir , logdir from xml file
get_xml_value(){
  if test -e $CONFIGFILE; then
    echo `cat $CONFIGFILE  | tr "\n", " "  | grep -o "<$1>.*</$1>"| cut -f2 -d">" | cut -f1 -d"<"| xargs`
  else
   echo "Config file not found"
  fi
}

# we need a profile to dump config db
get_config_db_path(){
  read -p "Enter the profile name[default = profile0] :" profile
  if [ -z "$profile" ]; then
    PROFILE="profile0"
  else
    PROFILE=$profile
  fi
  
  CONFIGDBPATH="$INSTALL_PREFIX/var/lib/trisul-config/$DOMAIN/$CONTEXT/$PROFILE/TRISULCONFIG.SQDB"
  if ! test -e $CONFIGDBPATH; then
   echo "Config DB not found $CONFIGDBPATH"
  else 
   eval "$BINDIR/sqlite3_probe_shell $CONFIGDBPATH"
  fi
  

}

#tail ns,fs,qs log
tailflog(){

  local patt lf
  patt=$1
  lf=`ls -rt $LOGDIR/$patt-00*|tail -1`
  echo '##############################################################'
  echo $lf
  echo '##############################################################'
  if [ ! -z "$lf" ]; then
    tail -f $lf
  else
    echo "Log file not found $lf"
  fi
}


#Check the script placed at correct location where the env.eff file  placed
ENVFILE=env.eff
if [ ! -e $ENVFILE ]; then
source /usr/local/share/trisul-probe/env.eff 
else
source env.eff 
fi



#check argument list
if [[ ( "$#" -gt 3 ) || ( "$#" -eq 2 ) ]]; then
Usage
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 

# we need to know hub or probe to get config file path
if [[ $NODE =~ ^probe ]]; then
  BASENODE="probe"
fi

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

#get the hub/probe path
NODEPATH="trisul-$BASENODE/$DOMAIN/$NODE/$CONTEXT"

#get the config file for both hub/probe
CONFIGFILE="$INSTALL_PREFIX/etc/$NODEPATH/trisul${BASENODE^}Config.xml"

if ! test -e $CONFIGFILE; then
 echo "Unable to find config file : $CONFIGFILE"
else
  echo "Config file found $CONFIGFILE"
  echo "Switching to $DOMAIN/$NODE/$CONTEXT"
fi

alias vi.x="vi $CONFIGFILE"
alias cd.s="cd $INSTALL_PREFIX/share/trisul-$BASENODE/;pwd"
alias cd.x="cd $INSTALL_PREFIX/etc/$NODEPATH/;pwd"

LOGDIR=$(get_xml_value "Logdir")
alias cd.l="cd $LOGDIR/;pwd"

CTXROOT=$(get_xml_value "DBRoot")
alias cd.root="cd $CTXROOT/;pwd"
alias cd.locallua="cd $CTXROOT/config/local-lua;pwd"
alias cd.run="cd $CTXROOT/run;pwd"


BINDIR=$(get_xml_value "BinDirectory")
alias sl="$BINDIR/sqlite3_probe_shell"
alias sl.c="get_config_db_path"
alias tailf.ns="tailflog ns "
alias rat.rx="$BINDIR/rat $CONFIGFILE rxring"
alias rat.pf="$BINDIR/rat $CONFIGFILE pfring"
alias rat.af="$BINDIR/rat $CONFIGFILE afp"
alias rat.lp="$BINDIR/rat $CONFIGFILE lpcap"
alias rat.ffpcap="$BINDIR/rat $CONFIGFILE ffpcap"
alias tp="$BINDIR/trisulctl_probe"
alias edit.cfg="$INSTALL_PREFIX/share/trisul-$BASENODE/cfgedit $RAWCONTEXT"
