All Downloads are FREE. Search and download functionalities are using the official Maven repository.

refcodes-batch.1.0.2.source-code.apply.sh Maven / Gradle / Ivy

There is a newer version: 1.0.5
Show newest version
#!/bin/bash

# /////////////////////////////////////////////////////////////////////////////
# REFCODES.ORG
# =============================================================================
# This code is copyright (c) by Siegfried Steiner, Munich, Germany and licensed
# under the following (see "http://en.wikipedia.org/wiki/Multi-licensing")
# licenses:
# =============================================================================
# GNU General Public License, v3.0 ("http://www.gnu.org/licenses/gpl-3.0.html")
# =============================================================================
# Apache License, v2.0 ("http://www.apache.org/licenses/LICENSE-2.0")
# =============================================================================
# Please contact the copyright holding author(s) of the software artifacts in
# question for licensing issues not being covered by the above listed licenses,
# also regarding commercial licensing models or regarding the compatibility
# with other open source licenses.
# /////////////////////////////////////////////////////////////////////////////

# //////////////////////////////////////////////////////////////////////////////
# See also  http://www.pro-linux.de/artikel/2/111/ein-shellskript-template.html
# //////////////////////////////////////////////////////////////////////////////

# //////////////////////////////////////////////////////////////////////////////
# Standard variables:
# //////////////////////////////////////////////////////////////////////////////
SCRIPT_NAME=$(basename "$0" .sh)
SCRIPT_DIR=$(dirname "$(readlink -f "$0")")

# //////////////////////////////////////////////////////////////////////////////
# Includes:
# //////////////////////////////////////////////////////////////////////////////

# Force a line width of 80:
LOG_LINE_WIDTH=80

. $SCRIPT_DIR/lib-common.inc
. $SCRIPT_DIR/lib-logging.inc

# //////////////////////////////////////////////////////////////////////////////
# Header:
# //////////////////////////////////////////////////////////////////////////////

setForegroundColor cyan
. $SCRIPT_DIR/header.inc
resetFormat

# //////////////////////////////////////////////////////////////////////////////
# Config:
# //////////////////////////////////////////////////////////////////////////////

# setLogFile ${SCRIPT_DIR}/apply.log

# //////////////////////////////////////////////////////////////////////////////
# Variables for the options switches with default values:
# //////////////////////////////////////////////////////////////////////////////
VERBOSE=n
HELP=n
LIST=n
BATCH=n
JOB=""
INTERACTIVE=n
SHUTDOWN=n
CONFIG_DIR=""

# //////////////////////////////////////////////////////////////////////////////
# Variables from / for the non options switch arguments:
# //////////////////////////////////////////////////////////////////////////////
CONFIG=""

# //////////////////////////////////////////////////////////////////////////////
# Varaibles:
# //////////////////////////////////////////////////////////////////////////////

CONFIG_FILE=""
JOB_FILE=""

# //////////////////////////////////////////////////////////////////////////////
# Functions:
# //////////////////////////////////////////////////////////////////////////////

# //////////////////////////////////////////////////////////////////////////////
# Missing variable: 
# $1: The missing variable's name (without prepended "$").
# $2: An optional comment placed in braces intoi the message.
# //////////////////////////////////////////////////////////////////////////////
function warnOnMissingVariable {
	if [ -z "$1" ] ; then
		showError "Argument missing: The function \"warnOnMissingVariable\" expects an argument !!!" >&2
		exit $EXIT_BUG
	fi

	# $VALUE is the value of the variable passed as $1:
	eval "VALUE=\$$1"
	if [ -z "$VALUE" ] ; then
		MESSAGE=""
		if [ ! -z "$2" ] ; then
			MESSAGE="($2) "
		fi
		showWarning "Missing or empty variable \"$1\" $MESSAGE!!!"
	fi
}

