#! /bin/sh
# set -xv
#=========================================================================
# Copyright (C) GemTalk Systems 1986-2020.  All Rights Reserved.
#
# Name - convprep22.sh
# Installed as - convprep22
#
# Purpose - 
#
# This script does prepares a 2.2.x repository for upgrade to version 3.1
# or later.  The repository is automatically shutdown at the end of the
# preparation.
#
# Requirements:
# 
# The following environment variables should be set: 
#	GEMSTONE 	- set to the GemStone/S 64 2.2.x product tree.
#	GEMSTONE_3X 	- set to the GemStone/S 64 3.1 or later 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: convprep22.sh 25988 2011-06-14 17:55:54Z 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

if [ "a$GEMSTONE_3X" = "a" ]; then
  echo "ERROR: This script requires a GEMSTONE_3X environment variable."
  echo "       Please set it to the directory where GemStone 3.x resides."
  exit 1
fi

upgradeDir=$GEMSTONE_3X/upgrade
export upgradeDir

# maintenance symbols
comid="convprep22"              # 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 2.2.x GemStone/S 64 Bit product tree.
    upgradeLogDir     set to a writable directory.
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$$

# Make sure the topazerrors.log file goes into $upgradeLogDir, not pwd.
GS_TOPAZ_ERROR_LOG_DIR=$upgradeLogDir
export GS_TOPAZ_ERROR_LOG_DIR
# 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 2.2.x conversion preparation...

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 conversion preparation...'


# Disable extended character tables for image upgrade.
GS_DISABLE_CHARACTER_TABLE_LOAD=1
export GS_DISABLE_CHARACTER_TABLE_LOAD

$GEMSTONE/bin/topaz -i -l -e $upgradeLogDir/conversion.conf << EOF > $upgradeLogDir/convprep_22_to_31.log 2>&1
output push $upgradeLogDir/convprep_22_to_31.out only
input $upgradeDir/convprep_22_to_31.topaz
EOF

topaz_stat=$?
if [ $topaz_stat -eq 0 ]; then
  echo "Conversion preparation completed. No errors detected."
  exit 0
else
  echo "ERROR: topaz exited with status $topaz_stat."
  echo "Please check the files convprep_22_to_31.out, topazerrors.log in "
  echo "$upgradeLogDir for errors."
  exit $topaz_stat
fi
