#! /bin/sh
#set -xv
######################################################################
#
# Copyright (C) GemTalk Systems 1986-2018.  All Rights Reserved.
#
# Script for doing "QUICK" 670 to 671 upgrade.
#
# See usage() below or run 'upgradeImage670to671 -h' for detailed 
# command line options
#
######################################################################

# leave a blank line
echo 

# 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="upgradeImage670to671"

usage() {
  cat <<EOF

Usage:
$comid [-h][-s<stone>][-p<password>]
-h              display this message and exit 
-s<stone>       name of running stone (default: 'gemserver67')
-p<password>    SystemUser password for logging in  (default: 'swordfish')

Description:

Script for doing a quick 6.7.0 to 6.7.1 upgrade (skips kernel file-in).

EOF
  exit $status
}


# ---
# defaults
# ---
stone="gemserver67"
password="swordfish"

# ---
# process command line
# ---
while getopts "hp:s:" opt; do
  case $opt in
    h) status=0; usage ;;
    s) stone=$OPTARG ;;
    p) password=$OPTARG ;;
    \?) status=1; usage ;;
  esac
done

# ---
# do a sanity check on login parameters
# ---
$GEMSTONE/bin/topaz -il <<EOF> /dev/null 2>&1
iferror exit
set user SystemUser password $password gemstone $stone
login
logout
exit
EOF
status=$?
if [ $status -ne 0 ]; then
  echo "$comid [ERROR]: Failed to login as SystemUser on stone $stone"
  usage
fi

#$GEMSTONE/bin/topaz -il <<EOF> /dev/null 2>&1
$GEMSTONE/bin/topaz -il <<EOF  2>&1
iferror exit
set user SystemUser password $password gemstone $stone
login
expectvalue true
run
| curHist oldVer newVer |
SystemRepository _setVersion.
curHist := (Globals at: #DbfHistory ifAbsent: ['']).
oldVer := '6.7'.
newVer := System gemVersionAt: #gsRelease .
Globals at: #DbfHistory put: curHist, 'GemStone ', oldVer, 
                             ' to ', newVer, ' QUICK upgrade (no kernel file-in) completed at ' ,
                             DateTime now asString , Character lf .
^ System commitTransaction
%
logout
exit
EOF
status=$?
if [ $status -ne 0 ]; then
  echo "$comid [ERROR]: Problem with quick upgrade!"
  exit 1
fi

echo
echo
echo "$comid : Successful quick 670 to 671 upgrade!"
echo

exit 0
