#!/bin/sh
CORE_FILES_DIR=/core_files
FFDC_FILES_DIR=/core_files/ffdc_data
MNT_DIR=/mnt

PATH=/bin:/usr/bin:/sbin:/usr/sbin

# we don't care if the total core files amounts to less that 5 K bytes
# LOWER_LIMIT will set this limit under with we don't care to report
LOWER_LIMIT=5

# First, if there is no CORE_FIELS_DIR then we are done :-)
if [ ! -d ${CORE_FILES_DIR} ]
then
	exit
fi

SLOT=`sin 2>/dev/null | grep "Slot No:" | cut -d ':' -f 2 | cut -d ' ' -f 2`
SLOT_STDBY=0

if [ -n "$SLOT" ]; then
    if [ 15 -eq $SLOT ]; then
        SLOT_STDBY=16
    elif [ 16 -eq $SLOT ]; then
        SLOT_STDBY=15
    elif [ 1 -eq $SLOT ]; then
        SLOT_STDBY=2
    elif [ 2 -eq $SLOT ]; then
        SLOT_STDBY=1
    fi
else
    SLOT_STDBY=0
fi

#To find the standby GOS IP
PARTITION=1
if [ "`cat /proc/device-tree/label 2>/dev/null`" = "p2-linux" ]; then
    PARTITION=2
fi
if [ "$PARTITION" == 2 ]; then
     (( PARTITION = $PARTITION - 1 ))
else
    (( PARTITION = $PARTITION + 1 ))
fi


# let us figure out how much core files are occupying...
ROOTkbytes=`/usr/bin/du -sk ${CORE_FILES_DIR} | cut -f1`
MNTkbytes=`/usr/bin/du -sk ${MNT_DIR}/${CORE_FILES_DIR} | cut -f1`
Ffdckbytes=`/usr/bin/du -sk ${FFDC_FILES_DIR} | cut -f1`
MNTFfdckbytes=`/usr/bin/du -sk ${MNT_DIR}/${FFDC_FILES_DIR} | cut -f1`
corekbytes=`expr ${ROOTkbytes} + ${MNTkbytes} - ${Ffdckbytes} - ${MNTFfdckbytes}`
ffdckbytes=`expr ${Ffdckbytes} + ${MNTFfdckbytes}`

st_numberOfCoreFiles=0
st_numberOfFfdcFiles=0

#excluding fdet and ptlog directories
ROOTnumberOfCoreFiles=`/usr/bin/find /${CORE_FILES_DIR}/  \( -name "*core*" \)  -print | grep -v fdet* | grep -v ptlog* | wc -l | sed -e's/[  ]//g'`

MNTnumberOfCoreFiles=`/usr/bin/find ${MNT_DIR}/${CORE_FILES_DIR}/  \( -name "*core*" \)  -print | grep -v fdet* | grep -v ptlog* | wc -l  | sed -e's/[ 	]//g'`

numberOfCoreFiles=`expr ${ROOTnumberOfCoreFiles} + ${MNTnumberOfCoreFiles}`

ROOTnumberOfFfdcFiles=`/usr/bin/find /${FFDC_FILES_DIR}/ \( -name "*ffdc*" \) -type f -print | \
wc -l | sed -e's/[ 	]//g'`
MNTnumberOfFfdcFiles=`/usr/bin/find ${MNT_DIR}/${FFDC_FILES_DIR}/ \( -name "*ffdc*" \) -type f -print | \
wc -l | sed -e's/[ 	]//g'`
numberOfFfdcFiles=`expr ${ROOTnumberOfFfdcFiles} + ${MNTnumberOfFfdcFiles}`

if [ ${SLOT_STDBY} -eq 0 ]; then

st_ROOTnumberOfCoreFiles=`/usr/bin/rsh -n 127.2.$PARTITION.0 ROLE_ID=root LOGIN_ID=root CHASSIS_ROLEID=0 FABOS_SWITCHNO=$FABOS_SWITCHNO CURRENT_VF=$CURRENT_VF CURRENT_AD=$CURRENT_AD \
 "/usr/bin/find /${CORE_FILES_DIR}/  \( -name "*core*" \) -print | grep -v fdet* | grep -v ptlog* | wc -l | sed -e's/[ 	]//g'"`
st_MNTnumberOfCoreFiles=`/usr/bin/rsh -n 127.2.$PARTITION.0 ROLE_ID=root LOGIN_ID=root CHASSIS_ROLEID=0 FABOS_SWITCHNO=$FABOS_SWITCHNO CURRENT_VF=$CURRENT_VF CURRENT_AD=$CURRENT_AD \
"/usr/bin/find ${MNT_DIR}/${CORE_FILES_DIR}/ \( -name "*core*" \) -print | grep -v fdet* | grep -v ptlog* | wc -l  | sed -e's/[ ]//g'"`


st_numberOfCoreFiles=`expr ${st_ROOTnumberOfCoreFiles} + ${st_MNTnumberOfCoreFiles}`

st_ROOTnumberOfFfdcFiles=`/usr/bin/rsh -n 127.2.$PARTITION.0 ROLE_ID=root LOGIN_ID=root CHASSIS_ROLEID=0 FABOS_SWITCHNO=$FABOS_SWITCHNO CURRENT_VF=$CURRENT_VF CURRENT_AD=$CURRENT_AD "/usr/bin/find /${FFDC_FILES_DIR}/ \( -name "*ffdc*" \) -type f -print | wc -l | sed -e's/[  ]//g'"`

st_MNTnumberOfFfdcFiles=`/usr/bin/rsh -n 127.2.$PARTITION.0 ROLE_ID=root LOGIN_ID=root CHASSIS_ROLEID=0 FABOS_SWITCHNO=$FABOS_SWITCHNO CURRENT_VF=$CURRENT_VF CURRENT_AD=$CURRENT_AD "/usr/bin/find ${MNT_DIR}/${FFDC_FILES_DIR}/ \( -name "*ffdc*" \) -type f -print | wc -l | sed -e's/[ 	]//g'"`
st_numberOfFfdcFiles=`expr ${st_ROOTnumberOfFfdcFiles} + ${st_MNTnumberOfFfdcFiles}`
fi

numberOfCoreFiles=`expr ${numberOfCoreFiles} + ${st_numberOfCoreFiles}`
numberOfFfdcFiles=`expr ${numberOfFfdcFiles} + ${st_numberOfFfdcFiles}`


if [ ${numberOfCoreFiles} -ge 1 ]
then
	/fabos/sbin/supportautoftp -f /core_files >> /tmp/ss.log 2>&1
	if [ $? -ne 0 ]
	then
	printf "
*** CORE FILES WARNING (%-20s) ***
%s KBytes in %s file(s)
use \"copy support\" command to upload 

" "`/bin/date +'%D - %T'`"  ${corekbytes} ${numberOfCoreFiles} > /dev/console
	fi
fi

if [ ${numberOfFfdcFiles} -ge 1 ]
then
	/fabos/sbin/supportautoftp -f /var/trace >> /tmp/ss.log 2>&1
	if [ $? -ne 0 ]
	then
	printf "
*** FFDC FILES WARNING (%-20s) ***
%s KBytes in %s file(s)
use \"copy support\" command to upload

" "`/bin/date +'%D - %T'`"  ${ffdckbytes} ${numberOfFfdcFiles} > /dev/console

	fi
fi
