#!/bin/bash
# -------------------------------------------------------------------
# Copyright (c) 2020, 2022 by Cisco Systems, Inc.
# All rights reserved.
# Author: Wilson Talaugon (wtalaugo@cisco.com)
#
# xr_file_monitor
# Script that monitors changes in any given file in the list using
# inotifywait() and takes an action accordingly. It is spawned by
# the xr_startup.sh script and terminated by the xr_stop.sh.
# -------------------------------------------------------------------

source /etc/init.d/pd-functions &> /dev/null
if declare -F pd_is_emerg_shell_supported &> /dev/null ; then
    if [ $(pd_is_emerg_shell_supported) -eq 0 ]; then
        exit 0
    fi
else
    exit 0
fi

# archive folders in DR
DR_DIR="/mnt/dr_part"
DR_LVM_LOG_DIR="$DR_DIR/lvm/log"
DR_UTILS_DIR="$DR_DIR/utils"
LVM_LOG_FILE="/var/log/lvm2.log"
PD_UTIL_SCRIPT="/usr/local/etc/fpga-functions"
PD_CONFIG_FILE="/usr/local/etc/fpga_platform_data_config"
PD_FUNCTION_FILE="/etc/init.d/pd-functions"
PD_DBE_DEFS_FILE="/usr/local/etc/doorbell_event_defs"
PD_SEND_DBE_SCRIPT="/usr/local/etc/send_doorbell_event.sh"
EMERGENCY_SETUP_SCRIPT="/etc/rc.d/init.d/xr_emergency_mode_setup"
EMERGENCY_RECOVERY_SCRIPT="/etc/rc.d/init.d/xr_recovery"

# set default permission to root
umask 077

# pre-event actions
# -----------------------------------------------------------------
if [ -d $DR_DIR ]; then
   # create the lvm log archive folder in DR, if it does not exist
   mkdir -p $DR_LVM_LOG_DIR
   # create an empty LVM log file, if it does not exist
   touch $LVM_LOG_FILE
   # create utilities directory in DR, if it does not exist
   mkdir -p $DR_UTILS_DIR
   # copy PD files
   if [[ -f $PD_UTIL_SCRIPT && -f $PD_CONFIG_FILE ]]; then
       cp $PD_UTIL_SCRIPT $PD_CONFIG_FILE $DR_UTILS_DIR
   fi
   # copy files for doorbell event
   if [[ -f $PD_FUNCTION_FILE && -f $PD_DBE_DEFS_FILE && -f $PD_SEND_DBE_SCRIPT ]]; then
       cp $PD_FUNCTION_FILE $PD_DBE_DEFS_FILE $PD_SEND_DBE_SCRIPT $DR_UTILS_DIR
   fi
   # copy the emergency mode setup script
   if [ -f $EMERGENCY_SETUP_SCRIPT ]; then
       cp $EMERGENCY_SETUP_SCRIPT $DR_UTILS_DIR
   fi
   # copy the emergency recovery script
   if [ -f $EMERGENCY_RECOVERY_SCRIPT ]; then
       cp $EMERGENCY_RECOVERY_SCRIPT $DR_UTILS_DIR
   fi
   /usr/bin/logger -t xr_file_monitor "File monitoring started. DR [$DR_DIR] archive folders were created."
else
   /usr/bin/logger -t xr_file_monitor "File monitoring started. Unable to create DR [$DR_DIR] archive & backup folders!"
fi
# -----------------------------------------------------------------

# monitored file event handling
# -----------------------------------------------------------------

# enumerate the files to monitor
files_to_monitor=(
   "$LVM_LOG_FILE"
)

function lvmlog_close_write_event()
{
   filename=$1
   case $filename in
      # LVM log file
      lvm2*)
         if [ -d $DR_LVM_LOG_DIR ]; then
            # copy the LVM log file
            cp $LVM_LOG_FILE $DR_LVM_LOG_DIR
            /usr/bin/logger -t xr_file_monitor "LVM log file $filename was updated. Archived to DR."
         else
            /usr/bin/logger -t xr_file_monitor "LVM log file $filename was updated. Unable to archive to DR!"
         fi
         ;;
   esac
}

# monitor any updates to the files in the list
inotifywait -q -m -e close_write ${files_to_monitor[@]} --format "%e %w" |
# identify the updated file and take corresponding action
while read event source; do
   filename="$(basename $source)"
   case $filename in
      # LVM log file event
      lvm2*)
         lvmlog_close_write_event $filename
         ;;
   esac
done
# -----------------------------------------------------------------
# exit if there is a problem monitoring the files
if [ $? -ne 0 ]; then
   /usr/bin/logger -t xr_file_monitor "Invalid event! Exiting."
   exit 1
fi
