# trisbashrc - helpful shortcuts for trisul-hub
#
# usage : source /usr/local/share/trisul-hub/trisbashrc 

#Usage 
Usage(){
 echo -e  " Usage   : source trisbashrc <domain> <hub/probe> context\n"
 echo -e  " Defaults: domain0 hub0 context0\n"
 echo -e  " Example : source trisbashrc 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_hub_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
if [ ! -e env.eff ]; then
	source /usr/local/share/trisul-hub/env.eff
else
	source env.eff 
fi


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


#defaults
DOMAIN="domain0"
NODE="hub0"
CONTEXT="context0"
BASENODE="hub"
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

#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 "Config file does not exist : $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"

TRAFFFICDBROOT=$(get_xml_value "TrafficDBRoot")
if [ -z "$TRAFFFICDBROOT" ]; then
  alias cd.m="echo 'No Traffic DB Root found $TRAFFFICDBROOT"
  alias sl.mdump="echo 'DB not found $TRAFFFICDBROOT/METASLICE.SQDB"
else
  alias sl.mdump="$BINDIR/sqlite3_hub_shell $TRAFFFICDBROOT/METASLICE.SQDB \".dump\""
  alias cd.m="cd $TRAFFFICDBROOT/;pwd"
fi


BINDIR=$(get_xml_value "BinDirectory")
alias sl="$BINDIR/sqlite3_hub_shell"
alias sl.c="get_config_db_path"
alias tailf.fs="tailflog fs "
alias tailf.qs="tailflog qs "
alias tailf.is="tailflog is "
alias cd.w="cd /usr/local/share/webtrisul"
alias tailf.ws="tailf $INSTALL_PREFIX/var/log/trisul-hub/webtrisul/production.log"
alias cd.wl="cd $INSTALL_PREFIX/var/log/trisul-hub/webtrisul;pwd"
alias th="$BINDIR/trisulctl_hub" 
alias cd.p0="cd $INSTALL_PREFIX/var/lib/trisul-config/$DOMAIN/$CONTEXT/profile0/;pwd"
