#!/bin/bash

# Pixar tractor-blade start-up wrapper.
# Dec 2008-2014
#
# This convenience shell script launches tractor-blade using its
# own bundled python distribution in order to guarantee the minimum
# required python version features.  An alternate site-installed
# python could also be used if it provides at least the same features.
#
# Note: The environment inherited by applications that are launched BY
# tractor-blade on the farm can be defined in several ways.  The settings
# here in this script are used to get tractor-blade itself launched.
# The preferred approach for affecting launched apps is to specify one
# or more environment handlers in blade.config; these can be selected by
# platform and/or by incoming environment selection keys in job scripts.
# The base environment is inherited from the blade's own launch 
# environment, or it can passed into tractor-blade by calling this
# script with the additional argument:  --env=/your/saved.env
# The env file is easily created with "printenv > /your/saved.env"
# followed by any desired text editing of that file.
#
## ----------------------------------------------------------------- ##
# Copyright (c) 1986-2016 Pixar. All rights reserved.
#
# The information in this file (the "Software") is provided for the exclusive
# use of the software licensees of Pixar ("Licensees").  Licensees have the
# right to incorporate the Software into other products for use by other
# authorized software licensees of Pixar, without fee. Except as expressly
# permitted herein, the Software may not be disclosed to third parties, copied
# or duplicated in any form, in whole or in part, without the prior written
# permission of Pixar.
#
# The copyright notices in the Software and this entire statement, including the
# above license grant, this restriction and the following disclaimer, must be
# included in all copies of the Software, in whole or in part, and all permitted
# derivative works of the Software, unless such copies or derivative works are
# solely in the form of machine-executable object code generated by a source
# language processor.
#
# PIXAR DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE, INCLUDING ALL
# IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL PIXAR BE
# LIABLE FOR ANY SPECIAL, INDIRECT OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
# WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION
# OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN
# CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.  IN NO CASE WILL
# PIXAR'S TOTAL LIABILITY FOR ALL DAMAGES ARISING OUT OF OR IN CONNECTION WITH
# THE USE OR PERFORMANCE OF THIS SOFTWARE EXCEED $50.
#
# Pixar
# 1200 Park Ave
# Emeryville CA 94608
## ----------------------------------------------------------------- ##

#
# Path to the installed Tractor bin directory
#
bdir=`dirname "$0"`

#
# If TRBLADE_PYTHON_EXE is set, use it as the python for launching
# the blade code itself.  Otherwise use the bundled rmanpy.
#
if [ 0 == ${#TRBLADE_PYTHON_EXE} ]; then
    #
    # usual case, run with the supplied "rmanpy" python distribution
    #
    # The inbound PYTHONHOME may point to a python distribution which
    # is incompatible with tractor-blade, but which may be important
    # for site-written applications that tractor will LAUNCH. We unset
    # it here in order to get the blade launched with its own python,
    # but then pass an override parameter to the blade so that it will
    # restore the inbound setting to launched apps on the farm.  This is
    # just a handy special case of the full env manipulations discussed
    # above, use blade.config envhandlers to fully control the environment
    # seen by launched apps.
    #
    if [ -z "$PYTHONHOME" ]; then
        appPHOME="None"
    else
        appPHOME="$PYTHONHOME"
        unset PYTHONHOME
    fi

    #
    # Similarly, these env vars need to be protected, as they will get
    # overriden during the blade launch.  The blade code will restore
    # these values for apps that it launches.
    #
    if [ 0 != ${#DYLD_FRAMEWORK_PATH} ]; then
        dyld_framework_path=$DYLD_FRAMEWORK_PATH
    else
        dyld_framework_path="None"
    fi
    if [ 0 != ${#LD_LIBRARY_PATH} ]; then
        ld_library_path=$LD_LIBRARY_PATH
    else
        ld_library_path="None"
    fi

    exec "$bdir/rmanpy" -m tractor.apps.blade --pythonhome="$appPHOME" --dyld_framework_path="$dyld_framework_path" --ld_library_path="$ld_library_path" "$@" 

else
    #
    # Use the site-specified python, it will not have the blade modules
    # built into its site-packages area.
    #
    bmod="$bdir"/../lib/python2.7/site-packages/tractor/apps/blade
    blade="not_found"
    for p in "$bmod"/tractor-blade-*.pyz; do
        # keep the last one
        blade=$p
    done

    exec "$TRBLADE_PYTHON_EXE" $blade "$@" 

fi

