#!/bin/sh

jgraspv_jgrasp_home=$JGRASP_HOME
jgraspv_jgrasp_java=$JGRASP_JAVA
jgraspv_jgrasp_classpath=$JGRASP_CLASSPATH

# Process the arguments.
# Each flag can override an environment variable.
graspv_v=
for graspv_v2 in $@; do
   if [ x$graspv_v = "x-j" ]; then
      jgraspv_jgrasp_home=$graspv_v2
   elif [ x$graspv_v = "x-a" ]; then
      jgraspv_jgrasp_java=$graspv_v2
   elif [ x$graspv_v = "x-cp" ]; then
      jgraspv_jgrasp_classpath=$graspv_v2
   fi
   graspv_v=$graspv_v2
done

# Get OS and processor type using uname.
OS=`uname -s`
PROC=`uname -m`
case "$PROC" in
   i[3-7]86) PROC=iX86;;
   ia64) PROC=iX86;;
   ia64_32) PROC=iX86;;
   amd64) PROC=iX86;;
   x86) PROC=iX86;;
   x86_64) PROC=iX86;;
   sun4*) PROC=sparc;;
esac

which_command="which"
if [ "$OS" != Darwin ]; then
   if eval type command > /dev/null 2>&1; then
      which_command="command -v"
   fi
fi


# Handle Sun uname -m stupidity.
if [ "$PROC" = sparc ]; then
   PROC=`uname -p`
   case "$PROC" in
      sparc*) PROC=sparc;;
   esac
fi

echo processor $PROC

# Find expr command.
expr_cmd="expr"
if [ -x "/usr/bin/expr" ]; then
	expr_cmd="/usr/bin/expr"
elif [ -x "/bin/expr" ]; then
	expr_cmd="/bin/expr"
fi

echo expr $expr_cmd

# Find jGRASP home directory and bin directory.
if [ "$jgraspv_jgrasp_home" ]; then
   jgraspv_jgrasp_home_dir="$jgraspv_jgrasp_home"
   jgraspv_jgrasp_bin_dir="$jgraspv_jgrasp_home/bin"
else
   # Follow links in execution path of this shell.
   jgraspv_prg=$0
   while [ -h "$jgraspv_prg" ]; do
      ls=`/bin/ls -ld "$jgraspv_prg"`
      # Link format in ls output is stuff-> target
      jgraspv_link=`$expr_cmd "$ls" : '.*-> \(.*\)$'`
      if $expr_cmd "$jgraspv_link" : '\/' > /dev/null 2>&1; then
         # Link is a full path.
         jgraspv_prg="$jgraspv_link"
      else
         # Link is relative to source path.
         jgraspv_prg=`dirname "$jgraspv_prg"`/$jgraspv_link
      fi
   done

   jgraspv_jgrasp_bin_dir=`dirname "$jgraspv_prg"`
   jgraspv_jgrasp_home_dir="$jgraspv_jgrasp_bin_dir/.."
fi

echo home $jgraspv_jgrasp_home_dir

# Look for "java"
# First try the same directory containing "javac"
if [ "x$jgraspv_jgrasp_java" = "x" ]; then
   if eval type javac > /dev/null 2>&1; then
      jgraspv_jgrasp_java=`$which_command javac`
      echo which_javac $jgraspv_jgrasp_java
      # Follow links in javac.
      while [ -h "$jgraspv_jgrasp_java" ]; do
         ls=`/bin/ls -ld "$jgraspv_jgrasp_java"`
         # Link format in ls output is stuff-> target
         link=`$expr_cmd "$ls" : '.*-> \(.*\)$'`
         if $expr_cmd "$link" : '\/' > /dev/null 2>&1; then
            # Link is a full path.
            jgraspv_jgrasp_java="$link"
            echo absolute_link $link
         else
            # Link is relative to source path.
            jgraspv_jgrasp_java=`dirname "$jgraspv_jgrasp_java"`/$link
            echo relative_link $link
         fi
      done
      echo which_javac_f $jgraspv_jgrasp_java
      jgraspv_jgrasp_java=`dirname "$jgraspv_jgrasp_java"`/java
      echo which_javac_d $jgraspv_jgrasp_java
      if [ -e "$jgraspv_jgrasp_java" ]; then
         echo exists;
      fi
      if [ ! -x "$jgraspv_jgrasp_java" ]; then
         jgraspv_jgrasp_java="";
         echo not_executable
      fi
   fi
   echo javac_home $jgraspv_jgrasp_java
fi

