  #!/pkg/bin/ksh
  #
  # March 2016
  #
  # Based on template from Jing Zhang (jingzh2) at:
  # /spirit/issu/issudir/scripts/issu_debug_script_template
  #
  #  Copyright (c) 2016 by cisco Systems, Inc.
  #  All rights reserved.
  #
  # Take a snapshot of the application and store it on persistent storage before
  # the node is shut down on ISSU Abort.
  # 
  
  # Get generic information - this will always be called by the script.
  get_general_info()
  {
      # Dump the FIA ltrace buffers
      echo "----------------------------------------------------------------"
      echo "## `date +%H:%M:%S` Output of: show_fia_ltrace -I .*"
      exec_timeout -t 10 $BIN_DIR/show_fia_ltrace -I .*
  }
  
  # Get debug information - this is called when the reason_enum option is set
  get_debug_info()
  {
      # Empty
      echo "----------------------------------------------------------------"
  }
  
  ###############################################################################
  # Start of main script
  ###############################################################################
  
  USAGE="[-e <reason_enum>] [-p pid_to_trace] node_name calling_process              \
[process_instance]"
  
  unset PID
  unset REASON_ENUM 
  
  LOG_CMD=/pkg/sbin/sysmgr_log
  BIN_DIR=/pkg/bin
  DETAIL_LOG=0
  
  while getopts p:e: c
  do
      case $c in
      p) PID=$OPTARG;;
      e) REASON_ENUM=$OPTARG;;
      *) echo "Usage: $0 $USAGE" >&2; exit 1;;
      esac
  done
  
  # Move forward in the argument list
  shift $(($OPTIND - 1))
  
  # Make the arguments up to 'calling process' compulsory
  if [ -z $2 ]
  then
      echo "Usage: $0 $USAGE" >&2
      exit 1
  fi
  
  NODE=$1
  CALLING_PROCESS=$2
  
  # Get optional argument 'process_instance'
  if [ -n $3 ]
  then
      PROC_INST=$3
  fi
  
  # Ensure there are no trailing extra arguments after 'opt_tag'
  if [ -n "$4" ]
  then
      echo "Usage: $0 $USAGE" >&2
      exit 1
  fi
  
  # Find the application storage device, using 'showtech' for now as issudir is not
  # defined as one of the appmedia_type currently
  BOOTDEV=`/pkg/sbin/getappmedia showtech`
  
  # Select log device by first available in order of priority.
  # If we are on a LC, first attempt to pick a device on the RP.
  for device in harddisk: $BOOTDEV bootflash:
  do 
      if [ -e /$device ]; then
          ISSU_DEBUG_DEV=$device; break; 
      fi
  done
  
  # If there is no device available for logging, exit
  if [ -z $ISSU_DEBUG_DEV ]; then
      echo "Logging device is not available; exiting..." >&2; exit 1
  fi
  
  # set up debug directory on the local log device based on environment variable 
  # ISSU_TRACE_DIR inheritted from the parent process spawning this script.
  # DO NOT CHANGE THE ENVIRONMENT VARIABLE !!!
  ISSU_DEBUG_DIR=/$ISSU_DEBUG_DEV/$ISSU_TRACE_DIR
  
  # Decide whether the script will log in detail or not
  # If the available device is bootflash; make it short
  # because of space.
  if [ $device != "bootflash:" ]; then
      DETAIL_LOG=1
  fi
  
  # Create directory if they don't exist
  if [ ! -d $ISSU_DEBUG_DIR ]; then
      mkdir $ISSU_DEBUG_DIR
      if [ $? -ne 0 ]; then
          exit 1
      fi
  fi
  
  if [ -z $PROC_INST ]
  then
      ISSU_LOG_FILE=$NODE.$CALLING_PROCESS.log
  else
      ISSU_LOG_FILE=$NODE.$CALLING_PROCESS.$PROC_INST.log
  fi
  
  ISSU_DEBUG_LOG=$ISSU_DEBUG_DIR/$ISSU_LOG_FILE
  
  # Generate the snap
  (
  echo "$0 invoked by ($CALLING_PROCESS) at `iosclock -d 0x0`."
  
  get_general_info
  
  if [[ ($REASON_ENUM -eq 1) ]]; then
      get_debug_info
  fi
  
  # If we have a pid, get the attach_process output
  if [ -n "$PID" ]
  then
      echo "------------------------------------------------------------"
      echo "Output of attach_process -p $PID -i 1"
      $BIN_DIR/attach_process -p $PID -i 1 -v -f
  fi
  
  ) > $ISSU_DEBUG_LOG 2>&1
  
  ISSU_DEBUG_TAR_DIR=/harddisk:/$ISSU_TAR_DIR
  if [ -e $ISSU_DEBUG_LOG ]; then
      scp -o StrictHostKeychecking=no $ISSU_DEBUG_LOG                                \
root@$LEAD_IPADDR:$ISSU_DEBUG_TAR_DIR/$ISSU_LOG_FILE
  fi

