#! /bin/bash
#########################################################################
#									#
#	chkbckshare is automatically generated,				#
#		please do not modify!					#
#									#
#########################################################################

#########################################################################
#									#
# Script ID: chkbckshare						#
# Author: Copyright (C) 2013-2019, 2021  Mark Grant			#
#									#
# Released under the GPLv3 only.					#
# SPDX-License-Identifier: GPL-3.0-only					#
#									#
# Purpose:								#
# To check if the backup share is mounted . E.g.			#
# 	\\Ambrosia\charybdisbck or hermes:/srv/backups/charybdis	#
# 	on								#
# 	/mnt/charybdisbck						#
#									#
# Syntax:	chkbckshare [-b or --boolean || -h or --help ||		#
#				-t or --terse || -V or --version]	#
#									#
# Exit codes used:-							#
# Bash standard Exit Codes:	0 - success				#
#				1 - general failure			#
# User-defined exit code range is 64 - 113				#
#	C/C++ Semi-standard exit codes from sysexits.h range is 64 - 78	#
#		EX_USAGE	64	command line usage error	#
#		EX_DATAERR	65	data format error		#
#		EX_NOINPUT	66	cannot open input		#
#		EX_NOUSER	67	addressee unknown		#
#		EX_NOHOST	68	host name unknown		#
#		EX_UNAVAILABLE	69	service unavailable		#
#		EX_SOFTWARE	70	internal software error		#
#		EX_OSERR	71	system error (e.g., can't fork)	#
#		EX_OSFILE	72	critical OS file missing	#
#		EX_CANTCREAT	73	can't create (user) output file	#
#		EX_IOERR	74	input/output error		#
#		EX_TEMPFAIL	75	temp failure; user is invited	#
#					to retry			#
#		EX_PROTOCOL	76	remote error in protocol	#
#		EX_NOPERM	77	permission denied		#
#		EX_CONFIG	78	configuration error		#
#	User-defined (here) exit codes range 79 - 113:			#
#		None							#
#									#
# Further Info:								#
# This script checks if a share is mounted as a target for the		#
# backup scripts.							#
#									#
#########################################################################

#########################################################################
#									#
# Changelog								#
#									#
# Date		Author	Version	Description				#
#									#
# 21/12/2013	MG	1.1.1	First release.				#
# 22/11/2014	MG	1.1.2	Add overall package version to -V.	#
# 22/11/2014	MG	1.1.3	Switched from getopts to GNU getopt to	#
#				allow long options.			#
# 02/02/2015	MG	1.1.4	Remove BSD support.			#
# 02/02/2015	MG	1.2.1	Add support for NFS shares. Add support	#
#				for different share targets for weekly	#
#				and adhoc backups.			#
# 24/02/2015	MG	1.2.2	Change configuration files directory to	#
#				lixbackups to limit risk of conflict	#
#				with other backup packages.		#
# 20/06/2017	MG	1.2.3	Ensure script respects the 80 column	#
#				rule.					#
# 30/11/2017	MG	1.2.4	Add SPDX license tags to source files.	#
# 03/12/2017	MG	1.2.5	Adopt normal exit code policy; 0 on	#
#				success, 1 on failure.			#
# 09/01/2019	MG	1.2.6	Use absolute bash path in shebang	#
#				instead of env. Using env is OK for a 	#
#				non-AutoTools script.			#
# 13/02/2019	MG	1.3.1	Do not hard code outputprefix contents.	#
#				Use true booleans.			#
#				Standardise output function.		#
#				Remove script_exit_code variable.	#
#				Improve trap function.			#
#				Use [[ ... ]] when appropriate.		#
#				Refactor into functions.		#
#				Make appropriate variables readonly.	#
#				Use meaningful exit codes.		#
#				Use standardised printf aot echo.	#
#				Make boolean output 'TRUE' not 'TRUE '	#
#				Improve output when mount detected.	#
#				Ensure numerics use (( )) not [[ ]]	#
#				Specify type for numeric variables.	#
# 19/04/2019	MG	1.3.2	Replace obsolete backticks with $(cmd).	#
# 09/08/2019	MG	1.3.3	Correct getopt CL for proper quoting.	#
# 11/10/2019	MG	1.3.4	Move script_exit() before it is used.	#
# 24/11/2021	MG	1.3.5	Tighten SPDX tag.			#
#									#
#########################################################################

