#!/bin/bash

function usage() {
	echo "
Usage:

    ${prog_name} [start | stop | restart | heal | status | init | dest | reinit]


options:

    start   - start slony replication.

    stop    - stop slony replication.

    restart - restart slony replication.

    heal    - maintain slony replication:
              * start slony replication, if slony replication is stoped
              * try to restart slony replication, if slony replication has error
              * send email alarm, if slony replication has error after trying to restart

    status  - report status of slony replication.

    init    - Initiate schema and triggers on database for slony.
              Before the first replication, the option must be performed once.
              After performing the option successfully, must fully replicate provider's database to receiver.
              The option will stop slony replication if replication has be started.

    dest    - Destroy schema and triggers initiated by performing the option \"init\".
              The option will stop slony replication if replication has be started.

    reinit  - Re-initiate schema and triggers on database for slony.
              In fact, it will perform the option \"dest\" first, then perform the option \"init\".
              After performing the option successfully, must fully replicate provider's database to receiver.
              The option will stop slony replication if replication has be started.

	"
}

function common() {
	PATH="${HOME}/bin:${PATH}"
	. "${HOME}/bin/tm_slony_setting.sh"
	prog_name=$(basename $0)
	prog_args="${@}"
	logs_stamp=`[ "${2}" == "logStamp" ] && echo 1 || echo 0`
	log_file="${LOG_FILE}"
	has_started_msg="slony is started"
	has_stoped_msg="slony is stoped"
	has_error_msg="slony has error"
}

function status() {
	master_pid_exists=`[ -f "${PID_FILE}" ] && echo 1 || echo 0`
	slave_pid_exists=`ssh "${SLAVEHOST}" "[ -f "${PID_FILE}" ] && echo 1 || echo 0 2>&1"`
	master_process=`ps -A | grep -v "${prog_name}" | grep slon`
	slave_process=`ssh "${SLAVEHOST}" "ps -A 2>&1" | grep -v "${prog_name}" | grep slon`
	#echo "pid_file=${PID_FILE}"
	#echo "master_pid_exists=${master_pid_exists}"
	#echo "slave_pid_exists=${slave_pid_exists}"
	#echo "master_process=${master_process}"
	#echo "slave_process=${slave_process}"

	if [ "${master_pid_exists}" == "1" ] && [ "${slave_pid_exists}" == "1"  ] && [ "${master_process}" != ""  ] && [ "${slave_process}" != ""  ]; then
		echo "${has_started_msg}"
		return 0
	elif [ "${master_pid_exists}" == "0" ] && [ "${slave_pid_exists}" == "0"  ] && [ "${master_process}" == ""  ] && [ "${slave_process}" == ""  ]; then
		echo "${has_stoped_msg}"
		return 0
	fi
	echo "${has_error_msg}"	
	return 1
}



function start() {
	ret=`status`
	if [ "${ret}" == "${has_started_msg}" ]; then
		echo "${has_started_msg} already"
		return 0
	fi

	echo "start slony replication..."
	tm_slony_master_start >> "${log_file}" 2>&1 &
	ssh -f "${SLAVEHOST}" "bash -l tm_slony_slave_start >> \"${log_file}\" 2>&1"
	tm_slony_replication_start >> "${log_file}" 2>&1

	ret=`status`
	if [ "${ret}" == "${has_started_msg}" ]; then
		echo "${has_started_msg}"
		return 0
	fi
	echo "${has_error_msg}"
	return 1
}

function stop() {
	ret=`status`
	if [ "${ret}" == "${has_stoped_msg}" ]; then
		echo "${has_stoped_msg} already"
		return 0
	fi

	echo "stop slony replication..."
	killall -g slon >> "${log_file}" 2>&1
	ssh "${SLAVEHOST}" "killall -g slon >> \"${log_file}\" 2>&1"

	ret=`status`
	if [ "${ret}" == "${has_stoped_msg}" ]; then
		echo "${has_stoped_msg}"
		return 0
	fi
	echo "${has_error_msg}"
	return 1
}

function restart() {
	stop
	start
}

function heal() {
	ret=`status`
	if [ "${ret}" == "${has_started_msg}" ]; then
		echo "slony is alive now"
		return 0
	elif [ "${ret}" == "${has_stoped_msg}" ]; then
		start
	elif [ "${ret}" == "${has_error_msg}" ]; then
		restart
		ret=`status`
		if [ "${ret}" == "${has_error_msg}" ]; then
			crm_alarm -n 5 "${has_error_msg}"
			return 1
		fi
	fi
}

function init() {
	ret=`status`
	if [ "${ret}" == "${has_started_msg}" ]; then
		stop
	fi

	echo "initiate schema and triggers for slony on database..."
	tm_slony_set_nodes "${MASTERDBNAME}" >> "${log_file}" 2>&1
	if [ "${?}" == "0" ]; then
		echo "initiating successfully"
		return 0
	fi
	echo "${has_error_msg}"
	return 1
}

function dest() {
	ret=`status`
	if [ "${ret}" == "${has_started_msg}" ]; then
		stop
	fi

	echo "destroy slony schema and triggers on database..."

	tm_slony_set_nodes -r "${MASTERDBNAME}" >> "${log_file}" 2>&1
	if [ "${?}" != "0" ]; then
		echo "${has_error_msg}"
		return 1
	fi
	psql -c "DROP SCHEMA _${CLUSTERNAME} CASCADE;" "${MASTERDBNAME}" >> "${log_file}" 2>&1
	ssh "${SLAVEHOST}" "tm_slony_set_nodes -r \"${SLAVEDBNAME}\" >> \"${log_file}\" 2>&1"
	ssh "${SLAVEHOST}" "psql -c \"DROP SCHEMA _${CLUSTERNAME} CASCADE;\" \"${SLAVEDBNAME}\" >> \"${log_file}\" 2>&1"
	
	echo "destroying successfully"
        return 0
}

function reinit() {
	dest
	init
}

function echoBegin() {
	if [ "${logs_stamp}" == "1" ]; then
		time_stamp=$(date "+%Y-%m-%d %H:%M:%S")
		echo "[${$}][${time_stamp}] begin: ${prog_name} ${prog_args}"
	fi
}

function echoFinish() {
	if [ "${logs_stamp}" == "1" ]; then
	        time_stamp=$(date "+%Y-%m-%d %H:%M:%S")
		echo "[${$}][${time_stamp}] finish: ${prog_name} ${prog_args}"
	fi
}


common ${@}

case $1 in
	"status")       echoBegin;status;echoFinish;;
	"start")	echoBegin;start;echoFinish;;  
	"stop")		echoBegin;stop;echoFinish;;
	"restart")	echoBegin;restart;echoFinish;;
	"heal")		echoBegin;heal;echoFinish;;
        "init")         echoBegin;init;echoFinish;;
        "dest")         echoBegin;dest;echoFinish;;
        "reinit")       echoBegin;reinit;echoFinish;;
        *)		usage;;
esac  
