#! /bin/sh
# set -xv
#=========================================================================
# Copyright (C) VMware, Inc. 1986-2011.  All Rights Reserved..
#
# Name - upgrade2gTo2x.sh  
# Installed as - upgradeImageFrom2g
#
# Purpose - 
#
# This script does a fresh filein of all the kernel classes.
# The filein of the GemStone/S 64 kernel classes will wipe out all the existing 
# methods in kernel classes. Therefore, if users have added methods to 
# kernel classes, they should file them out before running this script. 
# After this  script has been run, those methods should be filed in again.
#
# Requirements:
# 
# The following environment variables should be set: 
#	GEMSTONE 	- set to the GemStone/S 64 product tree.
#       upgradeLogDir   - a writable scratch directory.
#       GEMSTONE_SYS_CONF - 
#
# CUSTOMER UPGRADE PROCEDURE
#   Please refer to the GemStone/S 64 Bit 2.x Installation Guide for detailed
#   Instructions.
# STAGE 1:
# 1. maintain your 2G environment, e.g. GEMSTONE. DO NOT set GEMSTONE to
#    the 2.x product yet!
# 2. login to 2G stone, stop all other user sessions, and set SystemUser
#    password to 'swordfish'.
# 3. set environment variable upgradeLogDir to a writable directory
# 4. run the script convprep2g.  This script is found in the 2.x product tree, in
#    the upgrade directory.  This file should not be in your path, so it will need
#    to be invoked with an explicit path.
# STAGE 2:
# 5. set up your environment ($GEMSTONE) for the 2.x product tree. Maintain
#    your GS 2G extents; they will be read from during stage 2. The environment
#    variable upgradeLogDir must remain the same for stage 2 as it was for
#    stage 1.
# 6. start a 2.x gemstone with a virgin extent0.dbf and a config file specifying
#    all extents required for your 2.x system. Extent pregrow is recommended.
# 7. run the script conv2gTo2x. This script exists in the 2.x product tree
#    under the bin directory, so it needn't be called with an explicit path
# 8. wait for conv2gTo2x to exit.
# 9. restart the 2.x stone
# 10. run the script upgradeImageFrom2g. This script exists in the 2.x product
#     tree under the bin directory, so it needn't be called with an explicit
#     path.
# 11. run the script postconv. This script exists in the 2.x product tree under
#     the bin directory, so it needn't be called with an explicit path.
#
# $Id: upgrade2gTo2x.sh,v 1.4 2008-01-09 22:50:53 stever Exp $
#
#=========================================================================

if [ "a$GEMSTONE" = "a" ]; then
  echo "ERROR: GemStone scripts require a GEMSTONE environment variable."
  echo "       Please set it to the directory where GemStone resides."
  exit 1
fi

upgradeDir=$GEMSTONE/upgrade
export upgradeDir

# maintenance symbols
comid="upgradeImageFrom2g"              # this script's name

# make sure of a minimum path
PATH=:/bin:/usr/bin:/usr/ucb:$PATH; export PATH

. $GEMSTONE/bin/misc.sh

defaultErrorControl

usage() {
  cat <<EOF
Usage:
$comid [-c <tempObjCacheSize>][-s <stoneName>]
Environment Requirements:
    GEMSTONE          set to a 2.x GemStone/S 64 Bit product tree
    upgradeLogDir     set to a writable directory used in previous steps
Parameters:
    -s <stoneName>
        where <stoneName> is the name of a running 2.x stone.
        Default: gs64stone
    -c <tempObjCacheSize>
        size of temp obj cache to use in KB.
        Default: 100000
EOF
}


# default GEM_TEMPOBJ_CACHE_SIZE -- default to 100000
tmpObjSize=100000
stoneName=gs64stone

# process command line
while getopts "c:s:" opt; do
  case $opt in 
    c ) tmpObjSize=$OPTARG ;;
    s ) stoneName=$OPTARG ;;
   \? ) usage; exit 1 ;;
  esac
done
export stoneName

# make sure tmpObjSize is within legal limits
if [ $tmpObjSize -gt 1000000 ] || [ $tmpObjSize -lt 1000 ]; then
  echo "ERROR: Cannot set GEM_TEMPOBJ_CACHE_SIZE to $tmpObjSize."
  echo "ERROR: This value must be between 1000 and 1000000."
  usage
  exit 1
fi

# make sure $upgradeLogDir has been set
if [ a$upgradeLogDir = "a" ]; then
  echo "ERROR: The environment variable upgradeLogDir has not been set."
  usage
  exit 1
elif [ ! -d $upgradeLogDir ]; then
  echo "ERROR: $upgradeLogDir is not a directory."
  usage
  exit 1
fi

# make sure $upgradeLogDir is writable
touch $upgradeLogDir/tmp$$ 2>/dev/null 
if [ "$?" != "0" ]; then
  echo "ERROR: $upgradeLogDir is not writable."
  usage
  exit 1
fi
rm $upgradeLogDir/tmp$$

if [ ! -r $upgradeLogDir/from2g_2.out ]; then
  if [ -r $upgradeLogDir/from2g_3.out ]; then
    echo "ERROR: upgrade2gTo2x conversion step was already completed."
    exit 1
  else
    echo "ERROR: previous upgrade step conv2gTo2x was not completed successfully."
    exit 1
  fi
fi

# create a gem config file to use; note that this will also be used in postconv

cat <<EOF >$upgradeLogDir/conversion.conf
GEM_TEMPOBJ_CACHE_SIZE = $tmpObjSize;
GEM_TEMPOBJ_INITIAL_SIZE = $tmpObjSize;
EOF

cat <<EOF

Starting GemStone/S 64 kernel class filein.

Note: All user defined changes and additions to kernel classes will be
removed. It is therefore advised that all such changes be filed out before
starting conversion and filed back in after conversion is complete. Layered
products which change the kernel classes, such as GemConnect, will also lose
the changes. All such products must be reinstalled following the upgrade.

Stone name is $stoneName.

Press the return key to continue...
EOF
read prompt

$GEMSTONE/bin/waitstone "$stoneName" -1 > /dev/null
waitstone_stat="$?"
if [ $waitstone_stat -ne 0 ]; then
  echo "ERROR: no stone named $stoneName running on this machine."
  usage
  exit 1
fi

echo `date`
echo 'Starting upgrade...'

$GEMSTONE/bin/topaz -i -l -e $upgradeLogDir/conversion.conf < $upgradeDir/upgrade2gTo2x.topaz> $upgradeLogDir/topaz.out 2>&1

topaz_stat=$?
if [ $topaz_stat -eq 0 ]; then
  rm $upgradeLogDir/from2g_2.out >/dev/null 2>&1
  touch $upgradeLogDir/from2g_3.out
  echo "Upgrade completed. No errors detected."
  exit 0
else
  echo "ERROR: topaz exited with status $topaz_stat."
  echo "Please check the file topazerrors.log and all files in "
  echo "$upgradeLogDir for errors."
  exit $topaz_stat
fi
