#!/bin/sh
##
# This is a sample script for the activation of the program
# ProgControl and it contains proposals for the definition
# of all needed environment variables.
#
# Version 1.3, 3.11.93 Andy Schuerr
#
# Parameterprofil:
#
#  Progres [ -u UIStyle ] [ -b YES/NO ] [ -s SwapSpace ] [ -t TmpDir ] [ Project ]
#
usage="Call: $0 [-u UIStyle] [ -b YES/NO ] [-s SwapSpace] [-t TmpDir] [Project]"
#
# 
# Variables optionally influenced by parameters
# ---------------------------------------------
#
#   PROJECT = Project parameter = name of a project.
#             ( default:  "Demo" )
#   UISTYLE = UIStyle parameter = user interface style.
#             ( default:  smotif = extended motif style
#                                  (for grey scale monitors)
#               others:  monochrome = motif style for monochrome monitor,
#                        motif      = motif style,
#                        openlook   = openlook style )
#    
#   GRASALLOC = SwapSpace parameter = swap space for the underlying DBMS.
#               ( default:  2000 kB )
#   TMPDIR    = TmpDir parameter = name of a temporarily existing directory for
#               the underlying DBMS.
#               ( default:  /var/tmp/$USER$PROJECT )
#
#
#
#----------- Defaults for script parameters -----------------
#
# Name of the current project subdirectory
# Default value = "Demo"

PROJECT=Demo
export PROJECT

# User interface style definition.
# Default value = monochrome i.e. (for monochrome monitors)

UISTYLE=smotif

# Definition of the virtual memory size available for caching
# databases (in kB). An optimal value is larger than the size
# of all concurrently open documents (graphs) and must be
# smaller than the workstation's available swap space.
# Default value = 2000

GRASALLOC=2000
export GRASALLOC


# Determine which operating system version the host has
osver=`uname -a`
case $osver in
SunOS*5.*)    # Solaris 2.x
        PLATFORM=sol2
        OSVERSION=SOL2
        OSDYNLIBEXT=so;;
SunOS*4.1.*)  # SunOS 4.1.x
        echo "SunOS4.1.x machines are no longer supported."
        echo "Please rlogin on a machine running under Solaris2.x."
        exit 1
        PLATFORM=sunos4
        OSVERSION=SUNOS4
        OSDYNLIBEXT=so.0.0;;
Linux*2.*)  # Linux 2.0.x
        PLATFORM=linuxlibc6
        OSVERSION=LINUXLIBC6
        OSDYNLIBEXT=so;;	
*)      echo 'unknown os version' ;;  
esac
export PLATFORM OSVERSION OSDYNLIBEXT

#
# Parse Command-Line Parameters
#
while getopts hu:b:s:t: arg
do
  case $arg in
    u) UISTYLE=$OPTARG;;
    b) PROGWITHBUTTONS=$OPTARG;;
    s) GRASALLOC=$OPTARG;;
    t) NewTmp=$OPTARG;;
    h|\?) echo $usage; exit 1;;
  esac
done  

PROGWITHBUTTONS=${PROGWITHBUTTONS:-YES}
export PROGWITHBUTTONS

