#!/pkg/bin/ksh
# show tech-support placed command
#
# Feb 2010, Kiran Rathna Kempahanumaiah
#
# Copyright (c) 2010, 2013-2014, 2018, 2020 by cisco Systems, Inc.
# All rights reserved.
#----------------------------------------------------------------------------
# This script collects all enf & cdm traces..

ENF_LTRACE_DIRS="/dev/shmem/ltrace/enf"
CDM_LTRACE_DIRS="/dev/shmem/ltrace/cdm"


#jobid to hex
to_hex() {
    if (($# == 0)); then
        echo "no jobids to convert"
        return 
    fi

    let int="${1}"
    let size="${2:-0}"  #length of output hex string

    str="digit=( 0 1 2 3 4 5 6 7 8 9 a b c d e f )"
    if [[ $KSH_VERSION == *"KSH"* ]]; then 
        str="set -A digit '0 1 2 3 4 5 6 7 8 9 A B C D E F'"
    fi  
    eval $str || echo "If facing syntax error, this is an unexpected shell  
    			version. We expect pdksh 5.2.14 or bash 4.2.46"
    hex=''

    while ((int > 0)); do
         hex="${digit[$((int % 16))]}${hex}"
         int=$((int / 16))
    done

    if ((size != 0)); then
        typeset -Z${size} zeros=0
        typeset -R${size} hex="${zeros}${hex}"
    fi

    echo ${hex}
    return 
}


if [[ "${0##*/}" == "show_tech_enf" ]]; then
   (
   date
   uname -a
   for DIR in $(ls $ENF_LTRACE_DIRS)
   do
       hex_jid=$(to_hex "$DIR" )
       echo "ENF traces for job_id $DIR"
       echo "=============================================="
       show_ltrace_enf -j 0x$hex_jid -T all
       echo "=============================================="
   done
   
   for DIR in $(ls $ENF_LTRACE_DIRS)
   do
       hex_jid=$(to_hex "$DIR" )
       echo "ENF registration for job_id $DIR"
       echo "=============================================="
       show_enf -j 0x$hex_jid 
       echo "=============================================="
   done
   
   ) 2>&1
fi
