#!/bin/sh
TOOL_PREFIX="gasnet"
export TOOL_PREFIX
VERSION="1.26.3"
export VERSION
# other/perlstart.  Generated from perlstart.in by configure.
#   $Source: bitbucket.org:berkeleylab/gasnet.git/other/perlstart.in $
# Description: 
# Copyright 2004,  Dan Bonachea <bonachea@cs.berkeley.edu>
# Terms of use are as specified in license.txt
#
# Purpose of this utility:
#
# Perlstart does the "right thing" to start a sane perl environment on any
# machine, which involves finding the right configure-detected perl interpreter
# (or an appropriate substitute), resolving all symlinks to the perl script
# itself (so that $0 is a canonical absolute path), and occasionally passing
# special flags to workaround bugs in perl. It also allows passing
# configure-detected values through the environment to the perl script.
# Finally, it does all this while still allowing the *.pl file to remain a
# completely conventional perl script that can be directly executed (on "sane"
# systems) for script debugging and development purposes.
#
# perlstart usage info: 
#
# create a script.in file with the following contents (minus leading #'s)
#      #!/bin/sh
#      SOME_VAR_YOUR_SCRIPT_NEEDS=@SOME_VAR_YOUR_SCRIPT_NEEDS@
#      export SOME_VAR_YOUR_SCRIPT_NEEDS
#      # any options you want passsed to the perl invocation
#      PERLSTART_FLAGS="-Mstrict"
#      @PERL START@     (remove the embedded space and this comment)
# be sure to place your script file in configure.in AC_OUTPUT and GASNET_FIX_EXEC
# also be sure to add targets in Makefile to copy the .pl file when builddir!=srcdir

scriptname=$0

perl='/usr/bin/perl'
perlopts='-Mbytes'
readlink='/usr/bin/readlink'
dirname='/usr/bin/dirname'
basename='/usr/bin/basename'
pwd_prog='/bin/pwd'
awk='awk'

# written carefully to still work if readlink is missing or broken
if test -x "$readlink" -a "`$readlink / 2>&1 > /dev/null`" = ""; then
  link=`$readlink ${scriptname}`
else
  readlink=
  if test ! -x "$awk" -o "`$awk '{}' /dev/null 2>&1`" != ""; then
    awk=awk
  fi
  link=`/bin/ls -al ${scriptname} | $awk 'BEGIN{FS=">"}{split($2,A," ") ; print A[1]}'`
fi
# fall back on versions in PATH if our hard-coded ones disappear
# carefully detect stderr output
if test ! -x "$dirname" -o "`dirname foo/bar 2>&1`" != "foo" ; then
  dirname=dirname
fi
if test ! -x "$basename" -o "`basename foo/bar 2>&1`" != "bar" ; then
  basename=basename
fi
if test ! -x "$pwd_prog" -o "`$pwd_prog 2>&1 > /dev/null`" != "" ; then
  pwd_prog=pwd
fi
if test ! -x "$perl" -o "`$perl -v 2>&1 > /dev/null`" != "" ; then
  perl=perl
  perlopts=
fi

# be sure to follow any symbolic links to reach the location
# where this script truly resides. 
while  test "$link" && \
       "$dirname"  ${scriptname} > /dev/null 2>&1 && \
       "$basename" ${scriptname} > /dev/null 2>&1
  do
  dir=`"$dirname" ${scriptname}`;
  newpath="$link";
  newdir=`"$dirname" ${newpath}`;
  newname=`"$basename" ${newpath}`;
  dir=`cd ${dir} >/dev/null 2>&1; cd ${newdir} >/dev/null 2>&1; $pwd_prog`;
  scriptname="${dir}/${newname}";

  oldlink="$link"
  if test -x "$readlink" ; then
    link=`$readlink ${scriptname}`
  else
    link=`/bin/ls -al ${scriptname} | $awk 'BEGIN{FS=">"}{split($2,A," ") ; print A[1]}'`
  fi
  # readlink on the SGI Origin returns name of non-symlink, instead of blank, so
  # make sure a 'link' resolves to something other than itself
  if test x"$link" = x"$oldlink"; then
    link=
  fi
done

# be sure we always use a full path
dir=`"$dirname" ${scriptname}`;
scriptname=`cd ${dir} >/dev/null 2>&1; $pwd_prog`/`"$basename" ${scriptname}`;

PERLSTART_FLAGS="$perlopts $PERLSTART_FLAGS"
export PERLSTART_FLAGS

# bug 687/2475: perl's UTF support implicitly does some very stupid things, 
# esp when manipulating binary strings -- so we force it all to be turned off
LC_ALL=C
export LC_ALL
LANG=en_US
export LANG

exec $perl $PERLSTART_FLAGS "${scriptname}.pl" "$@"

echo "${scriptname}: exec failed." >&2
exit 1
