#!/bin/sh
#
# UPGRADE100
# @(#) generic/upgrade/%M% %I %G%
#
# Translated from
# UPGRADE49
#
# This shell script upgrades a 48, 49, 491, 492, 10.0.0 or 10.0.1
# server to 10.0.2 server.
#

############################################################
# Algorithm
#
#	PHASE I
#
#	Ensure that the device to be upgraded is the correct version
#	and save the previous stack size and memory size
#
#	Check that the server of correct version is running
#
#	Save the previous database segment information
#
#       Set the value for cschedspin & cclkrate
#
#	Set the new configuration values to appropriate defaults
#
#	PHASE II
#
#	Shutdown the existing server and boot the new 10.0.2 server.
#	
#	Upgrade the existing device to 10.0.2
#
#
#	To prevent the user from being swamped with pages of output, the
#	output from these phases is saved in various log files in the 
#	user's $SYBASE/upgrade directory. 
#
#	Here are the files created
#
#	1. <SERVERNAME>.log	A log of the upgrade progress
#	2. <SERVERNAME>.ocon	Previous configuration settings
#	3. <SERVERNAME>.rw2	Created for configuration changes
#	4. <SERVERNAME>.u92	Created while doing the final phase of upgrade
#	
#

	
############################################################
#
# Parse arguments and do initialization
#

