#!/usr/bin/env bash

#*******************************************************************************
#
# Bare Conductive Pi Cap Button Utility Startup Script
# ----------------------------------------------------
# 
# picap-button-at-startup - ensures that picap-button runs at startup (by adding
#                           it to /etc/rc.local)
#
# Written by Stefan Dzisiewski-Smith.
#
# This work is licensed under a Creative Commons Attribution-ShareAlike 3.0 
# Unported License (CC BY-SA 3.0) http://creativecommons.org/licenses/by-sa/3.0/
#
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
# THE SOFTWARE.
#
#*******************************************************************************

set -e # halt on any error

MODE="ENABLE"

for ARG in "$@"
do
	ARG=`echo $ARG | tr '[:upper:]' '[:lower:]'`
	if [[ $ARG == *"enable"* ]]
	then
		MODE="ENABLE"
	elif [[ $ARG == *"disable"* ]]
	then
		MODE="DISABLE"
	elif [[ $ARG == *"h"* ]]
	then
        echo -e "Sets Pi Cap Button Utility (button-utility) to run at boot or not by adding / removing it from /etc/rc.local\n"
        echo -e "Default behaviour is to enable button-utility (if no options are set)\n"
        echo -e "Usage: picap-button-at-startup [OPTION]\n"
        echo -e "Options:\n"
        echo -e "  --enable     adds button-utility to /etc/rc.local"
        echo -e "  --disable    removes button-utility from /etc/rc.local"
        echo -e "  -h, --help   displays this message"
        exit 0
	fi
done

PICAP_BUTTON_UTILITY_PATH="\/usr\/share\/doc\/picap\/examples\/cpp\/picap-button-utility-cpp\/button-utility"
PICAP_BUTTON_UTILITY_COMMENT="Run Pi Cap button utility"
RC_LOCAL_PATH="/etc/rc.local"

if [[ $MODE == "ENABLE" ]]
then
	# check if we're already set to run picap-button
	PICAP_EXISTS=$(grep "^[^#;]" /etc/rc.local | grep "$PICAP_BUTTON_UTILITY_PATH" | cat)

	if [[ $PICAP_EXISTS != "" ]]
	then
	    echo "The Picap button utility (button-utility) is already set to run at startup."
	    exit 0
	fi


	# if the command already exists (but prepended with "#") in /etc/rc.local, remove the "#"
	sudo sed -i -e "s/#$PICAP_BUTTON_UTILITY_PATH/$PICAP_BUTTON_UTILITY_PATH/g" "$RC_LOCAL_PATH"

	# check to see if the previous command worked...
	PICAP_EXISTS=$(grep "^[^#;]" /etc/rc.local | grep "$PICAP_BUTTON_UTILITY_PATH" | cat)

	if [[ $PICAP_EXISTS == "" ]]
	then
	    # if not, add a line to run it before exit 0 (or at the end of the file)
		sudo sed -r -i 's/^\s*exit\s{1,}[0-9]/# '"$PICAP_BUTTON_UTILITY_COMMENT"'\n'"$PICAP_BUTTON_UTILITY_PATH"'\n\n&/' "$RC_LOCAL_PATH"
	fi


	read -r -p "The Picap button utility (picap-button) will now run at startup. Do you want to restart now? [y/N] "
else
	# check if we're already set to run picap-button
	PICAP_EXISTS=$(grep "^[^#;]" /etc/rc.local | grep "$PICAP_BUTTON_UTILITY_PATH" | cat)

	if [[ $PICAP_EXISTS == "" ]]
	then
	    echo "The Picap button utility (button-utility) is already set to NOT run at startup."
	    exit 0
	fi


	# if the command exists in /etc/rc.local, add a "#" to comment it out
	sudo sed -i -e "s/$PICAP_BUTTON_UTILITY_PATH/#$PICAP_BUTTON_UTILITY_PATH/g" "$RC_LOCAL_PATH"

	read -r -p "The Picap button utility (picap-button) will no longer run at startup. Do you want to restart now? [y/N] "
fi

case $REPLY in
    [yY][eE][sS]|[yY]) 
        sudo reboot now
        ;;
    *)
        WHAT_DAY_IS_IT=$(date '+%A')
        echo "You will need to restart your Raspberry Pi before changes take effect. Have a nice $WHAT_DAY_IS_IT."
        ;;
esac

exit 0
