#!/bin/ksh -p

#
# ident "@(#)utdssync.ksh	1.7 04/01/19 SMI"
#
# Copyright 2002-2004 Sun Microsystems, Inc.  All rights reserved.
# Use is subject to license terms.
#

umask 022
PATH="/usr/sbin:/usr/bin:/bin"

TMPDIR=/var/opt/SUNWut/tmp
PROGRAM_ID="${0##*/}"
PROGRAM_OPTS="[-v]"
SDS_DSSERV_CONF="/etc/opt/SUNWut/srds/current/utdsd.conf"
BASEDIR=/etc/opt/SUNWut/basedir
UTPRODDIR="$BASEDIR"/lib
UTO_BASEDIR="$("$UTPRODDIR"/utprodinfo -r  SUNWuto)/SUNWut"
UTO_SBIN="${UTO_BASEDIR}/sbin"
UTRCMD="$UTO_BASEDIR/lib/utrcmd"
OPTSTR="v"
TMP_FILE="${TMPDIR}/${PROGRAM_ID}.$$.tmp"
VERBOSE=false
UPG_GROUP=""
HOSTNAME=$(hostname)
Error=false
UTADMIN_CONF=/etc/opt/SUNWut/utadmin.conf

trap "" INT QUIT  # ignore these signals

trap "CleanupAndExit 1" TERM

. ${UTO_BASEDIR}/lib/support_lib/utsync_lib

GetDsGroup() {
  if print $* | grep "UT_Replica_Id_Non" >/dev/null 2>&1; then
      return 0
  fi
  print $* | sed "s/^.*UT_Replica_Id_Pri //"
  return 0
}


#
# returns true if the server is the primary else false.
# It's a primary if pull_replica string is found in the
# config file
#
is_Primary() {
    # using egrep for regular expression
    print $* | egrep "UT_Replica_Id_Non|UT_Replica_Id_Pri" >/dev/null 2>&1
}


#
# extracts the port number from the string passed in.
# the string is the output of the utreplica -i command.
#
extract_Port() {
    print $* | \
	sed -n -e 's/.* UT_Replica_Port_Num=\([0-9][0-9]*\)[ ]*.*/\1/p'
}


#
# extracts the SunRay version  number from the string passed in.
# the string is the output of the utreplica -i command.
#
extract_Version() {
    print $* | sed -n -e 's/^Sun Ray server \([0-9]\).*/\1/p'
}


#
# MAIN
#


while getopts $OPTSTR OPT 2>/dev/null; do
  case "$OPT" in
    v) VERBOSE=true
       OUTPUT="";;
   \?) Usage;;
  esac
done
shift $(($OPTIND - 1))

if (( $# != 0 )); then
  Usage
fi

CheckUidIsZero
if [[ ! -f $UTADMIN_CONF ]]; then
   Fatal \
	"This server has not been configured.  You don't\
	\nneed to run utdssync on this server because SunRay Datastore\
	\nservice is required only on configured servers."
fi

UTREPLICA_I=$(${UTO_SBIN}/utreplica -i)

if ! is_Primary ${UTREPLICA_I}; then
   Fatal "must be run on the primary Sun Ray datastore server"
fi

GROUP=$(GetDsGroup ${UTREPLICA_I})

PORT=$(extract_Port ${UTREPLICA_I})
if [[ $PORT -eq 7012 ]]; then
    print "\n... server $HOSTNAME is already using the new SRDS default port."
fi

#
# Now contact each server and to make sure that they are running SR 2.0 or
# later.
#
for HOST in $GROUP
do
    resp=$(eval $UTRCMD -n $HOST ${UTO_SBIN}/utreplica -i 2> $TMP_FILE)
    if [[ $? -ne 0 ]]; then
	print -u2 "\nError: failed to contact the utrcmd service on $HOST."
	sed -e "s/^/	/" $TMP_FILE
	Error=true
	continue
    fi
    if [[ $(extract_Version $resp) -lt "3" ]]; then
	print -u2 "\nError: SunRay software on server $HOST has not been upgraded."
	Error=true
	continue
    fi

    # check if the primary for the remote server is the local host
    if print ${resp} | grep "UT_Replica_Id_Sec"  >/dev/null 2>&1; then
	primServ=$(print ${resp} | sed "s/^.*UT_Replica_Id_Sec //")
	if [[ "$primServ" != "$HOSTNAME" ]]; then
	    print -u2 "\nError: $HOST is listed as a secondary server for this primary.\n" \
	    	"However, it does not have $HOSTNAME configured as its primary."
	    Error=true
	    continue
	fi
    else
	print -u2 "\nError: $HOST is listed as a secondary server for this primary.\n" \
	    "However, it is not configured properly as a secondary server."
	Error=true
	continue
    fi

    if [[ $(extract_Port $resp) -ne 7012 ]]; then
	UPG_GROUP="$UPG_GROUP $HOST"
    else
	print "\n... server $HOST is already using the new SRDS default port."
    fi
done

if $Error; then
    print -u2 "\nYou must upgrade all the servers within the failover group to\n" \
    	"2.0 or later and have the secondary servers configured properly before\n" \
	"you can transition to the new SRDS port."
    CleanupAndExit 1
fi

if [[ $PORT -ne 7012 ]]; then
    print "\n... transitioning to the new SRDS default port on ${HOSTNAME}."
    if $VERBOSE; then
	${UTO_SBIN}/utreplica -z | tee $TMP_FILE | sed -e 's/^/    /'
    else
	${UTO_SBIN}/utreplica -z > $TMP_FILE
    fi
    if [[ $? -ne 0 ]]; then
	if ! $VERBOSE; then
	    sed -e "s/^/	/" $TMP_FILE
	fi
	print -u2 "\nError: failed to convert the port number on $HOSTNAME"
	CleanupAndExit 1
    fi
fi

FAILED_SERVER=""
for HOST in $UPG_GROUP
do
    print "\n... transitioning to the new SRDS default port on ${HOST}."
    /bin/rm -f $TMP_FILE
    if $VERBOSE; then
    	eval $UTRCMD -n $HOST ${UTO_SBIN}/utreplica -z | tee $TMP_FILE \
		| sed -e 's/^/    /'
    else
    	eval $UTRCMD -n $HOST ${UTO_SBIN}/utreplica -z > $TMP_FILE
    fi
    if [[ $? -ne 0 ]]; then
	print -u2 "\nError: utrcmd failed to invoke the operation on $HOST"
	if ! $VERBOSE; then
	    sed -e "s/^/	/" $TMP_FILE
	fi
	FAILED_SERVER="${FAILED_SERVER} ${HOST}"
    else
	egrep "Warning|Error" $TMP_FILE >/dev/null 2>&1
	if [[ $? -eq 0 ]]; then
	    # found some error
	    print -u2 "\nError: failed to convert the port number on $HOST"
	    if ! $VERBOSE; then
		sed -e "s/^/	/" $TMP_FILE
	    fi
	    FAILED_SERVER="${FAILED_SERVER} ${HOST}"
	fi
    fi
done

if [[ -n "${FAILED_SERVER}" ]]; then
    print -u2 "\n\nFailed to convert the following server(s):"
    print -u2 "\t${FAILED_SERVER}"
fi

CleanupAndExit 0