# //////////////////////////////////////////////////////////////////////////////
# Missing variable: 
# $1: The missing variable's name (without prepended "$").
# $2: An optional comment placed in braces intoi the message.
# //////////////////////////////////////////////////////////////////////////////
function exitOnMissingVariable {
	if [ -z "$1" ] ; then
		showError "Argument missing: The function \"exitOnMissingVariable\" expects an argument !!!" >&2
		exit $EXIT_BUG
	fi

	# $VALUE is the value of the variable passed as $1:
	eval "VALUE=\$$1"
	if [ -z "$VALUE" ] ; then
		MESSAGE=""
		if [ ! -z "$2" ] ; then
			MESSAGE="($2) "
		fi
		showError "Missing or empty variable \"$1\" $MESSAGE!!!"
		exit $EXIT_ERROR
	fi
}

# //////////////////////////////////////////////////////////////////////////////
# File defined in variable name non exisiting:
# $1 The variable name (without prepended "$") with the (non exisitent) path.
# //////////////////////////////////////////////////////////////////////////////
function exitOnMissingVariablePath {
	if [ -z "$1" ] ; then
		showError "Argument missing: The function \"exitOnMissingVariablePath\" expects an argument !!!" >&2
		exit $EXIT_BUG
	fi

	# $VALUE is the value of the variable passed as $1:
	eval "VALUE=\$$1"
	if [ ! -e "$VALUE" ] && [ ! -L "$VALUE" ] ; then
		showError "Path \"$VALUE\" in variable \"$1\" not found, defined either in the CONFIG \"$CONFIG\" the JOB \"$JOB\" or related script !!!"
		exit $EXIT_ERROR
	fi
}

# //////////////////////////////////////////////////////////////////////////////
# PROPERTIES:
# //////////////////////////////////////////////////////////////////////////////

# //////////////////////////////////////////////////////////////////////////////
# Plainly displays the given property name and value in a defined format: 
# $1: The property to be displayed (without prepended "$").
# //////////////////////////////////////////////////////////////////////////////
function showProperty {
	if [ -z "$1" ] ; then
		showError "Argument missing: The function \"showProperty\" expects an argument !!!" >&2
		exit $EXIT_BUG
	fi

	# $VALUE is the value of the variable passed as $1:
	eval "VALUE=\$$1"
	echo "$1 = \"$VALUE\"" >&2
}

# //////////////////////////////////////////////////////////////////////////////
# Plainly displays the given password property name and value in a defined 
# format, hiding away the password in case it is set: 
# $1: The property to be displayed (without prepended "$").
# //////////////////////////////////////////////////////////////////////////////
function showPasswordProperty {
	if [ -z "$1" ] ; then
		showError "Argument missing: The function \"showPasswordProperty\" expects an argument !!!" >&2
		exit $EXIT_BUG
	fi

	# $VALUE is the value of the variable passed as $1:
	eval "VALUE=\$$1"
	
	if [ -z "$VALUE" ] ; then
		echo "$1 = \"$VALUE\"" >&2
	fi
	if [ ! -z "$VALUE" ] ; then
		echo "$1 = \"************\"" >&2
	fi
}

# //////////////////////////////////////////////////////////////////////////////
# Missing property: 
# $1: The missing property's name (without prepended "$").
# //////////////////////////////////////////////////////////////////////////////
function exitOnMissingProperty {
	if [ -z "$1" ] ; then
		showError "Argument missing: The function \"exitOnMissingProperty\" expects an argument !!!"
		exit $EXIT_BUG
	fi

	# $VALUE is the value of the variable passed as $1:
	eval "VALUE=\$$1"
	if [ -z "$VALUE" ] ; then
		showError "Missing or empty property \"$1\" !!!"
		exit $EXIT_ERROR
	fi
}

# //////////////////////////////////////////////////////////////////////////////
# File defined in $CONFIG non exisiting:
# $1 The property name (without prepended "$") with the (non exisitent) path.
# //////////////////////////////////////////////////////////////////////////////
function exitOnMissingPropertyPath {
	if [ -z "$1" ] ; then
		showError "Argument missing: The function \"exitOnMissingPropertyPath\" expects an argument !!!"
		exit $EXIT_BUG
	fi

	# $VALUE is the value of the variable passed as $1:
	eval "VALUE=\$$1"
	if [ ! -e "$VALUE" ] && [ ! -L "$VALUE" ] ; then
		showError "Path or file \"$VALUE\" not found defined in property \"$1\" of the CONFIG \"$CONFIG\" !!!"
		exit $EXIT_ERROR
	fi
}