if [ "x$jgraspv_jgrasp_java" = "x" ]; then
   if eval type java > /dev/null 2>&1; then
      jgraspv_jgrasp_java=`$which_command java`
      # Follow links in java.
      while [ -h "$jgraspv_jgrasp_java" ]; do
         echo follow $jgraspv_jgrasp_java
         ls=`/bin/ls -ld "$jgraspv_jgrasp_java"`
         # Link format in ls output is stuff-> target
         link=`$expr_cmd "$ls" : '.*-> \(.*\)$'`
         if $expr_cmd "$link" : '\/' > /dev/null 2>&1; then
            # Link is a full path.
            jgraspv_jgrasp_java="$link"
            echo java_full $link
         else
            # Link is relative to source path.
            jgraspv_jgrasp_java=`dirname "$jgraspv_jgrasp_java"`/$link
            echo java_relative $link
         fi
      done
   fi
   echo java_home $jgraspv_jgrasp_java
fi


if [ -f "$jgraspv_jgrasp_home_dir/jgrasp.jar" ]; then
   jgraspv_classpath="jgrasp.jar"
else
   # For development only.
   jgraspv_classpath="../flgl:."
fi


# Look for tools path and add to classpath if found.
if [ "$jgraspv_jgrasp_classpath" ]; then
   jgraspv_classpath="$jgraspv_classpath:$jgraspv_jgrasp_classpath"
else
   if [ "$jgraspv_jgrasp_java" ]; then
      jgraspv_java_bin="$jgraspv_jgrasp_java"
   else
      if eval type java > /dev/null 2>&1; then
         jgraspv_java_bin=`$which_command java`
      fi
   fi

   echo search_tools_at $jgraspv_java_bin
   if [ "$jgraspv_java_bin" ]; then
      jgraspv_tools_jar=`dirname "$jgraspv_java_bin"`/../lib/tools.jar
      echo try_tools_at $jgraspv_tools_jar
      if [ -f "$jgraspv_tools_jar" ]; then
         jgraspv_classpath="$jgraspv_classpath:$jgraspv_tools_jar";
         echo found_tools
      else
         jgraspv_tools_jar=`dirname "$jgraspv_java_bin"`/../../lib/tools.jar
         echo try_tools2_at $jgraspv_tools_jar
         if [ -f "$jgraspv_tools_jar" ]; then
            jgraspv_classpath="$jgraspv_classpath:$jgraspv_tools_jar";
            echo found_tools2
         fi
      fi
   fi
fi


# Find subdirectory for current OS and processor.
# If not a delivered binary, run sys_jgrasp if it is present and executable.
# Run directly otherwise.
if [ "$OS" = Linux -a "$PROC" = iX86 ]; then
   jgraspv_prog="$jgraspv_jgrasp_bin_dir/linux/jgrasp"
elif [ "$OS" = SunOS -a "$PROC" = sparc ]; then
   jgraspv_prog="$jgraspv_jgrasp_bin_dir/solaris/jgrasp"
elif [ "$OS" = Darwin ]; then
   contents=`$expr_cmd "$jgraspv_jgrasp_bin_dir" : "\(..*\)\/Resources/jgrasp/bin"`
   if [ -x "$contents/MacOS/jcs" ]; then
      # Mac installation. 
      jgraspv_prog="$contents/MacOS/jcs"
   else
      # Zip installation.
      jgraspv_prog="$jgraspv_jgrasp_bin_dir/osx104/jcs.app/Contents/MacOS/jcs"
   fi
elif [ -x "$jgraspv_jgrasp_bin_dir/sys_jgrasp" ]; then
   jgraspv_prog="$jgraspv_jgrasp_bin_dir/sys_jgrasp"
else
   jgraspv_cmd=java
   if [ "$jgraspv_jgrasp_java" ]; then
      jgraspv_cmd=$jgraspv_jgrasp_java
   fi
   jgraspv_cmd_tmp="$jgraspv_cmd -cp $jgraspv_classpath Grasp"
   echo We do not have a native shell for system $OS on processor $PROC.
   echo Running jGRASP using \"$jgraspv_cmd_tmp\".
   echo Compile and run commands will not be supported.
   echo Any filename argument will be ignored.
   cd $jgraspv_jgrasp_home_dir
	exec "$jgraspv_cmd" -cp "$jgraspv_classpath" Grasp
   exit 0
fi

echo prog $jgraspv_prog

if [ -x "$jgraspv_prog" ]; then
   exec "$jgraspv_prog" -Icp "$jgraspv_classpath" -Ij "$jgraspv_jgrasp_home_dir" $@ &
else
   if [ -f "$jgraspv_prog" ]; then
      echo >&2 "$jgraspv_prog is not executable. Check permissions."
   else
      echo >&2 "jgrasp not found at $jgraspv_prog as expected."
   fi
   exit 1
fi