#!/bin/bash

# You can write your own commands by any *.sh
# *.sh file supports Unix/Linux shell command

# Available environment variable
# $EVENT_STAGE when an event occurred, there are two stages for invoking commands.
# When an event occurred, it enters OCCUR stage and invokes related commands.
# When an event finished, it enters FINISH stage and invokes related commands.
# $EVENT represents the event identification, $EVENT_CONDITION represents the condition identification.
#  To understand the value definition of both environment variable, please check online help or user's manual.
# $MODULE_NO represents a UPS module number to help identify which module the event occurs on. (PPB Local Only)

# Set your log file path
# Default directory is software installation directory
LOG_PATH="./advancedCommand.log"

if [ "$EVENT_STAGE" = "OCCUR" ]; then
    # Write commands here.
    # The commands will be run when an event occurs.

    # Write "[date time] $EVENT occur - start" into log file
    echo "[$(date +"%Y-%m-%d %H:%M:%S")] $EVENT occur - start" >> "$LOG_PATH" 2>&1

    # Write command execution result into log file
    yourCommand >> "$LOG_PATH" 2>&1

    # Write "[date time] $EVENT occur - end" into log file
    echo "[$(date +"%Y-%m-%d %H:%M:%S")] $EVENT occur - end" >> "$LOG_PATH" 2>&1
fi

if [ "$EVENT_STAGE" = "FINISH" ]; then
    # Write commands here.
    # The commands will be run when the occurred event is finished.
    # Note: not all occurred events have FINISH stage.

    # Write "[date time] $EVENT finish - start" into log file
    echo "[$(date +"%Y-%m-%d %H:%M:%S")] $EVENT finish - start" >> "$LOG_PATH" 2>&1

    # Write command execution result into log file
    yourCommand >> "$LOG_PATH" 2>&1

    # Write "[date time] $EVENT finish - end" into log file
    echo "[$(date +"%Y-%m-%d %H:%M:%S")] $EVENT finish - end" >> "$LOG_PATH" 2>&1
fi
