# implement "show tech-support brief"
# usage: tech-support [brief] [ save [ <filename> ] ]
# NOTE: this file is sourced, NOT executed

function header {
    echo
    echo ----------------
    echo "$*"
    echo ----------------
}

# by default send to stdout
OUT=1
FLAG=0
REMOTE=0
DEFAULT_PATH=/config/support
DEFAULT_GROUP=users

do_rotate ()
{
  local count=`ls -t $DEFAULT_PATH/*.tech-support.* 2>/dev/null |wc -l`
  if (( count >= 100 )); then
    local dfile=`ls -t $DEFAULT_PATH/*.tech-support.* 2>/dev/null |tail -1`
    rm -f $dfile >&/dev/null \
      && echo "Removed old tech-support brief output file '$dfile'"
  fi
}

HOSTNAME=`hostname`
CURTIME=`date +%F-%H%M%S`
if [ "$1" == "save-uncompressed" ]; then
  FLAG="1"
elif [ "$1" == "save" ]; then
  FLAG="0"
fi

if [ "$1" == "save" ] || [ "$1" == "save-uncompressed" ]; then
  # "save" or save-uncompressed is specified. save output to file.
  OUT="$HOSTNAME.tech-support.$CURTIME.txt"
  if [ -n "$2" ]; then
    if [[ "$2" =~ scp:///* || "$2" =~ ftp:///* ]]; then
      REMOTE="1"
    else
      #file to be save locally
      OUT="$2.$OUT"
    fi
  fi

  if [[ $OUT != /* ]]; then
    # it's not absolute path. save in default path.
    mkdir -p $DEFAULT_PATH >& /dev/null
    chgrp $DEFAULT_GROUP $DEFAULT_PATH >& /dev/null
    chmod 775 $DEFAULT_PATH >& /dev/null
    OUT="$DEFAULT_PATH/$OUT"
    do_rotate
  fi
  if ! touch $OUT >& /dev/null; then
    echo "Cannot create tech-support file '$OUT'"
    exit 1
  fi
  if [ $REMOTE != "1" ]; then
    echo "Saving output to '$OUT'..."
  fi
fi

(
export PATH=/sbin:/usr/sbin:$PATH

header Show Tech-Support Brief
header CONFIGURATION


header VyOS Version and Package Changes
show version all

header Running configuration
show configuration

header INTERFACES

header Interfaces
show interfaces

header ROUTING

function show_route_limit ()
{
  NUM=$(show $1 route $2 | wc -l)
  # subtract 3 lines of header
  [ $NUM -gt 3 ] &&  NUM=$[$NUM - 3]
  OUTPUT=$(echo show $1 route $2 \(total $NUM\))
  CMD="show $1 route $2"
  if [ $3 -eq 0 ]
  then
    header $OUTPUT
    vtysh -c "$CMD"
  else
    header "$OUTPUT- limit $3"
    vtysh -c "$CMD" | head -n $3
  fi
}

#
# show all connected/static, limit the output others and include a total
#
show_route_limit ip connected 0
show_route_limit ip static    0
show_route_limit ip rip  500
show_route_limit ip ospf 500
show_route_limit ip bgp  500
show_route_limit ip ''   500

show_route_limit ipv6 connected 0
show_route_limit ipv6 static    0
show_route_limit ipv6 ripng 500
show_route_limit ipv6 ospf6 500
show_route_limit ipv6 bgp   500
show_route_limit ipv6 ''    500


header Recent 100 Log Messages
show log tail 100

header "END OF TECH-SUPPORT BRIEF FILE"
) 1>&$OUT 2>&1

if [ $OUT != "1" ]; then
  chgrp $DEFAULT_GROUP $OUT >& /dev/null
  chmod 664 $OUT >& /dev/null
  if [ $FLAG == "0" ]; then
    gzip $OUT
    OUT=$OUT.gz
    if [ $REMOTE != "1" ]; then
      echo "File $OUT is the compressed file."
    fi
  fi

  if [ $REMOTE != "1" ]; then
    echo "Done."
  fi
fi

if [ $REMOTE == "1" ]; then
  python3 -c "from vyos.remote import upload; upload(\"$OUT\", \"$2\")"
fi