if [ $# -lt 4 ]
then
        echo 'Usage: upgrade100 servername masterdevice oldsybasedir newsybasedir
 [sapassword] [testnum]'
        exit 1
fi

SERVERNAME=$1           # SERVERNAME:string,required
MASTERDEVICE=$2         # MASTERDEVICE:string,required
OLDSYBASEDIR=$3         # OLDSYBASEDIR:string,required
NEWSYBASEDIR=$4         # NEWSYBASEDIR:string,required
SAPASSWORD=$5           # SAPASSWORD:string,required (or "")
TESTNUM=$6              # TESTNUM:string

############################################################
#
# platform specific choice section
#
# comment out the following two lines after uncommenting
#       /adding the correct stacksize default for your
#       10.0.2 platform
#
#PSTACKSZ="0"
#
#       Sun4, FTX, HP 9000, NCR, RS6000
PSTACKSZ="28672"                # sun4, hp800, ncr_svr4, rs6000, vms
#       Pyramid
#PSTACKSZ="40960"               # Pyramid
#       Sequent,ATT,NCR
#PSTACKSZ="30711"               # ATT
#       VOS
#PSTACKSZ="38912"               # VOS
#
if [ ${PSTACKSZ} -eq 0 ]
then
        echo "=== Choose stacksize default for this platform"
        echo "=== by editing this shell script (upgrade10)"
	echo " "
        exit 2
fi

#PMEMSIZE="0"
#
#       Sun4, FTX, HP 9000, NCR, RS6000
PMEMSIZE="5120"                # sun4, hp800, ncr_svr4, rs6000, vms
#       Pyramid
#PMEMSIZE="5120"               # Pyramid
#       Sequent,ATT,NCR
#PMEMSIZE="5120"               # ATT
#       VOS
#PMEMSIZE="5120"               # VOS
#
if [ ${PMEMSIZE} -eq 0 ]
then
        echo "=== Choose memsize default for this platform"
        echo "=== by editing this shell script (upgrade10)"
	echo " "
        exit 3
fi

############################################################
#
# timeout values
#
TIMEOUTINCR=5           # wait 5 seconds between connect check
TIMEOUTCOUNT=108        # try the check 108 times (9 minutes)

############################################################
#
# dump values
#

echo " "
echo ">>> Running upgrade100 with the following configuration:"
echo SERVERNAME="${SERVERNAME}"
echo MASTERDEVICE="${MASTERDEVICE}"
echo OLDSYBASEDIR="${OLDSYBASEDIR}"
echo NEWSYBASEDIR="${NEWSYBASEDIR}"


#
# use DSQUERY, DSLISTEN, DSCONSOLE rather than the -s dataserver option
#       as older versions of the server might not support this option
#
DSQUERY="${SERVERNAME}"
export DSQUERY
DSLISTEN="${SERVERNAME}"
export DSLISTEN
DSCONSOLE="${SERVERNAME}"
export DSCONSOLE

#
# handy synonyms
#
NEWBINDIR="${NEWSYBASEDIR}"/bin
OLDBINDIR="${OLDSYBASEDIR}"/bin
UPGRADEDIR="${NEWSYBASEDIR}"/upgrade
NULL=/dev/null

##############################################################
# Function to wait for server to come up by watching the
# error log
# Parameters: bindir, "type SQL Server", errorlog, flags
#

WaitForStart()
{
        # wait for the server to start ...
        sleep ${TIMEOUTINCR}
        TIMEOUTCOUNTER=0
        until $1/isql -Usa -P"${SAPASSWORD}" $4 -S"${SERVERNAME}" < ${NULL} > ${NULL} 2>&1
        do
                TIMEOUTCOUNTER=`expr ${TIMEOUTCOUNTER} + 1`
                if [ ${TIMEOUTCOUNTER} -gt ${TIMEOUTCOUNT} ]
                then
                        echo ">>> Upgrade cannot connect to $2 SQL Server."
			UA_ERR=19
			return 19
                fi
                sleep ${TIMEOUTINCR}
        done

        # wait for the server to come up all the way
        # Do NOT reset counter - we want the full timeout.
        while test 1 -eq 1
        do
                tail -20 $3 | \
                        egrep -i "server  Recovery complete." > ${NULL} 2>&1
                if [ $? -eq 0 ]
                then
                        break
                fi

                sleep ${TIMEOUTINCR}
                TIMEOUTCOUNTER=`expr ${TIMEOUTCOUNTER} + 1`

                if [ ${TIMEOUTCOUNTER} -gt ${TIMEOUTCOUNT} ]; then
                        echo ">>> $2 SQL Server ${SERVERNAME} did not come up after 9 minutes <<<"
			UA_ERR=20
			return 20
                fi
        done

        return 0
}


###########################################################
# Function to wait for the server to shutdown by trying to 
# connect to it.

WaitForShutdown()
{
        sleep ${TIMEOUTINCR}
        TIMEOUTCOUNTER=0
	
	while test 1 -eq 1
	do
		${NEWBINDIR}/isql -Usa -P"${SAPASSWORD}" -S"${SERVERNAME}" < ${NULL} > ${NULL} 2>&1
		STAT=$?
		if [ "${STAT}" -eq "0" ]
		then
			sleep ${TIMEOUTINCR}
			TIMEOUTCOUNTER=`expr ${TIMEOUTCOUNTER} + 1`
		
			if [ ${TIMEOUTCOUNTER} -gt ${TIMEOUTCOUNT} ]
			then
				echo ">>> SQL Server could not shutdown. "
				echo " "
				UA_ERR=21
				return 21
			fi
		else
			return 0
		fi
	done

	return 0
}
		
	
############################################################
#
# make sure the old server is running
#
${NEWBINDIR}/isql -Usa -P"${SAPASSWORD}" -S"${SERVERNAME}"  < ${NULL} > ${NULL} 2>&1
STAT=$?
if [ "${STAT}" -ne "0" ]
then
    echo ">>> Connection to existing SQL Server failed."
    echo ">>> Please boot the SQL Server to be upgraded to 10.0.2 before"
    echo ">>> running upgrade100."
    echo " "
    exit 1
fi


#
# construct template for output file name
#       encode the servername in all names
#
F=${NEWSYBASEDIR}/upgrade/${SERVERNAME}


#############################################################
#
# PHASE 1 BEGINS
#
echo "PHASE 1 begins..." >> $F.log
echo "==============================" >> $F.log
echo ""
echo ">>> PHASE 1 begins....... "
echo " "
############################################################

############################################################
#
# Save previous configuration in file
#
echo ">>> Saving current configuration in $F.ocon file."
echo " "
${NEWBINDIR}/buildmaster -d${MASTERDEVICE} -yall > $F.ocon


############################################################
#
# Save the database segment information in log file.
#
echo ">>> Saving the database segment information in $F.log file."
echo " "
echo "Log for upgrade of the ${SERVERNAME} SQL Server" >> $F.log
echo "to 10.0.2 release level starting `date`" >> $F.log
echo "==============================" >> $F.log
${NEWBINDIR}/isql -Usa -P"${SAPASSWORD}" -S"${SERVERNAME}" \
            < ${UPGRADEDIR}/usage.sql >> $F.log 2>&1
echo "==============================" >> $F.log


############################################################
#
# Ensure that the device to be upgraded is the correct version.
# (use the generated "Old Config" file)
#
VERSIONSTRING=`grep  "cfgupgradeversion" ${F}.ocon | sed -e "s/^[^0-9]*//`
case "${VERSIONSTRING}" in
        48*)
                VERSION="4.8"
                echo ">>> Upgrading from 4.8.x."
		echo " "
                ;;
        490)
                VERSION="4.9.0"
                echo ">>> Upgrading from 4.9.0."
		echo " "
                ;;
        491)
                VERSION="4.9.1"
                echo ">>> Upgrading from 4.9.1."
		echo " "
                ;;
        492)
                VERSION="4.9.2"
                echo ">>> Upgrading from 4.9.2."
		echo " "
                ;;
        1000)
                VERSION="10.0.0"
                echo ">>> Upgrading from 10.0.0"
		echo " "
                ;;
        1001)
                VERSION="10.0.1"
                echo ">>> Upgrading from 10.0.1"
		echo " "
                ;;
        1002)
                echo ">>> SQL Server is already a 10.0.2 SQL Server"
                echo ">>> No upgrade is required"
		echo " "
                exit 3
                ;;
        *)
                echo ">>> Upgrade10 can only upgrade a Release 4.8 or above."
		echo " "
                exit 3
                ;;
