#!/bin/ksh
###
# Summary: 	Generate a SQL Replication repair script and execute it at the APPLY server.
#			It's assumed that only minor repairs are required, otherwise you may as well cold start or redirect-restore from Capture to Apply.
# Prereq : 	Uses the function library in $FPATH, available from http://www.sustainablesoftware.net/downloads/DB2FUNCTIONS.tar.gz
#		   	The install instructions detail some minor but necessary additions to the instance userprofile.
# Calls	 :  Calls SQL query generate_asntdiff_commands.sql, which you should create and place in the same directory as this script. 
#			The SQL query is simply:
#			select 	'asntdiff db=',current server, 'SCHEMA=asn',
#					' WHERE=', chr(34)||'TARGET_OWNER=',
#					''''||rtrim(target_owner)||'''',
#					' and TARGET_TABLE=',
#					''''||rtrim(target_table)||'''',
#					chr(34)
#			from 	asn.ibmsnap_subs_membr 
#			order by target_owner, target_table
#			;
#			You may wish to edit the SQL query to limit the number of tables/subscription sets polled. 
# Usage  : 	Run as workdir/asntrep.cmd <targetdbalias> | tee workdir/asntrep.out
# Returns: 	0 - no errors
#		   	1 - nothing to repair
#		   	2+  any other error.	
# Alex Levy t/a http:www.sustainablesoftware.net, June 2013.
###

set -u			# assert all variables are defined. 
typeset -i rc=0 # o/s return code.
typeset -u DB
typeset workdir=$(dirname $0)

print F_SETUP_DB2_ENVIRONMENT $INSTHOME $USER $DBALIAS +x
F_SETUP_DB2_ENVIRONMENT $INSTHOME $USER $DBALIAS +x
rc=$? ; (( rc > 0 )) && exit $rc

if   (( SQL_REPLICATION_TARGET == 1 ))
then DB=$DBALIAS 
else print "$0 : The userprofile does not define the database for this instance as a SQL Replication target. Exiting."
	 exit 2
fi

print F_CONNECT $DBALIAS +x
F_CONNECT $DBALIAS +x
rc=$? ; (( rc > 0 )) && exit $rc

# Generate the asntdiff commands, one per subscribed table, to determine differences between CAPTURE and APPLY servers.
print F_RUN_DB2FILE ${workdir}/generate_asntdiff_commands.sql "-xtf" 1  ${workdir}/generate_asntdiff_commands.out nocompress +x
F_RUN_DB2FILE ${workdir}/generate_asntdiff_commands.sql "-xtf" 1  ${workdir}/generate_asntdiff_commands.out nocompress +x
rc=$?  
case $rc in 
0) 	# strip headers and blank lines from output
	egrep "asntdiff db=" ${workdir}/generate_asntdiff_commands.out > /tmp/generate_asntdiff_commands.out 
	mv /tmp/generate_asntdiff_commands.out ${workdir}/generate_asntdiff_commands.out
	;;
1) 	print "$0 : No subscriptions are defined in asn.IBMSNAP_SUBS_MEMBR; there is nothing to repair."
	exit 1 ;;
*)  exit $rc ;;
esac

# Main loop: execute each asntdiff cmmand; if differences exist, repair them and clear down the default differences table ASN.ASNTDIFF.
# The asntdiff returns 0 for no differences found, 1 for differences found.
while read diffline
do
	${diffline}
	rc=$? 
	case $rc in
	0) 	: ;;
	1) 	# asntrep and asntdiff take the same arguments:
		repair=$(print ${diffline} | sed -e 's/asntdiff/asntrep/')
	   	${repair}
	   	rc=$? ; (( rc > 0 )) && print "Error executing ${repair}" && exit 2 		 
	   	db2 -vm "truncate table asn.asntdiff immediate"
		wait ;;
	*) 	print "Error executing ${diffline}" && exit 2 ;;
	esac
done < ${workdir}/generate_asntdiff_commands.out
		
db2 connect reset > /dev/null
exit 0 
