#!/bin/sh

XSL_FILE="/usr/share/pacemaker/cluconf2cib.xsl"
SAXON_JAR="/usr/share/java/saxon.jar"

# Above is autogenerated; DEBUG here is run-time
DEBUG=0

export XSL_FILE SAXON_JAR DEBUG

die()
{
	echo "$*"
	exit 1
}

_validate()
{
	if [ $DEBUG -eq 1 ]; then
		echo "debug: adding . to $PATH"
		export PATH="$PATH:."
	fi

	which ccs_flatten &> /dev/null	|| die "Can't find ccs_flatten in path!"
	which java &> /dev/null		|| die "Can not find java in path!"

	if [ -z "$XSL_FILE" ]; then
		if [ $DEBUG -eq 1 ]; then
			XSL_FILE=./cluconf2cib.xsl
			echo "debug: using $XSL_FILE"
		else
			die "Please specify path to XSLT script using -X <path>."
		fi
	fi

	if [ -z "$SAXON_JAR" ]; then
		if [ $DEBUG -eq 1 ]; then
			SAXON_JAR=/usr/share/java/saxon.jar
			echo "debug: using $SAXON_JAR"
		else
			die "Please specify path to saxon.jar using -J <path>."
		fi
	fi

	[ -d /usr/share/cluster ] || die "/usr/share/cluster does not exist."
	[ -f /usr/share/cluster/service.sh ] || die "Missing rgmanager resource agents?"

	[ -f "$XSL_FILE" ] || die "$XSL_FILE does not exist!"
	[ -f "$SAXON_JAR" ] || die "$SAXON_JAR does not exist!"

	[ -f "$1" ] || die "Input file $1 not found"
	if [ -f "$2" ]; then
		[ $3 -ne 0 ] || die "Output file $2 exists; please remove or use -f"
	fi

	return 0
}


help()
{
cat <<EOT
usage: $(basename $1) [options]
	-i input_file	Configuration input [/etc/cluster/cluster.conf]
	-o output_file	File to output [cib.xml]
	-f		Force update (remove output_file if it exists)
	-R		Disable rgmanager in input file after completion
			  WARNING: Edits config file in-place
	-r output_conf	Disable rgmanager, store result in output_conf
			  instead of modifying input_file in place.
	-n		Don't call crm_verify on the new cib.xml
	-d		Enable development/debug mode
	-X <path>	Specify path to XSLT script
	-J <path>	Specify path to Saxon jar file
	-h		This message
EOT
}


# main
declare conf_in cib_out xsl_file saxon_jar conf_out opt do_update tmp
declare force_update no_verify

# defaults
conf_in="/etc/cluster/cluster.conf"
cib_out="cib-converted.xml"
conf_out=""
do_update=0
no_verify=0
force_update=0
tmp=$(mktemp /tmp/ccs2cib.tmp.XXXXXX)

while getopts i:o:X:J:Rr:dnhf opt; do
	case $opt in
	d)
		DEBUG=1
		;;
	i)
		conf_in="$OPTARG"
		;;
	o)
		cib_out="$OPTARG"
		;;
	R)
		do_update=1
		;;
	r)
		do_update=1
		conf_out="$OPTARG"
		;;
	n)
		no_verify=1
		;;
	f)
		force_update=1
		;;
	X)
		XSL_FILE="$OPTARG"
		;;
	J)
		SAXON_JAR="$OPTARG"
		;;
	h)
		help $0
		exit 0
		;;
	*)
		echo "Error parsing $opt"
		help $0
		exit 1
		;;
	esac
done

[ -z "$conf_out" ] && conf_out="$conf_in"

_validate "$conf_in" "$cib_out" $force_update

echo " * Converting configuration"
if ! ccs_flatten "$conf_in" > $tmp; then
	rm -f $tmp
	die "Flattening of configuration file failed."
fi

if ! java -jar $SAXON_JAR -xsl:$XSL_FILE $tmp > $cib_out; then
	rm -f $tmp
	die "Conversion failed."
fi

echo " * Calling crm_verify to validate the configuration."

if [ $no_verify -eq 0 ]; then
	crm_verify --xml-file $cib_out -V || die "Validation failed."
fi

if [ $do_update -ne 0 ]; then
	echo " * Disabling rgmanager in $conf_out"
	rm -f $tmp
	disable_rgmanager "$conf_in" > "$tmp" || die "Failed to disable rgmanager"
	mv "$tmp" "$conf_out"
	if [ "$conf_out" = "/etc/cluster/cluster.conf" ]; then
		if clustat -Q &> /dev/null; then
			echo " * Calling cman_tool to update cluster.conf"
			cman_tool version -r
		else
			echo "   * You will need to manually copy $conf_out to the other cluster"
			echo "     nodes using scp, ccs_sync, or some other utility."
		fi
	fi
fi

echo " * Proposed cib stored in $cib_out"

exit 0
