#/bin/bash

#-----------------------------------------------------------------
# infra/xoslib/test/src/infra/xostrace_ng
#
# Sep 2015, Stefan Schaeckeler
#
# Copyright (c) 2015 by cisco Systems, Inc.
# All rights reserved.
#------------------------------------------------------------------


if [ $# -eq 0 ] || [ "$1" = "-h" ]; then
  echo "$0 is a wrapper around xostrace for linux based routers."
  echo "It supports all of xostrace's options (see xostrace -h) plus option -N <node id>"
  echo "for retrieving traces from remote location <node id>. A location's node id can be"
  echo "retrieved with 'nslot all', e.g. 0x520"
  exit
fi


OPT=
BUFS=
ssh_cmd="ssh -o StrictHostKeychecking=no -o LogLevel=QUIET -l root"

while true
do
   case $1 in
      -N) nodeid=$2; shift; shift;;
      -[r\|s\|m\|n\|L]) OPT="${OPT} $1 $2"; shift; shift;;   # option arguments
      -*) OPT="${OPT} $1"; shift;;                        # no option arguments
       *) BUFS="$*"; break;;
   esac
done



if [ "${nodeid}" ] ; then
  # remote location via ssh
  ip=$(nodeid_to_ip $nodeid)
  # ip is either the node's ip address or empty on error
  if [ "${ip}" ] ; then
    cmd="${ssh_cmd} ${ip} /pkg/sbin/xostrace ${OPT} ${BUFS}"
  else
    cmd="false"
  fi
else 
  # this loction without ssh
  cmd="/pkg/sbin/xostrace ${OPT} ${BUFS}"
fi

eval $cmd