esac

#
# Initialize return status
#
UA_ERR=0

######################################################
# Save current stack size value for use if larger than
# the default value
#

if [ ${VERSION} = "4.8" -o ${VERSION} = "4.9.0" -o ${VERSION} = "4.9.1" -o ${VERSION} = "4.9.2" -o ${VERSION} = "10.0.0" -o ${VERSION} = "10.0.1" ]
then
	echo ">>> Saving previous stack size."
	echo " "
	CSTACKSZ=`grep  "cfgstacksz" $F.ocon | sed -e "s/^[^0-9]*//`
fi


############################################################
#
# UPGRADE from 4.8, 4.9.0, 4.9.1, 4.9.2, 10.0.0, 10.0.1
#
# fix up stack size. 
#

if [ ${CSTACKSZ} -lt ${PSTACKSZ} ]
then
	echo ">>> Stack size is $CSTACKSZ. This is less than the default"
	echo ">>> size. Setting the value of stack size to default size."
	echo " "
	echo "Setting the value of cfgstacksz to ${PSTACKSZ}." >> $F.rw2
	echo ${NEWBINDIR}/buildmaster -d${MASTERDEVICE} -ycfgstacksz=${PSTACKSZ} >> $F.rw2	
	${NEWBINDIR}/buildmaster -d${MASTERDEVICE} -ycfgstacksz=${PSTACKSZ} >> $F.rw2
else
	echo ">>> Stack size is set to $CSTACKSZ. This is more than "
	echo ">>> or equal to the default stack size of $PSTACKSZ. "
	echo ">>> Leaving the stack size of $CSTACKSZ unchanged."
	echo " "
	echo "Stack size is $CSTACKSZ which is more than the default" >> $F.rw2
	echo "size of $PSTACKSZ. stack size is left unchanged." >> $F.rw2
fi

######################################################
# Save current memory size value for use if larger than
# the default value
#

