#!/bin/sh
#
#Copyright 1991-2020 Mentor Graphics Corporation
#
#All Rights Reserved.
#
#THIS WORK CONTAINS TRADE SECRET AND PROPRIETARY INFORMATION WHICH IS THE PROPERTY OF 
#MENTOR GRAPHICS CORPORATION OR ITS LICENSORS AND IS SUBJECT TO LICENSE TERMS.
#

# MTI_VCO_MODE environment variable may be set to "32" or "64"
# to force selection of 32-bit or 64-bit platform directory for executables.
mode=${MTI_VCO_MODE:-""}

arg0="$0"
cmd=`basename "$arg0"`

uname=`uname`
utype=`uname -r`
umach=`uname -m`

case $uname in
  AIX)
    islink=-L
    ;;
  CYGWIN_NT* | MSYS_NT*)
    islink=-h
    ;;
  Win*)
    uname=CYGWIN_NT
    islink=-h
    ;;
  *)
    islink=-h
    ;;
esac

while [ $islink "$arg0" ] ; do
  x=`/bin/ls -ld "$arg0"`
  x=`expr "$x" : '.* \(.*\)'`
  case "$x" in
    /*) arg0="$x"                 ;;
    *)  arg0="`dirname ""$arg0""`/$x" ;;
  esac
done

dir=`dirname "$arg0"`

vco=${uname}${utype}

case $vco in
  Linux*)
	  # On Linux, default to 32 bit unless MTI_VCO_MODE is set
	  # except for Visualizer where we invoke 64-bit version by default
	  if [ "$mode" = "" ] ; then
		  if ( [ "$cmd" = "visualizer" -o "$cmd" = "vis" ]) \
		  && ( [ -x "$dir/linux_x86_64/visualizer" ] ) ; then
			  mode="64";
		  elif ( [ -x "$dir/linux/vsim" -o -x "$dir/linuxpe/vsim" ] ) \
		  &&   ( [ "$umach" != "aarch64" ] ); then
			  mode="32";
		  fi
	  fi

	  # If IA-64 or X86-64 architecture and machine-specific vsim is not
	  # installed but linux vsim is available, use linux platform.
	  # Otherwise use machine-specific platform.
	  #
	  if [ "$mode" != "32"  ]            \
      && (  [ "$umach" = "ia64" ]         \
		 || [ "$umach" = "x86_64" ]       \
         || [ "$umach" = "aarch64" ] )    \
      && (  [ "$mode" = "64" ]            \
         || [ -x "$dir/linux_$umach/vsim" ] \
         || [ ! -x "$dir/linux/vsim" ] ) ; then
		vco="linux_$umach"
      else
		vco="linux"
		if [ ! -x "$dir/$vco/vsim" -a -x "$dir/linuxpe/vsim" ]; then
			vco="linuxpe"
		fi
      fi
    ;;
  CYGWIN_NT* | MSYS_NT*)
    if [ -x "$dir/win32/vsim" ]; then
      vco="win32"
    elif [ -x "$dir/win32pe/vsim" ]; then
      vco="win32pe"
    fi
    if [ "$mode" =  ""  ] ||
	   [ "$mode" = "64" ] ; then
      sysostype="`systeminfo 2>&1 | grep -i 'System Type' | cut -f1 -d- | sed s@x@X@ | cut -f2 -dX`"
      if [ "$sysostype" == "64" ] ; then
        vco="win64"
      fi
    fi
    ;;
  *)
    vco="unknown"
    ;;
esac

doatenv=0

case $cmd in
  qhdir)
    cmd=vdir
    ;;
  qhmap)
    cmd=vmap
    ;;
  qvlcom)
    cmd=vlog
    ;;
  qhlib)
    cmd=vlib
    ;;
  qhsim)
    cmd=vsim
    ;;
  qhdel)
    cmd=vdel
    ;;
  qhmake)
    cmd=vmake
    ;;
  qvhcom)
    cmd=vcom
    ;;
  qhgencomp)
    cmd=vgencomp
    ;;
  vco)
    echo $vco
    exit
    ;;
  hdloffice)
    doatenv=1
    ;;
  hdlstudio)
    doatenv=1
    ;;
esac

vco="linuxloem"

# verify that we can find the command to execute.
if [ -x "$dir/$vco/$cmd" ] ; then
  arg0="$dir/$vco/$cmd"
else
  # if bin nolonger has symbolic links in it then
  # we need to look up and over
  f2="$dir/$vco/$cmd"
  if [ -x "$f2" ] ; then
    arg0="$f2"
  elif [ -x "$dir/${vco}pe/$cmd" ] ; then
    arg0="$dir/${vco}pe/$cmd"
  elif [ -x "$dir/${vco}le/$cmd" ] ; then
    arg0="$dir/${vco}le/$cmd"
  else
    # Last ditch effort.  Use original arg0 and try it!
    d2=`dirname "$0"`
    f2="$d2/$vco/$cmd"
    if [ -x $f2 ] ; then
      arg0=$f2
    else
      echo "Error: cannot find \"$dir/$vco/$cmd\""
      exit 1
    fi
  fi
fi

if [ $doatenv ]; then
    if [ -x "$dir/$vco/athdlenv" ] ; then
      #. $dir/$vco/athdlenv
        if [ -z "$LD_LIBRARY_PATH" ]; then
            export LD_LIBRARY_PATH="$dir/$vco"
        else
            export LD_LIBRARY_PATH="$dir/$vco:$LD_LIBRARY_PATH"
        fi
        #echo LD_LIBRARY_PATH=$LD_LIBRARY_PATH
    fi
fi

if [ -z "$*" ] ; then
  exec "$arg0"
else
  exec "$arg0" "$@"
fi
