
#!/bin/sh
# File	: ping_shutdown_countdown.sh
# By	: MaartenDeBoer.nl, 241212
# Subject	: Script to test a DEST en after some time sutdown system
PGM=`basename $0|cut -d\. -f1`
VER="0.1"
LOG="${HOME}/log/${PGM}.log"
DEST="44.137.36.254"
CNTFILE="${HOME}/data/${PGM}.cnt"
CNT_INIT=5

# If CNTFILE is not there, then create with init value
if [ ! -f ${CNTFILE} ]; then
  CNT=${CNT_INIT}
  echo ${CNT} > ${CNTFILE}
fi

ping ${DEST} -W 2 -c 2 1> /dev/null 2>&1
EC=${?}
if [ ${EC} -gt 0 ]; then
  CNT=`cat ${CNTFILE}`
  CNT=`expr ${CNT} - 1`
  echo "  ${PGM}: Shuttingdown countdown : ${CNT}"|logger -p emerg
  echo ${CNT} > ${CNTFILE}
  if [ ${CNT} -le 0 ]; then
# (count)down reached.
    echo "  ${PGM}: Shutting down ..."|tee -a ${LOG}|logger -p emerg
    rm ${CNTFILE}
    sleep 5
    sudo init 0
  fi  # CNT >= 0
else
# When ping OK. Set cnt to init vaule
  CNT=${CNT_INIT}
  echo ${CNT} > ${CNTFILE}
fi  # EC > 0

exit 0

