#! /bin/sh
#set -xv
#=========================================================================
# Copyright (C) VMware, Inc. 1986-2011.  All Rights Reserved..
#
# Name - convprep.sh
# Installed as - convprep1x
#
# Purpose -
#
# This script is executed as step 4 of GemStone/S 64 Bit 1.x to 2.x 
# conversion as outlined below.
# It is run with a running 1.x stone. This script does the
# following steps:
#   1. run cache warmers to load shared OT into the SPC (optional)
#   2. file in the new SmallDouble class definition and commit.
#   3. SystemRepository>>reclaimAll
#   4. Stop all remaining sessions
#   5. Object table scan
#   6. Data page scan
#
# CUSTOMER UPGRADE PROCEDURE
#   Please refer to the GemStone/S 64 Bit 2.x Installation Guide for detailed
#   Instructions.
# STAGE 1:
# 1. maintain your 1.x environment, e.g. GEMSTONE. DO NOT set GEMSTONE to
#    the 2.x product yet
# 2. login to 1.x stone, stop all other user sessions, and set SystemUser
#    password to 'swordfish'.
# 3.1 set environment variable upgradeLogDir to a writable directory
# 3.2 set environment variable GEMSTONE_22 to be the path to the 
#      product directory of the 2.x product tree .
# 4. run the script convprep1x.  This script is found in the 2.x product tree, in
#    the upgrade directory. This 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 1.x 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 conv1xTo2x. 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 conv1xTo2x to exit.
# 9. restart the 2.x stone
# 10. run the script upgradeImageFrom1x. 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: convprep.sh,v 1.15 2008-01-09 22:50:52 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

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

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

. $GEMSTONE/bin/misc.sh

defaultErrorControl

info() {
  echo $comid[INFO]: $*
}
error() {
  echo $comid[ERROR]: $*
}
usage() {
cat <<EOF
Usage:
convprep [-h] [-s <stonename>] [-c -n <numcachewarmers>]
Prepare a GemStone/S 64 Bit 1.x repository for conversion to 2.x.
Environment requirements:
    GEMSTONE          set to a 1.x GemStone/S 64 Bit product tree
    upgradeLogDir     set to a writable directory
Parameters:
    -c  prewarm the cache using startcachewarmers (default: no prewarm)
    -h print this Usage and exit
    -n <numcachewarmers>
        where <numcachewarmers> is the number of cachewarmer gems to start
        (defaults to 1 if no number specified; has no effect if -c is not
        specified)
    -s <stonename>
        where <stonename> is the name of a running 1.x stone (defaults
        to gs64stone)
EOF
}

# defaults
stoneName=gs64stone
numcachewarmers=1
startcachewarmers=0

#process command line
while getopts "hcn:s:" opt; do
  case $opt in 
    c ) startcachewarmers=1 ;;
    h ) usage; exit 0 ;;
    n ) numcachewarmers=$OPTARG ;;
    s ) stoneName=$OPTARG ;;
   \? ) error "bad arg: $opt"; usage; exit 1 ;;
  esac
done
export stoneName

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

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

# make sure stone is running
info "verifying $stoneName is running..."
$GEMSTONE/bin/waitstone $stoneName -1 > /dev/null 2>&1
status=$?
if [ $status -ne 0 ]; then
  error "...no stone named $stoneName found"
  usage
  exit $status
fi
info "...$stoneName is running."

# start cache warmer gems if specified
if [ $startcachewarmers -eq 1 ]; then
  info "starting cache warmers..."
  if [ $numcachewarmers -le 0 ]; then
    numcachewarmers=1
  fi
  $GEMSTONE/bin/startcachewarmer -n$numcachewarmers -s$stoneName -W
  status=$?
  if [ $status -eq 0 ]; then
    info "...successfully started cache warmers."
  else
    error "...cache warming failed!"
    exit $status
  fi
fi

# clean up from previous runs, if any...
rm $upgradeLogDir/convprep.log $upgradeLogDir/*.bm $upgradeLogDir/*.topaz \
  $upgradeLogDir/from*.out >/dev/null 2>&1

# start topaz
info "starting topaz for conversion prep. This may take a while..."

$GEMSTONE/bin/topaz -l -i << EOF >$upgradeLogDir/convprep.log 2>&1
output push $upgradeLogDir/convprep.out only
set gemstone $stoneName
set user SystemUser pass swordfish
login

iferror exit 1
display resultcheck

input $GEMSTONE_22/upgrade/convprep1x.topaz

! do a clean shutdown
expecterror GemStoneError 4057
send System shutDown

exit 
EOF

status=$?
if [ $status -ne 0 ]; then
  error "...topaz exited with status $status"
else
  info "...conversion preparation complete."
  touch $upgradeLogDir/from1x_1.out
fi
exit $status