if [ ${VERSION} = "4.8" -o ${VERSION} = "4.9.0" -o ${VERSION} = "4.9.1" -o ${VERSION} = "4.9.2" -o ${VERSION} = "10.0.0" -o ${VERSION} = "10.0.1" ]
then
	echo ">>> Saving previous memory size."
	echo " "
	CMEMSIZE=`grep  "cmemsize" $F.ocon | sed -e "s/^[^0-9]*//`
fi

############################################################
#
# UPGRADE from 4.8, 4.9.0, 4.9.1, 4.9.2, 10.0.0, 10.0.1
#
# fix up memory size. 
#

if [ ${CMEMSIZE} -lt ${PMEMSIZE} ]
then
	if [ ${CMEMSIZE} -eq 0 ]
	then
		echo ">>> Memory size currently configured to default value,"
		echo ">>> so it is left unchanged."
		echo " "
		echo "Memory size is configured to default value." >> $F.rw2
		echo "Leaving the memory size unchanged" >> $F.rw2
	else
		echo ">>> Memory size is $CMEMSIZE. This is less than the default"
		echo ">>> size. Setting the value of memory size to default size."
		echo " "
		echo "Setting the value of cmemsize to ${PMEMSIZE}." >> $F.rw2
		echo ${NEWBINDIR}/buildmaster -d${MASTERDEVICE} -ycmemsize=0 >> $F.rw2	
		${NEWBINDIR}/buildmaster -d${MASTERDEVICE} -ycmemsize=${PMEMSIZE} >> $F.rw2
	fi
else
	echo ">>> Memory size is set to $CMEMSIZE. This is more than "
	echo ">>> or equal to the default memory size of $PMEMSIZE. "
	echo ">>> Leaving the memory size of $CMEMSIZE unchanged."
	echo " "
	echo "Memory size is $CMEMSIZE which is more than default" >> $F.rw2
	echo "size of $PMEMSIZE.  Memory size is left unchanged." >> $F.rw2
fi

############################################################
#
# UPGRADE from 4.8, 4.9.0, 4.9.1, 4.9.2, 10.0.0, 10.0.1
#
# update the value of cschedspin to 2000 if the value is not 0
# Some customers were told to lower the value of cschedspin to work
# around a performance problem.  This problem has been resolved
# so we need to make sure the value has the right value.  A value
# of 0 probably means the server is running on a dedicated machine.
# We'll leave the value 0.
#

	CSCHEDSPINVALUE=`grep  "cschedspins" ${F}.ocon | sed -e "s/^[^0-9]*//`
	if [ "${CSCHEDSPINVALUE}" -ne "0" ]
	then
		echo "Setting the value of cschedspins to 2000." >> $F.rw2
		echo ${NEWBINDIR}/buildmaster -d"${MASTERDEVICE}" -ycschedspins=2000 >> $F.rw2
		${NEWBINDIR}/buildmaster -d"${MASTERDEVICE}" -ycschedspins=2000 >> $F.rw2
	fi

############################################################
#
# UPGRADE from 4.8, 4.9.0, 4.9.1, 4.9.2, 10.0.0, 10.0.1
#
# Configuration values used by buildmaster -y below
#       to configure the 10.0.1 server
# update the value of cclkrate to CFGCLKRATE.  This is a platform specific
# number based on performance testing.
#

	echo "Setting the value of cclkrate to 100000." >> $F.rw2
	echo ${NEWBINDIR}/buildmaster -d"${MASTERDEVICE}" -ycclkrate=100000 >> $F.rw2
	${NEWBINDIR}/buildmaster -d"${MASTERDEVICE}" -ycclkrate=100000 >> $F.rw2

