#!/bin/bash
###############################################################################
# Copyright (c) 2018-2019 Methodics, Inc.
# All Rights Reserved.
#
# This source file is confidential and the proprietary information of
# Methodics, Inc. and its receipt or possession does not convey any rights to
# reproduce or disclose its contents, or to manufacture, use or sell anything
# it may describe. Reproduction, disclosure or use without Methodics, Inc.
# specific written authorization is strictly forbidden.
################################################################################

################################################################################
# FILE
#       mdx_wdog_stat_wrapper.sh - script called by Linux Watchdog Daemon to
#       run user-defined tests.
#
# SYNOPSIS
#       mdx_wdog_stat_wrapper.sh test
#       mdx_wdog_stat_wrapper.sh repair <errcode>
#
# OPTIONS
#       test - used when the the Linux Watchdog Daemon calls this script to
#              run user defined tests.
#       repair <errcode> - used when the script is called after it is called
#                          with the 'test' option and exits with a non-zero
#                          <errcode>.
#
# DESCRIPTION
#       This script is called by the Linux Watchdog Daemon. When called to
#       run user-defined tests, the 'test' option is passed in. If the script
#       exits with a non-zero error code, <errcode>, then the script is called
#       again with the 'repair' option and <errcode> arguments. If the script
#       again exits with a non-zero error code, the Linux Watchdog Daemon will
#       reboot the node.
#
# EXAMPLE
#      mdx_wdog_stat_wrapper.sh test
#      mdx_wdog_stat_wrapper.sh repair 1
#
# ENVIRONMENT
#       The Linux Watchdog Daemon package must be installed.
#       The daemon calls this script as root.
#       This script outputs information into a file given by the variable 'f'
#       below. To help determine the cause of a reboot, analyze this file,
#       the Linux Watchdog Daemon log file at /var/log/messages, and the
#       PiCache Watchdog Monitor's log file.
################################################################################

python=/mdx/tools/methodics/devtools/2019-02-14/env/python
stat_script=/home/bob/dev/git/ipcache/src/methodics/wdog/mdx_wdog_stat.py
basenm=`basename ${stat_script}`
config_file=/home/bob/dev/git/ipcache/conf/picache.conf
f=/etc/watchdog.d/wrapper-out.txt
stat=0

# set verbose to ${TRUE} to output verbose log messages to 'f'
TRUE=1
FALSE=0
verbose=${FALSE}

# Debug Output function Outputs to $f if $verbose is $TRUE.
#
dbgOutp()
{
  if [ ${verbose} -eq ${TRUE} ]; then
    date=`date`
    echo -e "$date: $@" >> ${f}
  fi
}

# Error Output function. Always outputs to $f.
#
errOutp()
{
    date=`date`
    echo -e "$date: $@" >> ${f}
    #echo -e "$date: $@" 1>&2
}

# The main function.
#
main()
{
  if [ "$1" = "test" ]; then

    dbgOutp "got 'test'"
    if [ ! -x ${python} ]; then
      errOutp "$python is not present or is not executable"
      stat=2 # 2 = ENOENT, No such file or directory
    fi
    if [ ! -x ${stat_script} ]; then
      errOutp "$stat_script is not present or is not executable"
      stat=2 # 2 = ENOENT, No such file or directory
    fi
    if [ ! -r ${config_file} ]; then
      errOutp "$config_file is not present or is not readable"
      stat=2 # 2 = ENOENT, No such file or directory
    fi
    if [ ${stat} -eq 0 ]; then
      dbgOutp "Calling $basenm..."
      ${python} ${stat_script} -c ${config_file}
      stat=$?
      if [ ${stat} -eq 0 ]; then
        dbgOutp "$basenm exited with $stat"
      else
        errOutp "$basenm exited with $stat"
      fi
    fi

  elif [ "$1" = "repair" ]; then

    errOutp "got 'repair' for error code $2"
    # there is no repair capability...exit with the same error code as passed-in
    stat=$2

  else

    errOutp "got unexpected '$1', arg2=$2, arg3=$3"
    stat=22 # 22 = EINVAL, Invalid argument

  fi

  if [ ${stat} -eq 0 ]; then
    dbgOutp "exiting with error code $stat\n"
  else
    errOutp "exiting with error code $stat\n"
  fi

  exit ${stat}
}

# invoke the main function and exit from there
main "$@"
