#!/bin/bash
#
# Shell script: caffeinate-script.sh
# Author: Solouki Barbican
# Purpose: To stop sleep/hibernate mode while another script is executing.
# Usage: . caffeinate-script.sh <filename-of-script-to-caffeinate.sh>
# Creation Date: 2001.05.12
# Modification Date: 2005.09.01
# Modification Date: 2012.12.03 (major revision, converted to caffeinate utility)
#
#
#################################################################################
# The folloing describes an example usage:
#
# This script may be used to build all of M4M including up-to-date
# Tables of Contents, Table of Figures, Table of Tables, Indices,
# and Bibliographies. Since this build takes roughly 2-3 hours
# (depending on the speed of the CPU), the Mac computer is
# `caffeinated' so that it doesn't go into Sleep Mode in the middle
# of a build.
#
# This bash script should be executed by the following commands (for example)
# to build all of M4M:
# . caffeinate_script.sh build_Scripts_M4M_all_copy_aux.sh
# or, for example, to build just a single Thinking Problem:
# . caffeinate_script.sh M4M-CM-Schulers-Pendulum-Thinking-Problem.sh
#
# If the script returns to Terminal input, and if %1 job is stopped, then
# enter "%1" to get the job running again and hit "RETURN" to restart
# the caffeinate script.
#
# You should probably set GPG_wait_for_input="YES" in your input script so that
# the build will wait for you to return to enter the GPG passphrase instead of
# just timing out. After stopping the first time, you can enter "NO" in order
# that the build-box.sh script does not stop for future GPG passphrases (I have
# set my GPG Suite remember passphrase time to 1200 or 3600 seconds instead of
# the default 600 seconds).
#
# When the script finishes (returns to Terminal prompt), the M4M build is complete.
#################################################################################
# ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
# Arguments to the caffeinate-script.sh script:
CAFF_arguments="$@"
CAFF_arguments_upper=`echo $CAFF_arguments | tr '[:lower:]' '[:upper:]'`
##CAFF_argument_1=`echo $CAFF_arguments | awk '{print $1}' | tr '[:lower:]' '[:upper:]'`
CAFF_argument_1=`echo $CAFF_arguments | awk '{print $1}'`
##CAFF_argument_2=`echo $CAFF_arguments | awk '{print $2}' | tr '[:lower:]' '[:upper:]'`
CAFF_argument_2=`echo $CAFF_arguments | awk '{print $2}'`
CAFF_argument_3=`echo $CAFF_arguments | awk '{print $3}' | tr '[:lower:]' '[:upper:]'`
CAFF_argument_4=`echo $CAFF_arguments | awk '{print $4}' | tr '[:lower:]' '[:upper:]'`
CAFF_argument_5=`echo $CAFF_arguments | awk '{print $5}' | tr '[:lower:]' '[:upper:]'`
CAFF_script=${CAFF_argument_1}
#CAFF_test="YES"
CAFF_test="No"
if [ ! \( "$CAFF_test" = "No" \) ]; then
echo " Number of args = $#"
echo " CAFF_arguments = $CAFF_arguments"
echo " CAFF_arguments_upper = $CAFF_arguments_upper"
echo " CAFF_argument_1 = $CAFF_argument_1"
echo " CAFF_argument_2 = $CAFF_argument_2"
echo " CAFF_argument_3 = $CAFF_argument_3"
echo " CAFF_argument_4 = $CAFF_argument_4"
echo " CAFF_argument_5 = $CAFF_argument_5"
echo " CAFF_script = $CAFF_script"
if [ \( "$CAFF_test" = "YES" \) ]; then
# If CAFF_test=YES, then:
echo " After reading arguments to caffeinate-scrupt.sh: "
read -p " [Hit <return> to continue, NO to end stopping, CONT to continue stopping, EXIT to exit]: " INPUTTEMP
INPUTTEMP=`echo $INPUTTEMP | tr '[:lower:]' '[:upper:]'`
if [ "$INPUTTEMP" = "NO" ]; then
CAFF_test="No"
fi # if [ "$INPUTTEMP" = "NO" ]; then
if [ "$INPUTTEMP" = "CONT" ]; then
CAFF_test="YES"
fi # fi of if [ "$INPUTTEMP" = "CONT" ]; then
if [ "$INPUTTEMP" = "EXIT" ]; then
### cd ${BashDirectory_caffeinate}
return
exit
fi # if [ "$INPUTTEMP" = "EXIT" ]; then
fi # if [ \( "$CAFF_test" = "YES" \) ]; then
fi # fi of if [ ! \( "$CAFF_test" = "No" \) ]; then
# ----------------------------------------------------------------------
# ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
# List the shell variables and global environment variables:
#( set -o posix ; set ) | grep -i gpg # for bash
#( set -o posix ; set ) | grep -i bash # for bash
###( setopt posixbuiltin; set; ) | grep -i gpg # for zsh
###( setopt posixbuiltin; set; ) | grep -i bash # for zsh
unset BashDirectory_caffeinate
unset BashScript_caffeinate
unset BashTarget_caffeinate
unset CurrentDirectory_caffeinate
# ----------------------------------------------------------------------
# ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
# Determine which caffeinate-script.sh is being executed:
CurrentDirectory_caffeinate="$( cd -P "$( dirname "." )" && pwd )"
BashScript_caffeinate="${BASH_SOURCE[0]}"
while [ -h "$BashScript_caffeinate" ]; do # Resolve $BashScript until it is no longer a symlink
BashTarget_caffeinate="$(readlink "$BashScript_caffeinate")"
if [[ $BashScript_caffeinate == /* ]]; then
echo " Bash Script: '$BashScript_caffeinate' is an Absolute Symlink to: '$BashTarget_caffeinate'"
BashScript_caffeinate="$BashTarget_caffeinate"
else
BashDirectory_caffeinate="$( dirname "$BashScript_caffeinate" )"
echo " Bash Script: '$BashScript_caffeinate' is a Relative Symlink to: '$BashTarget_caffeinate' (relative to '$BashDirectory_caffeinate')"
BashScript_caffeinate="$BashDirectory_caffeinate/$BashTarget_caffeinate" # If $BashScript was a relative symlink, resolve it relative
# to the path where the symlink file was located
fi
done
RBashDirectory_caffeinate="$( dirname "$BashScript_caffeinate" )"
BashDirectory_caffeinate="$( cd -P "$( dirname "$BashScript_caffeinate" )" && pwd )"
if [ "$BashDirectory_caffeinate" != "$RBashDirectory_caffeinate" ]; then
echo " Directory: '$RBashDirectory_caffeinate' resolves to: '$BashDirectory_caffeinate'"
fi
echo " "
echo " Bash Script's Name: '$BashScript_caffeinate'"
echo " Bash Script's Directory: '$BashDirectory_caffeinate'"
echo " Current Directory: '$CurrentDirectory_caffeinate'"
echo " "
# ----------------------------------------------------------------------
# ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
# Configure the variables for building M4M:
M4M_dir="${HOME}/Math"
##export M4M_dir
#echo $PWD
if [ -f math_build.login ]; then
#echo " Conditional: if [ -f math_build.login ]"
. math_build.login
else
#echo " Conditional: if [ -f math_build.login ] else"
if [ -f $M4M_dir/Scripts/math_build.login ]; then
. $M4M_dir/Scripts/math_build.login
fi # fi of if [ -f $M4M_dir/Scripts/math_build.login ]; then
fi
#echo $PWD
# --------------------------------------------------------------------------------
# ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
# Execute the script:
# There are several alterantive ways to execute the script, pick one
# (uncomment the way particular commands below that you wish to use).
cd $CurrentDirectory_caffeinate
echo " ******************************************************************************** "
echo " PWD = ${PWD}"
echo " Script to caffeinate:"
#echo " ./${CAFF_script} >>/dev/null 2>&1 & "
#./${CAFF_script} >>/dev/null 2>&1 &
echo " ./${CAFF_script} & "
./${CAFF_script} &
#echo " . ${CAFF_script} &"
#. ${CAFF_script} &
#echo " source ${CAFF_script} &"
#source ${CAFF_script} &
echo " ******************************************************************************** "
# --------------------------------------------------------------------------------
# ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
# Caffeinate the script:
sleep 5
# Find the script's process ID:
caffpid=`ps auxww | grep -i ${CAFF_script} | grep -v grep | awk -F ' ' '{print $2}'`
export caffpid
echo " Following is the caffeinated script:"
ps auxww | grep -i ${CAFF_script} | grep -v grep | awk -F ' ' '{print $2}'
echo " PID of the caffeinated script: ${caffpid}"
# Caffeinate the process:
caffeinate -ims -w ${caffpid} &
sleep 5
echo " Following is the caffeinate process:"
ps auxww | grep -i caffeinate | grep -v grep | awk -F ' ' '{print $2}'
# After the caffeinated process complete, the caffeinate halts.
# --------------------------------------------------------------------------------