#!/bin/sh  
# --------------------------------------------------------------------------
# Copyright 1992-1994 by Forschungszentrum Informatik (FZI)
#
# You can use and distribute this software under the terms of the license
# version 1 you should have received along with this software.
# If not or if you want additional information, write to
# Forschungszentrum Informatik, "STONE", Haid-und-Neu-Strasse 10-14,
# D-76131 Karlsruhe, Germany.
# --------------------------------------------------------------------------
# 'obst-check_cnt_version - 27:02:91 - Jochen Alt'
#
# obst-check_cnt_version [-s] <version> [<pid>]
#
# Checks the compatibility of the passed version with the version of the current
# container directory.
# Incompatibility results in an error code and a message. Otherwise, the
# return code will be 0.
#
# The '-s(ilent)' option supresses the output of error messages.
#
# Compatibility is determined using the table in the file
# obst-compatible-version. This file lists all
# compiler version/container version pairs that can go together.
#
# If a pid is passed, the corresponding process is killed (via a TERM signal)
# if the check fails.

  self='obst-check_cnt_version'
 usage="[-s] <OBST-version> [<pid>]"
do_msg='+'

[ "$1" = '-s' ] && { do_msg=; shift; }

if [ -n "$OBSTCONTAINER" ] ; then
  cnt="$OBSTCONTAINER"
elif [ -n "$SOSCONTAINER" ] ; then
  cnt="$SOSCONTAINER"
else
  [ "$do_msg" ] &&\
     echo >&2 "*** $self: environment variable OBSTCONTAINER not defined"
  exit 1
fi
if [ ! -d $cnt ]; then
   [ "$do_msg" ] && echo >&2 "*** $self: invalid container directory: $cnt"
   exit 1
fi

if [ $# -eq 0  -o  $# -gt 2 ] ; then
   [ "$do_msg" ] && echo >&2 "*** usage: $self $usage"
   exit 1 
fi

prg_version="$1"
pid="$2"

if [ -f $cnt/version ]; then
   full_cnt_version="`cat $cnt/version | grep -i version`"
elif [ "$SOSC_BOOTING" ]; then
   full_cnt_version="$prg_version"
else
   full_cnt_version="unknown version"
fi

# remove the compiler path to get a handy version string
cnt_version="`echo $full_cnt_version |  sed 's/^.*://'`"

[ "$cnt_version" = "$prg_version" ] || {
   # get the file with the compatible pairs
   compatible_file="`dirname $0`/obst-compatible_versions"

   grep -s "$prg_version/.*$cnt_version" $compatible_file >/dev/null || {
      [ "$do_msg" ] && {
         echo >&2 "Cnt-Path     : $cnt"
         echo >&2 "OBST-Version : $prg_version"
         echo >&2 "Cnt-Version  : $cnt_version"
         echo >&2 "Check        : incompatible versions"
      }
      [ "$pid" ] && {
         kill -TERM $pid || {
	   [ "$do_msg" ] && echo >&2 "$self: 'kill -TERM $pid' failed"
	   exit 3
         }
      }
      exit 2
   }
}
exit 0
