#! /bin/sh
#set -xv
#=========================================================================
# Copyright (C) VMware, Inc. 1986-2011.  All Rights Reserved..
#
# Name - startfastfdcto6xfile.sh
# Installed as - startfastfdcto6xfile
#
# Purpose - This script is used in step 9 below.  It runs a fast FDC on a
#           GemStone/64 2.x database converted from a GemStone 6.x database.
#
# Steps for converting for a GS64 fast FDC
#
#  1) File in $GEMSTONE_22/upgrade/preConvFor62Gc.topaz to the 6.x 
#     production database.  This must be done BEFORE making the copy for
#     the conversion and offline GC.  This step needs to be performed only
#     once on the 6.x production system.
#
#  2) Shutdown the 6.x production database and make a copy of the extents.
#
#  3) Start the copy of 6.x production using startstone.
#
#  4) Run $GEMSTONE_22/upgrade/convprepForGc6x on the copy.  This will
#     shutdown the database when finished.
#
#  5) Start a new 2.x stone with a large shared page cache and pregrow 
#     the extents .
#
#  6) run $GEMSTONE_22/bin/conv62To2xForFastGc to perform the low level 
#     conversion.  The new 2.x database will be shutdown automatically 
#     when this step is completed.
#
#  7) Restart the new 2.x stone using startstone
#
#  8) Run the $GEMSTONE_22/bin/startotcachewarmers script.  This will 
#     start object table cache warmer gems to load the object table into 
#     the shared page cache.
#
#  9) Run the $GEMSTONE_22/bin/startfastfdcto6xfile script to start 
#     the fast offline FDC.  This will produce a binary file containing
#     a list of dead objects found by the FDC.  The file will be in 
#     GemStone 6.x oop format.
#
# 10) Build the offline GC user action library in
#     $GEMSTONE_22/examples/offlinegc6x.  Only this version of the 
#     offline GC user action will work correctly with the file produced
#     in step 9.  Remember to set $GEMSTONE to reference your 6.x 
#     GemStone tree when compiling and linking.  If you cannot build
#     the user action library, contact GemStone support to obtain it.
#
# 11) Use the user action library built in step 10 to run the MGC on
#     the production database at a convenient time.  See documentation
#     in $GEMSTONE_22/examples/offlinegc6x for more details.
#
# $Id: startfastfdcto6xfile.sh,v 1.2 2008-01-09 22:50:53 stever Exp $
#
#=========================================================================

# must have $GEMSTONE
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

# ensure a minimal path
if [ "x$PATH" = "x" ]; then PATH=:/bin:/usr/bin:/usr/ucb; export PATH; fi 

# error control - do not allow hup
trap '' 1

comid="startfastfdcto6xfile"

usage() {
  cat <<EOF
Usage:
$comid [-h][-n<numGems>][-s<stone>][-w<delayTime>][-W]

-h              display this message and exit 
-b<bufSize>     number of pages in the page buffer for the FastFdc (default: 1024)
-f<outfile>     Full path and name of the output file (default: fdc6x.bm)
-n<numWarmers>  number of FDC warmer gems to start (default: 5)
-s<stone>       name of running stone (default: 'gs64stone')
-W              wait for the FDC to exit before exiting this script
                (default: spawn the FDC gem in the background and exit immediately)

Description:

Script for starting a fast findDisconnectObjects (FDC) operation on the repository.
The code will use a high amount of disk I/O and CPU resources in order to complete
the FDC as quickly as possible.  The resulting list of dead objects will be written
to a binary output file which is readable by the offline GC code for GemStone 6.x.
The stone is automatically shutdown at the end of the FDC operation.
EOF
  exit $status
}

# ---
# defaults
# ---
bufSize=1024
fdcFile="fdc6x.bm"
numWarmers=5
stone="gs64stone"
delayTime=1
waitForFdc=0

# ---
# process command line
# ---
while getopts "hb:f:n:s:W" opt; do
  case $opt in
    h) status=0; usage ;;
    b) bufSize=$OPTARG ;;
    f) fdcFile=$OPTARG ;;
    n) numWarmers=$OPTARG ;;
    s) stone=$OPTARG ;;
    W) waitForFdc=1 ;;
    \?) status=1; usage ;;
  esac
done

if [ $numWarmers -lt 1 ]; then
  echo "$comid [ERROR]: numWarmers is too small."
  status=1
  usage
  exit $status
fi


HERE=`pwd`
touch $HERE/foo > /dev/null 2>&1
status=$?
if [ $status -ne 0 ]; then
  echo "$comid [ERROR]: Current directory must be writable."
  usage
fi
rm $HERE/foo > /dev/null 2>&1

if [ -f "$fdcFile" ]; then
  echo "$comid [ERROR]: output file $fdcFile already exists"
  usage
  exit 1
fi

touch $fdcFile > /dev/null 2>&1
status=$?
if [ $status -ne 0 ]; then
  echo "$comid [ERROR]: Cannot create output file $fdcFile."
  usage
  exit 1
fi
rm $fdcFile > /dev/null 2>&1


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

rm fastFdc.log > /dev/null 2>&1
$GEMSTONE/sys/geml << EOF > fastFdc.log 2>&1 &
  runFastFdcToFileFor6x '$stone' $bufSize $numWarmers '$fdcFile'
EOF

thisPid=$!
echo "Finished spawning fast FDC gem with process ID $thisPid."

if [ $waitForFdc -eq 1 ]; then
  echo "waiting for the FDC gem with the process ID $thisPid to exit:"
  wait $thisPid
fi

exit 0