############################################################
#
# UPGRADE from 4.8, 4.9.0, 4.9.1, 4.9.2, 10.0.0, 10.0.1
#
# Configuration values used by buildmaster -y below
#       to configure the 10.0.1 server
# update the value of cfgcpuacctflush to 300 and cfgioacctflush to 1000.
# 

	echo "Setting the value of cfgcpuacctflush to 300." >> $F.rw2
	echo ${NEWBINDIR}/buildmaster -d"${MASTERDEVICE}" \
		-ycfgcpuacctflush=300 >> $F.rw2
	${NEWBINDIR}/buildmaster -d"${MASTERDEVICE}" \
		-ycfgcpuacctflush=300 >> $F.rw2

	echo "Setting the value of cfgioacctflush to 1000." >> $F.rw2
	echo ${NEWBINDIR}/buildmaster -d"${MASTERDEVICE}" \
		-ycfgioacctflush=1000 >> $F.rw2
	${NEWBINDIR}/buildmaster -d"${MASTERDEVICE}" \
		-ycfgioacctflush=1000 >> $F.rw2

############################################################
#
# UPGRADE from 4.8, 4.9.0, 4.9.1, 4.9.2
#
# Configuration values used by buildmaster -y below
#       to configure the 10.0.2 server
# Set configure variables new to 10.0.2 to default values.
#
# Note: if a configure value is being reused, the following method could
# leave the earlier server version unrunable, because the configure value
# is set to an unacceptable value. In the System 10 upgrade, these
# values are truly new, not reused.
#

if [ ${VERSION} != "10.0.0" -a ${VERSION} != "10.0.1" ]
then
	echo "Setting the new configure values." >> $F.rw2

	echo ${NEWBINDIR}/buildmaster -d"${MASTERDEVICE}" \
		-ycfgpasswordexp=0>> $F.rw2
	${NEWBINDIR}/buildmaster -d"${MASTERDEVICE}" \
		-ycfgpasswordexp=0 >> $F.rw2

	echo ${NEWBINDIR}/buildmaster -d"${MASTERDEVICE}" \
		-ycfg_audque_size=100>> $F.rw2
	${NEWBINDIR}/buildmaster -d"${MASTERDEVICE}" \
		-ycfg_audque_size=100 >> $F.rw2

	echo ${NEWBINDIR}/buildmaster -d"${MASTERDEVICE}" \
		-ycaddnetmem=0 >> $F.rw2
	${NEWBINDIR}/buildmaster -d"${MASTERDEVICE}" \
		-ycaddnetmem=0 >> $F.rw2

	echo ${NEWBINDIR}/buildmaster -d"${MASTERDEVICE}" \
		-ycdftpktsz=512 >> $F.rw2
	${NEWBINDIR}/buildmaster -d"${MASTERDEVICE}" \
		-ycdftpktsz=512 >> $F.rw2

	echo ${NEWBINDIR}/buildmaster -d"${MASTERDEVICE}" \
		-ycmaxpktsz=512 >> $F.rw2
	${NEWBINDIR}/buildmaster -d"${MASTERDEVICE}" \
		-ycmaxpktsz=512 >> $F.rw2

	echo ${NEWBINDIR}/buildmaster -d"${MASTERDEVICE}" \
		-yclargextent=0 >> $F.rw2
	${NEWBINDIR}/buildmaster -d"${MASTERDEVICE}" \
		-yclargextent=0 >> $F.rw2

	echo ${NEWBINDIR}/buildmaster -d"${MASTERDEVICE}" \
		-ycfg_burn_factor=5000 >> $F.rw2
	${NEWBINDIR}/buildmaster -d"${MASTERDEVICE}" \
		-ycfg_burn_factor=5000 >> $F.rw2

	echo ${NEWBINDIR}/buildmaster -d"${MASTERDEVICE}" \
		-ycsortbufsize=0 >> $F.rw2
	${NEWBINDIR}/buildmaster -d"${MASTERDEVICE}" \
		-ycsortbufsize=0 >> $F.rw2

	echo ${NEWBINDIR}/buildmaster -d"${MASTERDEVICE}" \
		-ycsortpgcount=0 >> $F.rw2
	${NEWBINDIR}/buildmaster -d"${MASTERDEVICE}" \
		-ycsortpgcount=0 >> $F.rw2

	echo ${NEWBINDIR}/buildmaster -d"${MASTERDEVICE}" \
		-ycbp_hw=0 >> $F.rw2
	${NEWBINDIR}/buildmaster -d"${MASTERDEVICE}" \
		-ycbp_hw=0 >> $F.rw2

	echo ${NEWBINDIR}/buildmaster -d"${MASTERDEVICE}" \
		-ycbp_os=0 >> $F.rw2
	${NEWBINDIR}/buildmaster -d"${MASTERDEVICE}" \
		-ycbp_os=0 >> $F.rw2
