COBSJE_USAGE="N"
COBSJE_HELP="N"
COBSJE_J64="N"
COBSJE_NOEXE=""
COBSJE_VERBOSE="N"
COBSJE_VERS=""
COBSJE_VCOMP=""
JAVA_HOME=${JAVA_HOME:-}
COBSJE_JH="$JAVA_HOME"
COBSJE_ECHOING=0
COBSJE_EXECUTING=1
COBDIR=${COBDIR:-}
COBCPY=${COBCPY:-}
CLASSPATH=${CLASSPATH:-}
cobrun >/dev/null 2>&1
if test $? -ne 0 -o -z "$COBDIR"
then
	echo "cobsje: First set up your environment for COBOL"
	return 2
fi
	COBSJE_J64="Y"
while test $# -gt 0
do
        case $1 in
        -n) COBSJE_NOEXE="Y"
            shift
            ;;
        -v) COBSJE_VERBOSE="Y"
            shift
            ;;
        -J) shift
	    if test $# -gt 0
	    then
            	COBSJE_JH=$1
            	shift
	    fi
            ;;
	-help)
	   COBSJE_HELP="Y"
	   break
	   ;;
        *) COBSJE_USAGE="Y" 
	   break
           ;;
        esac
done
if test -z "$COBSJE_JH" -a -z "$JAVA_HOME" -o "$COBSJE_NOEXE" = "Y" -a "$COBSJE_VERBOSE" = "Y"
then
        COBSJE_USAGE="Y"
fi
if test "$COBSJE_NOEXE" = "Y" -o "$COBSJE_VERBOSE" = "Y"
then
  COBSJE_ECHOING=1
fi
if test "$COBSJE_NOEXE" = "Y"
then
  COBSJE_EXECUTING=0
fi
if test "$COBSJE_USAGE" = "Y" -o "$COBSJE_HELP" = "Y"
then
	echo "Usage: . \$COBDIR/bin/cobsje [-n|-v] [-J <Java_home>]"
	if test "$COBSJE_HELP" = "Y"
	then
		echo ""
		echo "This script adds Java support to the COBOL environment."
		echo ""
		echo "With no arguments cobsje will attempt to use \$JAVA_HOME as Java_home."
		echo ""
		echo "-n     do not change the environment settings but display what they would"
		echo "       have been set to"
		echo "-v     verbose mode: apply the changes and display the new settings"
		echo "-J     Java_home is the full path name of the directory where Java has been"
		echo "       installed. If this is a valid path name, \$JAVA_HOME is set to this"
		echo "       value."
		echo "-?     any invalid flag will cause the Usage message to be displayed"
		return 0
	else
		echo "Use: \". \$COBDIR/bin/cobsje -help\" for details."
		return 1
	fi
fi
if test -z "$COBSJE_JH"
then
	echo "cobsje: Java home directory not specified"
	return 1
fi
if test ! -x $COBSJE_JH/bin/java
then
	echo "cobsje: Cannot execute $COBSJE_JH/bin/java"
	return 2
fi
COBSJE_VERS=`CLASSPATH=$COBDIR/lib $COBSJE_JH/bin/java WhatJavaVersion  | cut -d'.' -f1-3 | cut -d'_' -f1`
COBSJE_VCOMP=`echo $COBSJE_VERS | sed -e "s+\.++g"`
if test "$COBSJE_J64" = "Y"
then
	JAVA_VERSIONS="1.4.2 1.5.0"
	JAVA_BITS="64-bit"
else
	JAVA_VERSIONS=""
	JAVA_BITS="32-bit"
fi
echo " " $JAVA_VERSIONS " " | grep " $COBSJE_VERS " >/dev/null 2>&1
if test ! $? -eq 0
then
  if test -z "$JAVA_VERSIONS"
  then
    JAVA_VERSIONS="none"
  fi
  echo "cobsje: Supported $JAVA_BITS versions of Java are: " $JAVA_VERSIONS
  return 2
fi
if test "$COBSJE_J64" = "Y"
then
  if test $COBSJE_ECHOING -eq 1
  then
    echo COBJVM="ibm"_${COBSJE_VCOMP}_64
  fi
  if test $COBSJE_EXECUTING -eq 1
  then
    COBJVM="ibm"_${COBSJE_VCOMP}_64
  fi
else
  if test $COBSJE_ECHOING -eq 1
  then
    echo COBJVM="ibm"_$COBSJE_VCOMP
  fi
  if test $COBSJE_EXECUTING -eq 1
  then
    COBJVM="ibm"_$COBSJE_VCOMP
  fi
fi
if test $COBSJE_ECHOING -eq 1
then
  echo JAVA_HOME=$COBSJE_JH
fi
if test $COBSJE_EXECUTING -eq 1
then
  JAVA_HOME=$COBSJE_JH
fi
if test $COBSJE_EXECUTING -eq 1
then
  export JAVA_HOME COBJVM
fi
if test $COBSJE_ECHOING -eq 1
then
  echo LD_LIBRARY_PATH=$COBSJE_JH/jre/bin:$COBSJE_JH/jre/bin/classic:$LD_LIBRARY_PATH
  echo PATH=$COBSJE_JH/jre/bin:$COBSJE_JH/bin:$PATH
fi
if test $COBSJE_EXECUTING -eq 1
then
  LD_LIBRARY_PATH=$COBSJE_JH/jre/bin:$COBSJE_JH/jre/bin/classic:$LD_LIBRARY_PATH
  PATH=$COBSJE_JH/jre/bin:$COBSJE_JH/bin:$PATH
  export LD_LIBRARY_PATH PATH 
fi

if test $COBSJE_ECHOING -eq 1
then
  echo CLASSPATH=$COBDIR/lib/mfcobol.jar:.:$COBSJE_JH/jre/lib/rt.jar:$COBDIR/lib/mfimtk.jar:$COBDIR/lib/xerces.jar:$COBDIR/lib/castor-0_9_4_1-xml.jar:$CLASSPATH
  echo COBCPY=$COBDIR/cpylib:$COBCPY
fi
if test $COBSJE_EXECUTING -eq 1
then
  CLASSPATH=$COBDIR/lib/mfcobol.jar:.:$COBSJE_JH/jre/lib/rt.jar:$COBDIR/lib/mfimtk.jar:$COBDIR/lib/xerces.jar:$COBDIR/lib/castor-0_9_4_1-xml.jar:$CLASSPATH
  COBCPY=$COBDIR/cpylib:$COBCPY
fi

if test $COBSJE_EXECUTING -eq 1
then
  COBSJE_OLDDIR=`pwd`
  export CLASSPATH COBCPY
  cd $COBDIR/lib
  cobjrun WhatJava
  cd $COBSJE_OLDDIR >/dev/null 
fi
