#!/pkg/bin/ksh
#
# show_inv_cardlib_debug command
#
# Oct 2014, Srihari Kondapaneni
#
# Copyright (c) 2014-2015 by cisco Systems, Inc.
#
# All rights reserved.
#----------------------------------------------------------------------------
# This script collects all cardlib traces for inv_proxy and sdr-invmgr

INV_CARDLIB_LTRACE_DIRS="/dev/shmem/ltrace/inv_cardlib"

#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

    declare -a digit
    digit=( 0 1 2 3 4 5 6 7 8 9 a b c d e f )
    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_inv_cardlib_debug" ]]; then
   (
   date
   uname -a
   for DIR in $(ls $INV_CARDLIB_LTRACE_DIRS)
   do
       echo "=============================================="
       hex_jid=$(to_hex "$DIR" )
       sysmgr_show -o -p 0x$hex_jid
       echo ""
       echo "=============================================="
       echo "inv_cardlib traces for job_id $DIR"
       show_inv_cardlib_trace -j $DIR 
       echo "=============================================="
   done
   ) 2>&1
fi