#
# Get the Projectname
#
shift `expr $OPTIND - 1`
if [ $# -gt 1 ]
then
  echo "More than one project name !!"
  echo $usage
  exit 10
else
  PROJECT=${1:-$PROJECT}
fi  

#
# Name of a temporarily existing directory for DBMS (on the
# workstation's local disk whenever possible)
# Default value = "/var/tmp/$USER$PROJECT"
#
# ATTENTION: do not delete this directoy or its contents
#            after system crashes. The underlying DBMS
#            uses the log-files within this directory to
#            recover the most recently consistent state
#            of the project database. In rare cases, 
#            recovery may not work. Then delete both
#            the directories $TMPGRAS and $GRAS, create
#            a new project database, and recreate all
#            documents by parsing the files in $IPSENBACKUP
#            (see below).

TMPGRAS=${NewTmp:-/var/tmp}/$USER$PROJECT
export TMPGRAS

echo ""
echo ""
echo "Start ProgControl with:"
echo ""
echo "      PROJECT   = $PROJECT"
echo "      UISTYLE   = $UISTYLE"
echo "      GRASALLOC = $GRASALLOC"
echo "      TMPGRAS   = $TMPGRAS"
echo ""

if [ ! -d $TMPGRAS ]
then
  echo "Creating a new temporary directory $TMPGRAS ..."
  mkdir -p $TMPGRAS
fi
  
#
#----------- Installation specific variables --------------
#
#            PLEASE CHECK THE FOLLOWING LINES
#

# Definition of the PROGRES environment's root directory
# i.e. the root of the directory which contains all
# installed PROGRES files. $HOME or $HOME/progres might
# be a reasonable value if PROGRES has an own account
# or is a subdirectory of the current account.
 
PROGRESROOT=${PROGRESROOT:-/home/projects/progres}
export PROGRESROOT
 
# test whether PROGRESROOT exists and
# actually contains PROGRES-installation
# files.
 
if [ ! -f $PROGRESROOT/bin/$OSVERSION/ProgControl ]
then
  PROGRESROOT=$HOME/progres
  if [ ! -f $PROGRESROOT/bin/$OSVERSION/ProgControl ]
  then
    PROGRESROOT=/home/projects/progres
    if [ ! -f $PROGRESROOT/bin/$OSVERSION/ProgControl ]
    then
      echo "Variable PROGRESROOT has wrong value!"
      echo "Change default value in this script!"
      exit 1
    fi
  fi
fi

# definition of a postscript printer's name
# which will be used for printing graphs ...
 
PRINTER=${PRINTER:-postscript}
export PRINTER
 
GRASBROWSERPRINTER=$PRINTER
export GRASBROWSERPRINTER


# IMPORTLIB indicates where the library of the imported types and functions
# can be found. Note that this library is not os-independent.
IMPORTLIB=${IMPORTLIB:-$PROGRESROOT/import/lib/$OSVERSION/libImportedFunctions.$OSDYNLIBEXT}
export IMPORTLIB

# Path to the installation of Tcl/Tk
TK_LIBRARY=${TK_LIBRARY:-$PROGRESROOT/lib/$OSVERSION/tk}
TCL_LIBRARY=${TCL_LIBRARY:-$PROGRESROOT/lib/$OSVERSION/tcl}
export TK_LIBRARY TCL_LIBRARY

# Initialize additional environment variables

LD_LIBRARY_PATH="$LD_LIBRARY_PATH:$PROGRESROOT/lib/$OSVERSION:$PROGRESROOT/lib"
export LD_LIBRARY_PATH

. $PROGRESROOT/bin/InitEnvVars


#
#--------- The following lines should not be changed ------
#

# Preventing program halt in case of undefined
# environment variable DISPLAY (default value
# is local machine)

DISPLAY=${DISPLAY:-$HOST":0.0"}
export DISPLAY

# Definition of environment variables for the
# graph browser EDGE (university of Karlsruhe)
# with various extensions (dynamic linking of
# application specific procedures etc.)

# Definition of the browser's root directory

GRASBROWSERLOADPATH=${GRASBROWSERLOADPATH:-$PROGRESROOT/browser}
export GRASBROWSERLOADPATH

# Definition of a list of library directories
# used for dynamic linking purposes.

GRASBROWSERLIBPATH=${GRASBROWSERLIBPATH:-$PROGRESROOT}
export GRASBROWSERLIBPATH

# Definition of the directory where the foreign
# layout package hglayout is available

HGLAYOUT=$PROGRESROOT/bin/$OSVERSION
export HGLAYOUT

# Definition of a single directory, where node icons for
# our graph browser EDGE (browser) may be stored.
# Default icons are stored in $PROGRESROOT/browser/etc
# (list of directories not yet supported).
 
EDGE_ICON_PATH=${EDGE_ICON_PATH:-$DATAROOT/icons}
export EDGE_ICON_PATH

if [ ! -h $EDGE_ICON_PATH ]
then
  # link to directory doesn't exist
  if [ ! -d $EDGE_ICON_PATH ]
  then
    # directory doesn't exist either
    echo "Creating a link to the icon directory ..."
    if  [ -d $PROGRESROOT/browser/etc ]
    then
      ln -s $PROGRESROOT/browser/etc $EDGE_ICON_PATH
    fi
  fi
fi

PROGRES_ICONS=${PROGRES_ICONS:-$EDGE_ICON_PATH/ProgresIcons}
export PROGRES_ICONS

if [ ! -h $PROGRES_ICONS ]
then
  # link to directory doesn't exist
  if [ ! -d $PROGRES_ICONS ]
  then
    # directory doesn't exist either
    echo "Creating a link to the icon directory ..."
    if  [ -d $PROGRESROOT/browser/etc/ProgresIcons ]
    then
     ln -s $PROGRESROOT/browser/etc/ProgresIcons $PROGRES_ICONS
    fi
  fi
fi

# definition of the root directory for all project databases.

GRAS=$DATAROOT/gras/$OSVERSION
export GRAS

if [ ! -d $GRAS/Demo ]
then
  # directory doesn't exist
  if [ ! -d $GRAS ]
  then
    echo "Creating a new DBMS directory ..."
    mkdir -p $GRAS
  fi
  if [ $PROJECT = "Demo" -a -f $PROGRESROOT/data/gras/${OSVERSION}/Demo.C.gz ]
  then
    # copy example project
    echo "Uncompressing example project Demo ..."
    echo "(needs some time, please be patient)"
    cp  $PROGRESROOT/data/gras/${OSVERSION}/Demo.C.gz $GRAS
    $PROGRESROOT/bin/UncompressPool Demo
    rm $GRAS/Demo.C.gz
  fi
fi

# Definition of the directory containing all user specific
# environment setup files (determining the layout of menues,
# properties of windows etc). For further informations, see
# $IPSENTABLES/tables.README.

IPSENTABLES=$DATAROOT/tables
export IPSENTABLES

if [ ! -d $IPSENTABLES ]
then
  # directory doesn't exist
  echo "Creating default setup files ..." 
  cp -r $PROGRESROOT/data/tables $DATAROOT
  newrev="yes"
else
  # Maybe the user has old local tables from a former release. 
  # In that case these tables are backuped, a short message 
  # informs the user and the current version is installed
  
  # If head is not available at your site use cat instead. 
  headcmd=head 
  
  oldrevline=`$headcmd $IPSENTABLES/cmd.tab | grep '$Id: ' `
  
  newrevline=`$headcmd $PROGRESROOT/data/tables/cmd.tab | grep '$Id: ' `
  
  if [ "$oldrevline" != "$newrevline" ]
  then
    newrev="yes"
    echo "Installing new version of setup files."
    echo "This resets e.g. your changes to the command table."
    echo "You may restore your changes by carefully merging files in"
    echo "$IPSENTABLES"
    echo "${IPSENTABLES}.oldrev"
    echo " "
    rm -rf ${IPSENTABLES}.oldrev
    mv $IPSENTABLES ${IPSENTABLES}.oldrev
    cp -r $PROGRESROOT/data/tables $DATAROOT

    if [ ! -d $EDGE_ICON_PATH ]
    then
       echo "Redirecting  link of icon directory ..."
       rm $EDGE_ICON_PATH
       ln -s $PROGRESROOT/browser/etc $EDGE_ICON_PATH
    fi
  else
    newrev="no"
  fi
fi


# Definition of the directory containing all message texts.

IPSENMESSAGES=$PROGRESROOT/data/messages
export IPSENMESSAGES

# Definition of a directory for all backup files.
# This directory contains a text representation of
# all documents of the current project (including
# layout informations). In rare cases, when the
# system's recovery after crashes doesn't work,
# you may recreate all documents by parsing these
# backup text files.
#

IPSENBACKUP=$DATAROOT/backups/$PROJECT
export IPSENBACKUP

if [ ! -d $IPSENBACKUP ]
then
  # directory doesn't exist
  echo "Creating a new project backup directory ..."
  mkdir -p $IPSENBACKUP
fi

#
#-----------------------------------------------
#

# Preparing recovery after a (very hard) crash.

t=`echo $GRAS/$PROJECT/*.tmp`

set $t
Crash=$1
ControlledCrash=$TMPGRAS/.CrashFlag

if [ -f $Crash ]
then
  echo ""
  echo "***********************************************"
  echo "* Crash files found in database directory!!!  *"
  echo "* Error recovery will be initiated and should *"
  echo "* be successful if previously used temporary  *"
  echo "* database directory is still existent and    *"
  echo "* currently accessible (cf. error recovery    *"
  echo "* section of man/GUIDED_TOUR.tex).            *"
  echo "***********************************************"
  echo ""
fi

if [ -f $ControlledCrash ]
then
  rm $ControlledCrash
fi

#
# Startup Sequence for Ganimed
#

$PROGRESROOT/bin/StartGanimed

# if [ $newrev = "yes" ]
# then
  # more $PROGRESROOT/README-TOO
  # echo
# fi

echo "Dynamic linking ProgControl ..."

OldPWD=$PWD
cd $IPSENBACKUP
$PROGRESROOT/bin/$OSVERSION/ProgControl -$UISTYLE
cd $OldPWD

# Restarting the environment in recovery mode after
# a crash.

t=`echo $GRAS/$PROJECT/*.tmp`

set $t
Crash=$1

while [ -f $Crash -o -f $ControlledCrash ]
do

  if [ -f $ControlledCrash ] 
  then
    rm $ControlledCrash
  fi

  echo ""
  echo "*******************************************"
  echo "*                                         *"
  echo "*       P R O G R E S  -  C R A S H       *"
  echo "*                                         *"
  echo "*******************************************"
  echo ""
 
  if [ $PLATFORM = "sol2" ]
  then 
    echo "Trying to restart (y/n): \c"
  else
    echo -n "Trying to restart (y/n): "
  fi
  if read ok
  then 
    ok=${ok:-"y"}
    echo ""
    if [ ! $ok = "y" ]
    then
      exit 1
    fi
  else
    echo ""
    exit 1
  fi
       
  # Restart with recovery = y(es)

  OldPWD=$PWD
  cd $IPSENBACKUP
  $PROGRESROOT/bin/$OSVERSION/ProgControl -$UISTYLE
  cd $OldPWD

done


# Normal program termination.
# Removing empty directory TMPGRAS if ProgControl
# terminates without leaving recovery informations
# in TMPGRAS (including deletion of interpreter
# garbagge).

if [ -f $TMPGRAS/ToBePrettied ]
then
  rm $TMPGRAS/ToBePrettied
fi
if [ -f $TMPGRAS/IsPretty ]
then
  rm $TMPGRAS/IsPretty
fi

t=`echo $TMPGRAS/*`
 
set $t
Crash=$1
ControlledCrash=$TMPGRAS/.CrashFlag
 
if [ -f $Crash -o -f $ControlledCrash ]
then
  echo ""
  echo "ATTENTION: directory $TMPGRAS"
  echo "contains still temporary files."
  echo "These files may be necessary for recovery"
  echo "from previous system crash."
  echo ""
elif [ -d $TMPGRAS ]
then
  rmdir $TMPGRAS
fi
