#! /bin/sh
# set -xv
#=========================================================================
# Copyright (C) GemTalk Systems 1986-2014.  All Rights Reserved..
#
# Name - upgradeSeasideImage
# Installed as - upgradeSeasideImage
#
# Purpose - 
#
# This script re-enables SessionMethods recompiles all methods managed by Monticello.
# Currently upgrades from GemStone/S 64 Bit 2.4.4.5 or later are supported.
#
# 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 3.x Installation Guide and/or
#   release notes for detailed instructions.
#
# $Id: upgradeSeasideImage.sh 19139 2008-05-30 23:48:14Z stever $
#
#=========================================================================

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="upgradeSeasideImage"              # this script's name

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

. $GEMSTONE/bin/misc.sh

usage() {
  cat <<EOF
Usage:
$comid [-c <tempObjCacheSize>][-s <stoneName>]
Environment Requirements:
    GEMSTONE          set to a 3.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 3.x stone.
        Default: gs64stone
    -c <tempObjCacheSize>
        size of temp obj cache to use in KB.
        Default: 100000
EOF
}


defaultErrorControl

# 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$$

# create a gem config file to use

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

cat <<EOF

Starting GemStone/S 64 3.x Seaside upgrade.

Note: This script should be run AFTER upgradeImage has been run.
      SessionMethods will be enabled and all code included in the GLASS configuration will be reloaded.

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/upgradeSeasideImage.topaz> $upgradeLogDir/topaz.out 2>&1

topaz_stat=$?
if [ $topaz_stat -eq 0 ]; then
  echo "Seaside Upgrade completed. No errors detected."
#  echo "HOWEVER ... there is still cleanup needed in the script itself (see MADynamicobject, MCDirtyPackageInfoi, inlined)"
  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