# //////////////////////////////////////////////////////////////////////////////
# File defined in $CONFIG has wrong format, i.e. it ends with a slash "/":
# $1 The property name (without prepended "$") with the (wrongly formatted) path.
# //////////////////////////////////////////////////////////////////////////////
function exitOnWrongPropertyPath {
	if [ -z "$1" ] ; then
		showError "Argument missing: The function \"exitOnWrongPropertyPath\" expects an argument !!!"
		exit $EXIT_BUG
	fi

	# $VALUE is the value of the variable passed as $1:
	eval "VALUE=\$$1"
	# if [[ "$VALUE" =~ "/" ]] ; then
	if [[ ${VALUE: -1} == "/" ]] ; then
		showError "Path or file \"$VALUE\" in property \"$1\" of the CONFIG \"$CONFIG\" ends unexpectedly with a slash \"/\" !!!"
		exit $EXIT_ERROR
	fi
}

# //////////////////////////////////////////////////////////////////////////////
# In case a property is set to "y": 
# $1: The property to be tested (without prepended "$").
# //////////////////////////////////////////////////////////////////////////////
function exitOnPropertyYes {
	if [ -z "$1" ] ; then
		showError "Argument missing: The function \"exitOnMissingVariable\" expects an argument !!!"
		exit $EXIT_BUG
	fi

	# $VALUE is the value of the variable passed as $1:
	eval "VALUE=\$$1"
	if [[ $VALUE = y ]] ; then
		showError "The property \"$1\" is enabled (set to \"y\"), disable to continue !!!"
		exit $EXIT_ERROR
	fi
}

# //////////////////////////////////////////////////////////////////////////////
# In case a property is not set to "y": 
# $1: The property to be tested (without prepended "$").
# //////////////////////////////////////////////////////////////////////////////
function exitOnPropertyNo {
	if [ -z "$1" ] ; then
		showError "Argument missing: The function \"exitOnMissingVariable\" expects an argument !!!"
		exit $EXIT_BUG
	fi

	# $VALUE is the value of the variable passed as $1:
	eval "VALUE=\$$1"
	if [[ ! $VALUE = y ]] ; then
		showError "The property \"$1\" is disabled (not set to \"y\"), enable to continue !!!"
		exit $EXIT_ERROR
	fi
}

# //////////////////////////////////////////////////////////////////////////////
# In case a configuration as a whole is not set to "y": 
# $1: The config's "yes/no" property to be tested (without prepended "$").
# //////////////////////////////////////////////////////////////////////////////
function exitOnConfigOff {
	if [ -z "$1" ] ; then
		showError "Argument missing: The function \"exitOnConfigOff\" expects an argument !!!"
		exit $EXIT_BUG
	fi

	# $VALUE is the value of the variable passed as $1:
	eval "VALUE=\$$1"
	if [[ ! $VALUE = y ]] ; then
		showError "The CONFIG \"$1\" is off (not set to \"y\"), enable to continue !!!"
		exit $EXIT_OFF
	fi
}

# //////////////////////////////////////////////////////////////////////////////
# WARN:
# //////////////////////////////////////////////////////////////////////////////

# //////////////////////////////////////////////////////////////////////////////
# In case the last job issued an error code then this method will show a 
#  warning:
# $1: The text to display in case of an error.
# //////////////////////////////////////////////////////////////////////////////
function warnOnError {
	exitCode=$?
	if [ -z "$1" ] ; then
		showError "Argument missing: The function \"exitOnError\" expects an argument !!!"
		exit $EXIT_BUG
	fi

	if [[ $exitCode != 0 ]] ; then
		showWarning "$1 (exit code = $exitCode)"
	fi
}

# //////////////////////////////////////////////////////////////////////////////
# ERROR:
# //////////////////////////////////////////////////////////////////////////////