##################
# Init variables #
##################
outputprefix="$(basename $0): "			# Can be altered by --terse CLA
readonly version=1.3.5				# set version variable
readonly packageversion=1.3.8		# Version of the package
readonly etclocation=/etc/lixbackups	# Path to etc directory

bool=false
terse=false
mntdir=""
mountoutput=""

#############
# Functions #
#############

# -h --help output.
# No parameters
# No return value
usage()
{
cat << EOF
Usage is $0 [options]
	-b or --boolean displays TRUE or FALSE instead of mount information
	OR
	-h or --help displays usage information
	OR
	-t or --terse removes prepending script name from the output
	OR
	-V or --version displays version information
EOF
}

# Standard function to emit messages depending on various parameters.
# Parameters -	$1 What:-	The message to emit.
#		$2 Where:-	stdout == 0
#				stderr != 0
# No return value.
output()
{
	if (( !$2 )); then
		printf "%s\n" "$outputprefix$1"
	else
		printf "%s\n" "$outputprefix$1" 1>&2
	fi
}

# Standard function to tidy up and return exit code.
# Parameters - 	$1 is the exit code.
# No return value.
script_exit()
{
	exit $1
}

# Standard function to test command error and exit if non-zero.
# Parameters - $1 is $?
# No return value.
std_cmd_err_handler()
{
	if (( $1 )); then
		script_exit $1
	fi
}

# Standard trap exit function.
# No parameters.
# No return value.
trap_exit()
{
	local -i exit_code=$?
	local msg

	msg="Script terminating with exit code $exit_code due to trap received."
	output "$msg" 1 0
	script_exit $exit_code
}

# Process command line arguments with GNU getopt.
# Parameters -	$1 is the command line.
# No return value.
proc_CL()
{
	local GETOPTTEMP
	local tmpGETOPTTEMP

	tmpGETOPTTEMP="getopt -o bhtV --long boolean,help,terse,version"
	GETOPTTEMP=$($tmpGETOPTTEMP -n "$0" -- "$@")
	std_cmd_err_handler $?

	eval set -- "$GETOPTTEMP"
	std_cmd_err_handler $?

	while true; do
		case "$1" in
		-b|--boolean)
			bool=true
			shift
			;;
		-h|--help)
			usage
			shift
			script_exit 0
			;;
		-t|--terse)
			terse=true
			outputprefix=""
			shift
			;;
		-V|--version)
			printf "%s Script version %s\n" $0 $version
			printf "%s Package version %s\n" $0 $packageversion
			shift
			script_exit 0
			;;
		--)	shift
			break
			;;
		*)	output "Internal error." 1
			script_exit 64
			;;
		esac
	done

	# Script does not accept other arguments.
	if (( $# > 0 )); then
		output "Invalid argument." 1
		script_exit 64
	fi
}

# Process the config file just looking for mntdir.
# Parameters - None
# No return value.
proc_config_file()
{
	local input=()
	local msg
	local oldIFS=$IFS

	if [[ ! -f $etclocation/backups.conf \
		|| ! -r $etclocation/backups.conf ]]; then
		msg="Config file $etclocation/backups.conf does not exist, is "
		msg+="not a regular file or is not readable."
		output "$msg" 1
		script_exit 66
	fi

	IFS="="

	exec 3<$etclocation/backups.conf
	while read -u3 -ra input; do
		case ${input[0]} in
		mntdir)
			mntdir=${input[1]}
			;;
		esac
	done
	exec 3<&-

	IFS=$oldIFS
}


########
# Main #
########

# Setup trap.
trap trap_exit SIGHUP SIGINT SIGQUIT SIGTERM

proc_CL "$@"

proc_config_file

# Check to see if the backup share is mounted
mountoutput=$(mount | grep "$mntdir")
if $bool ; then
	if [[ $mountoutput ]]; then
		output "TRUE" 0
	else
		output "FALSE" 0
	fi
else
	if [[ $mountoutput ]]; then
		output "Mount is ${mountoutput% type*}" 0
	else
		output "Backup share not mounted" 0
	fi
fi

script_exit 0