fi

###########################################################
#
# start with the old sybase environment
#
SYBASE="${NEWSYBASEDIR}"
export SYBASE

#############################################################
#
# PHASE 1 COMPLETE
#
echo "PHASE 1 completes..." >> $F.log
echo "==============================" >> $F.log
echo ""
echo ">>> PHASE 1 completes....... "
echo " "
############################################################

############################################################
#
# Shutdown existing SQL Server
# If the existing SQL Server is up, shut it down
#

#	make sure that "allow updates" is set to TRUE before starting
#	up the 10.0.2 server.
RET=`${NEWBINDIR}/isql -Usa -P"${SAPASSWORD}" -S"${SERVERNAME}" <<EOF >> $F.log
sp_configure "allow updates", 1
go
EOF
`

echo ">>> Shutting down the existing SQL Server. "
echo " "
echo "Shutdown existing SQL Server." >> $F.log

${NEWBINDIR}/isql -Usa -P"${SAPASSWORD}" -S"${SERVERNAME}" < ${UPGRADEDIR}/shutdown.sql >> $F.log
 
WaitForShutdown  
RET=$?
if [ "$RET" -ne "0" ]
then
# 	The Wait routine has printed messages and set the UA_ERR
	exit $RET
fi

############################################################
#
# Boot 10.0.1 server
#

echo ">>> Booting the new 10.0.2 SQL Server. "
echo " "
${NEWBINDIR}/dataserver -d"${MASTERDEVICE}" -i"${NEWSYBASEDIR}" > ${NULL} 2>&1 &

WaitForStart ${NEWBINDIR} "10.0" errorlog "-J"
RET=$?
if [ "$RET" -ne "0" ]
then
# 	The Wait routine has printed messages and set the UA_ERR
	exit $RET
fi

##############################################################
#
# BEGIN PHASE 2
#
echo ""
echo ">>> PHASE 2 begins....... "
echo ""
echo "PHASE 2 begins..." >> $F.log

##############################################################
#
# Run final phase of upgrade.
#

echo ">>> Final phase to upgrade to 10.0.2 SQL Server. "
echo " "
if [ -f $F.u92 ]
then
        mv -f $F.u92 $F.u92.old
fi
echo ${UPGRADEDIR}/upgrade -S${SERVERNAME}  > ${F}.u92

${UPGRADEDIR}/upgrade -P${SAPASSWORD} -S${SERVERNAME} >> $F.u92
UA_ERR=$?

if [ ${UA_ERR} -eq 2 ]
then
	echo ">>> Server encountered non-fatal errors in final phase of upgrade."
	echo ">>> Check errorlog $F.u92 for details."
	echo " "
	exit 52
fi

if [ ${UA_ERR} -ne 0 ]
then
	echo ">>> Server could not run final phase of upgrade."
	echo ">>> Check errorlog $F.u92 for details."
	echo " "
	UA_ERR=22
	exit 51
fi

##############################################################
#
# END PHASE 2
#
echo ""
echo ">>> PHASE 2 completes......"
echo ""
echo "PHASE 2 completes..." >> $F.log

##############################################################
#
# Inform user that upgrade is done.
#
echo "Upgrade completes." >> $F.log
echo ">>> Upgrade to 10.0.2 SQL Server is now complete."
echo " "

exit ${UA_ERR}