# //////////////////////////////////////////////////////////////////////////////
# In case the last job issued an error code then this method will exit out
#  accordingly:
# $1: The text to display in case of an error.
# //////////////////////////////////////////////////////////////////////////////
function exitOnError {
	exitCode=$?
	if [ -z "$1" ] ; then
		showError "Argument missing: The function \"exitOnError\" expects an argument !!!"
		exit $EXIT_BUG
	fi

	if [[ $exitCode != 0 ]] ; then
		showError "$1 (exit code = $exitCode)"
		exit $exitCode
	fi
}

# //////////////////////////////////////////////////////////////////////////////
# SWITCHES:
# //////////////////////////////////////////////////////////////////////////////

# //////////////////////////////////////////////////////////////////////////////
# Usage:
# //////////////////////////////////////////////////////////////////////////////
function showUsage {
	logTitle "[$SCRIPT_NAME]: Applies a job on a configuration"
	cat <&2
	# logLine "#"
	# echo "# [$SCRIPT_NAME]: Listing available jobs and config" >&2
	# logLine "#"
	logTitle "[$SCRIPT_NAME]: Listing available jobs and configs"
	# logLine "-"
	# echo "Available jobs (\"*.job\") found:" >&2
	# logLine "-"
	logHeadline "Available jobs (\"*.job\") found:"
	if [ -z "$AVAILABLE_JOBS" ] ; then
		echo "(none found)" >&2
	fi
	if [ ! -z "$AVAILABLE_JOBS" ] ; then
		for FILENAME in $AVAILABLE_JOBS ; do
			echo "* $FILENAME" >&2
			NUM_JOBS=`expr $NUM_JOBS + 1`
		done
	fi
	# echo "--------------------------------------------------------------------------------"  >&2
	# echo "Available configurations (\"*.config\") found:" >&2
	# echo "--------------------------------------------------------------------------------" >&2
	logHeadline "Available configurations (\"*.config\") found:"
	
	if [ -z "$AVAILABLE_CONFIG" ] ; then
		echo "(none found)" >&2
	fi
	if [ ! -z "$AVAILABLE_CONFIG" ] ; then
		for FILENAME in $AVAILABLE_CONFIG ; do
			echo "* $FILENAME" >&2
			NUM_CONFIG=`expr $NUM_CONFIG + 1`
		done
	fi
	# echo "--------------------------------------------------------------------------------"  >&2
	# echo "Found [$NUM_JOBS] jobs and [$NUM_CONFIG] configurations." >&2
	# echo "--------------------------------------------------------------------------------"  >&2
	logHeadline "Found [$NUM_JOBS] jobs and [$NUM_CONFIG] configurations."
}

# //////////////////////////////////////////////////////////////////////////////

MY_OPTARG="$OPTARG"
MY_OPTIND="$OPTIND"
while getopts ':j:lvhbis' OPTION ; do
	case "$OPTION" in
		v) VERBOSE=y
		;;
		h) HELP=y
		;;
		b) BATCH=y
		;;
		j) JOB="$OPTARG"
		;;
		l) LIST=y
		;;
		i) INTERACTIVE=y
		;;
		s) SHUTDOWN=y
		;;
		\?)  showUsage "Unknown option \"-$OPTARG\" !!!"
		exit $EXIT_ERROR
		;;
		:) showUsage "Option \"-$OPTARG\" needs an argument !!!"
		exit $EXIT_ERROR
		;;
		*) showUsage "This should never happen..."
		exit $EXIT_BUG
		;;
	esac
done

shift $(( OPTIND - 1 ))

