

# is_yes() and is_no() taken from PLD rc-scripts
is_yes()
{
	# Test syntax	
	if [ $# = 0 ] ; then
		echo "Usage: is_yes {value}"
		return 2
	fi

	# Check value
	case "$1" in
		yes|Yes|YES|true|True|TRUE|on|On|ON|Y|y|1)
			# true returns zero
			return 0
		;;
		*)
			# false returns one
			return 1
		;;
	esac
}

is_no()
{
	# Test syntax
	if [ $# = 0 ] ; then
		echo "Usage: is_no {value}"
		return 2
	fi

	# It's important that is_no() should always return logical
	# negation of is_yes(). Really ALWAYS. --misiek
	# NOTE: This means that passing value which mean neither "yes" nor
	# "no" will return "true", beacause it does not mean "yes". Weird,
	# isn't it? :) --mkochano
	if is_yes "$1" ; then
		return 1
	else
		return 0
	fi
}

# 200 OK
# 400 Bad request
# 403 Not permitted
# 500 Internal error

source_config () {
	
	BACKUPUSER="`id -un`"
	cd ${clientconfdir}/backups || { echo "500 Internal client error" ; exit ; }
	. ${clientconfdir}/backups/$BACKUP.conf 
	cd /
	if is_no $ENABLED ; then
		echo "403 configuration disabled"
		exit 2
	fi
	if [ "$NAME" != "$BACKUP" ] ; then
		echo "500 configuration name mismatch"
		exit 2
	fi
	if [ -z "$TOOL" -a -z "$COMMAND" ] ; then
		echo "500 no tool or command specified"
		exit 2
	fi
}

configure_tool () {

	if [ -n "$TOOL" ] ; then
		if [ -f ${clientdatadir}/$TOOL.tool ] ; then
			. ${clientdatadir}/$TOOL.tool 
		else
			echo "500 Tool not known" 
			exit 2
		fi
	else
		if [ -f ${clientdatadir}/command.sh ] ; then
			. ${clientdatadir}/command.sh 
		else 
			echo "500 Internal client error" 
			exit 2
		fi
	fi
	
}

do_backup () {


	# $1 	master or slave
	# $2	backup or test

	source_config

	configure_tool

	tool_check

	if [ "$BACKUPUSER" != "`id -un`" ] ; then
		if [ "$1" == "slave" ] ; then
			echo "500 Internal client error"
			exit 2
		elif ! ${SUDO} -u "$BACKUPUSER" ${clientdatadir}/backupc-slave "$2" "$BACKUPID" "$BACKUP" "$BACKUPLEVEL" ; then
			if [ "$?" -lt 2 ] ; then
				echo "500 Internal client error"
				exit 2
			fi
		fi 
		exit 0
	fi
	
	echo "200 Seems OK"
	echo "$DESCRIPTION"
	
	if tool_run $2 ; then
		echo "200 OK" > ${clientvardir}/$BACKUPID-$BACKUP.status
		sleep 2
	else
		exit 2
	fi

}

# vi: syntax=sh
