#! /bin/sh
# set -xv
#=========================================================================
# Copyright (C) GemTalk Systems 1986-2020.  All Rights Reserved.
#
# Name - upgradeComments
# Installed as - upgradeComments
#
# Purpose - 
#
# This script upgrades the comments of all classes in the repository
# to comment format in GS/64 3.1 and later.
#
# 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 Installation Guide and/or
#   release notes for detailed instructions.
#
# $Id: upgradeComments.sh 28349 2012-05-18 22:36:53Z 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="upgradeComments"              # 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:
    -c <tempObjCacheSize>
        where <tempObjCacheSize> is the size of temp obj cache in KB.
    -s <stoneName>
        where <stoneName> is the name of a running 3.x stone.
        Default: gs64stone
EOF
}

defaultErrorControl
tmpObjSize=50000
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 64000000 ] || [ $tmpObjSize -lt 2000 ]; then
  echo "ERROR: Cannot set GEM_TEMPOBJ_CACHE_SIZE to $tmpObjSize."
  echo "ERROR: This value must be between 2000 and 64000000."
  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/comment.conf
GEM_TEMPOBJ_CACHE_SIZE = $tmpObjSize;
EOF

# Check that stone exists
$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

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

echo `date`
echo 'Beginning conversion of comments/descriptions to GS/64 3.1 conventions..'
echo ""

# Now run topaz and execute upgradeComments topaz script
$GEMSTONE/bin/topaz -i -l -e $upgradeLogDir/comment.conf < $upgradeDir/upgradeComments.topaz > $upgradeLogDir/upgradeComments.out 2>&1

topaz_stat=$?
if [ $topaz_stat -eq 0 ]; then
  echo "Upgrade completed. No errors detected."
  echo ""
  exit 0
else
  echo "ERROR: topaz exited with status $topaz_stat."
  echo "Please check for errors in the file" 
  echo "$upgradeLogDir/upgradeComments.out"
  echo ""
  echo "If error is: Error 4067 OutOfMemory "
  echo "(VM temporary object memory is full, perm space overflow)"
  echo "then re-run upgradeComment with a larger -c tempObjCacheSize."
  echo ""
  exit $topaz_stat
fi