# -------------------
# Validate arguments:
# -------------------
if ( [[ $HELP = y ]] && ( [[ $LIST = y ]] || [[ $BATCH = y ]] || [[ $INTERACTIVE = y ]] || [[ $SHUTDOWN = y ]] || (( $# != 0 )) ) ) ; then
	showUsage "Illegal combination of options switches with HELP \"-h\" !!!"
	exit $EXIT_ERROR
fi

if ( [[ $LIST = y ]] && ( [[ $HELP = y ]] || [[ $BATCH = y ]] || [[ $INTERACTIVE = y ]] || [[ $SHUTDOWN = y ]] || [ ! -z "$JOB" ] || (( $# > 1 )) ) ) ; then
	showUsage "Illegal combination of options switches with LIST \"-l\" !!!"
	exit $EXIT_ERROR
fi

if ( [[ $BATCH = y ]] && ( [[ $HELP = y ]] || [[ $LIST = y ]]  || ( (( $# != 0 )) && (( $# != 1 )) ) ) ) ; then
	showUsage "Illegal combination of options switches with BATCH \"-b\" !!!"
	exit $EXIT_ERROR
fi

if ( [ ! -z "$JOB" ] && ( [[ $LIST = y ]] ) ) ; then
	showUsage "Illegal combination of options switches with JOB \"-j\" !!!"
	exit $EXIT_ERROR
fi

if ( [[ $INTERACTIVE = y ]] && ( [[ $HELP = y ]] || [[ $LIST = y ]]  || (( $# > 1 )) ) ) ; then
	showUsage "Illegal combination of options switches with INTERACTIVE \"-i\" !!!"
	exit $EXIT_ERROR
fi

# --------------------------------
# Show usage for this script only:
# --------------------------------
if [[ $LIST = n ]] && [ -z "$JOB" ] ; then
	if [[ $HELP = y ]] ; then
		showUsage
		exit $EXIT_SUCCESS
	fi
	showUsage "You must provide a job (\"-j JOB\") !!!"
	exit $EXIT_ERROR
fi
# ----------------------------------------------------
# Show ERROR_MESSAGE (EXIT) when JOB_FILE not exisits:
# ----------------------------------------------------
if [[ $LIST = n ]] ; then
	JOB=${JOB//.job/}
	JOB_FILE="$SCRIPT_DIR/$JOB.job"
	if [ ! -e "$JOB_FILE" ] ; then
		showUsage "The job file \"$JOB_FILE\" does not exist !!!"
		exit $EXIT_ERROR
	fi
fi

# ----------------------------
# Show usage  for JOB as well:
# ----------------------------
if [[ $HELP = y ]] ; then
	showUsage
	. $JOB_FILE
	exit $EXIT_SUCCESS
fi

# -----------------------------
# Set CONFIG arg when HELP = n:
# -----------------------------
if [[ $HELP = n ]] ; then

	# ----------------------------------------
	# Test single CONFIG file (no batch mode):
	# ----------------------------------------
	if [[ $BATCH = n ]] && [[ $LIST = n ]] ; then
		if (( $# != 1 )) ; then
			showUsage "Only and at least the CONFIG_FILE argument is required !!!"
			exit $EXIT_ERROR
		fi
	fi
	if [[ $BATCH = y ]] || [[ $LIST = y ]] ; then
		if (( $# > 1 )) ; then
			showUsage "Only and at least the CONFIG_DIR argument is required !!!"
			exit $EXIT_ERROR
		fi
	fi

	# --------------
	# Loop all ARGS:
	# --------------
	i=1
	for ARG ; do
		case $i in
			1) CONFIG="$ARG"
			;;
		esac
		i=`expr $i + 1`
	done
	
	# ----------------------
	# List CONFIGs and JOBs:
	# ----------------------
	if [[ $LIST = y ]] ; then		
		list
		exit $EXIT_SUCCESS
	fi
	
	# ---------------------------------------
	# Use single CONFIG file (no batch mode):
	# ---------------------------------------
	if [[ $BATCH = n ]] ; then
		CONFIG=${CONFIG//.config/}
		CONFIG="$CONFIG.config"
		if [ -e "$CONFIG" ] ; then
			CONFIG_FILE=$CONFIG
		else
			CONFIG_DIR=$(dirname $(readlink -f $CONFIG))
			# ------------------------------------------------
			# Show ERROR_MESSAGE when CONFIG_FILE not exisits:
			# ------------------------------------------------ 
			if [ ! -z "$CONFIG_DIR" ] ; then
				showUsage  "The CONFIG \"$CONFIG\" file does not exist in the given folder \"$CONFIG_DIR\" !!!"
				exit $EXIT_ERROR
			fi 
			CONFIG_FILE="$SCRIPT_DIR/$CONFIG"
			# ------------------------------------------------
			# Show ERROR_MESSAGE when CONFIG_FILE not exisits:
			# ------------------------------------------------
			if [ ! -e "$CONFIG_FILE" ] ; then
					showUsage  "The CONFIG_FILE \"$CONFIG\" file does not exist in the script folder \"$SCRIPT_DIR\" !!!"
					exit $EXIT_ERROR
			fi
		fi
		# -----------
		# Start EXEC:
		# -----------
		# logLine "#"
		# echo "# [$SCRIPT_NAME]: Applying \"$JOB\" on \"$CONFIG\"" >&2
		# logLine "#"
		# resetFormat
		logTitle "[$SCRIPT_NAME]: Applying \"$JOB\" on \"$CONFIG\""
		
		if [[ $VERBOSE = y ]] ; then
			echo "CONFIG_FILE = \"$CONFIG_FILE\"" >&2
			echo "JOB_FILE = \"$JOB_FILE\"" >&2
			logSeparator
		fi

		if [[ $INTERACTIVE = y ]] ; then
			validkey=n
			cancel=n
			while [[ $validkey = n ]] ; do
				echo "Press [\"e\"] to EXECUTE or [\"c\"] to CANCEL ..." >&2
				read -sn 1 keypress
				case $keypress in
					c) validkey=y
					SHUTDOWN=n
					cancel=y
					;;
					e) validkey=y
					;;
				esac
			done
			logSeparator
			if [[ $cancel = y ]] ; then
				exit $EXIT_SUCCESS
			fi
		fi

		# ---------------------
		# Load the CONFIG file:
		# ---------------------
		if [ -e "$CONFIG_FILE" ] ; then
			. $CONFIG_FILE
		fi

		# --------------------------------------------------------
		# Invoke the JOB, it must show usage and EXIT when HELP=y:
		# --------------------------------------------------------
		. $JOB_FILE
	fi

	# --------------------------------------------------------
	# Use all "*.config" CONFIG files discovered (batch mode):
	# --------------------------------------------------------
	if [[ $BATCH = y ]] ; then

		START_TIME=$(date +"%Y-%m-%d %T")
		START_TIMESTAMP=$(date +%s)
	
		CURRENT_DIR=$(pwd)
		if [ ! -z "$CONFIG" ] ; then
			
			CONFIG=${CONFIG%/}
			
			if [ ! -d "$CONFIG" ] ; then
				showUsage "The CONFIG_DIR \"$CONFIG\" does not exist!"
				exit $EXIT_ERROR
			fi
			cd "$CONFIG"
		else
			cd "$SCRIPT_DIR"
		fi
		AVAILABLE_CONFIG=$(ls -1 *.config)
		cd "$CURRENT_DIR"

		AVAILABLE_CONFIG=${AVAILABLE_CONFIG//.config/}

		# echo "" >&2
		# echo "################################################################################" >&2
		# echo "# [$SCRIPT_NAME]: Applying \"$JOB\" as batch on all \"*.config\" files:" >&2
		# echo "################################################################################" >&2
		logTitle "[$SCRIPT_NAME]: Applying \"$JOB\" as batch on all \"*.config\" files:"

		NUM_CONFIG=0
		NUM_OFF=0
		NUM_SUCCESS=0
		NUM_SKIPPED=0

		if [ -z "$AVAILABLE_CONFIG" ] ; then
			if [[ $VERBOSE = y ]] ; then
				# echo ""  >&2
				# echo "--------------------------------------------------------------------------------" >&2
				# echo "(no \"*.config\" configurations discovered, nothing to do)" >&2
				# echo "--------------------------------------------------------------------------------" >&2
				logHeadline "(no \"*.config\" configurations discovered, nothing to do)"
			fi
		fi

		if [ ! -z "$AVAILABLE_CONFIG" ] ; then
			
			for FILENAME in $AVAILABLE_CONFIG ; do
			
				if [ ! -z $CONFIG ] ; then
					FILEPATH="$CONFIG/$FILENAME"
				else
					FILEPATH=$FILENAME
				fi
			
				EXIT_CODE=0
				if [[ $VERBOSE = y ]] || [[ $INTERACTIVE = y ]] ; then
					# echo ""  >&2
					# echo "--------------------------------------------------------------------------------" >&2
					# echo "Applying \"$JOB\" on \"$FILENAME\" ..." >&2
					# echo "--------------------------------------------------------------------------------" >&2
					logHeadline "Applying \"$JOB\" on \"$FILENAME\" ..."
				fi

				skip=n
				if [[ $INTERACTIVE = y ]] ; then
					validkey=n
					cancel=n
					while [[ $validkey = n ]] ; do
						echo "Press [\"e\"] to EXECUTE NEXT, [\"a\"] to EXECUTE ALL (non INTERACTIVE), [\"s\"] to SKIP NEXT or [\"c\"] to CANCEL ALL ..." >&2
						read -sn 1 keypress
						case $keypress in
							c) validkey=y
							SHUTDOWN=n
							cancel=y
							;;
							a) validkey=y
							INTERACTIVE=n
							;;
							e) validkey=y
							;;
							s) validkey=y
							skip=y
							NUM_SKIPPED=`expr $NUM_SKIPPED + 1`
						esac
					done
					if [[ $cancel = y ]] ; then
						break
					fi
				fi

				if [[ $skip = n ]] ; then
					if [[ $VERBOSE = y ]] ; then
						$SCRIPT_DIR/apply.sh -vj $JOB $FILEPATH
						EXIT_CODE=$?
					fi

					if [[ $VERBOSE = n ]] ; then
						$SCRIPT_DIR/apply.sh -j $JOB $FILEPATH
						EXIT_CODE=$?
					fi

					if [[ $EXIT_CODE = $EXIT_OFF ]] ; then
						NUM_OFF=`expr $NUM_OFF + 1`
					fi
					if [[ $EXIT_CODE = 0 ]] ; then
						NUM_SUCCESS=`expr $NUM_SUCCESS + 1`
					fi
				fi
				NUM_CONFIG=`expr $NUM_CONFIG + 1`
			done
		fi

		NUM_FAILURE=`expr $NUM_CONFIG - $NUM_SUCCESS`
		NUM_FAILURE=`expr $NUM_FAILURE - $NUM_OFF`
		NUM_FAILURE=`expr $NUM_FAILURE - $NUM_SKIPPED`

		END_TIME=$(date +"%Y-%m-%d %T")
		END_TIMESTAMP=$(date +%s)
		TOTAL_TIME=$(( $END_TIMESTAMP - $START_TIMESTAMP ))

		# echo ""  >&2
		# echo "--------------------------------------------------------------------------------"  >&2
		# echo "Applied \"$JOB\" on [$NUM_CONFIG] \"*.config\" files:" >&2
		# echo "--------------------------------------------------------------------------------"  >&2
		logHeadline "Applied \"$JOB\" on [$NUM_CONFIG] \"*.config\" files:"

		echo "  End time  : $END_TIME"  >&2
		echo "- Start time: $START_TIME"  >&2
		logSeparator
		echo "= Total time: $TOTAL_TIME seconds"  >&2
		logSeparator
		echo "  Disabled  : $NUM_OFF" >&2
		echo "+ Skipped   : $NUM_SKIPPED" >&2
		echo "+ Failure   : $NUM_FAILURE" >&2
		echo "+ Successful: $NUM_SUCCESS" >&2
		logSeparator
		echo "= Total     : $NUM_CONFIG" >&2
		logSeparator
	fi
fi

# ---------
# SHUTDOWN:
# ---------
if [[ $SHUTDOWN = y ]] ; then		
	shutdown now
fi

exit $EXIT_SUCCESS




© 2015 - 2024 Weber Informatics LLC | Privacy Policy