#!/bin/sh
#
# DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
#
# Copyright (c) 2009-2010 Oracle and/or its affiliates. All rights reserved.
#
# The contents of this file are subject to the terms of either the GNU
# General Public License Version 2 only ("GPL") or the Common Development
# and Distribution License("CDDL") (collectively, the "License").  You
# may not use this file except in compliance with the License.  You can
# obtain a copy of the License at
# https://glassfish.dev.java.net/public/CDDL+GPL_1_1.html
# or packager/legal/LICENSE.txt.  See the License for the specific
# language governing permissions and limitations under the License.
#
# When distributing the software, include this License Header Notice in each
# file and include the License file at glassfish/bootstrap/legal/LICENSE.txt.
#
# GPL Classpath Exception:
# Oracle designates this particular file as subject to the "Classpath"
# exception as provided by Oracle in the GPL Version 2 section of the License
# file that accompanied this code.
#
# Modifications:
# If applicable, add the following below the License Header, with the fields
# enclosed by brackets [] replaced by your own identifying information:
# "Portions Copyright [year] [name of copyright owner]"
#
# Contributor(s):
# If you wish your version of this file to be governed by only the CDDL or
# only the GPL Version 2, indicate your decision by adding "[Contributor]
# elects to include this software in this distribution under the [CDDL or GPL
# Version 2] license."  If you don't indicate a single choice of license, a
# recipient has the option to distribute your version of this file under
# either the CDDL, the GPL Version 2 or to extend the choice of license to
# its licensees as provided above.  However, if you add GPL Version 2 code
# and therefore, elected the GPL Version 2 license, then the option applies
# only if the new code is made subject to such option by the copyright
# holder.
#

#
# Startup wrapper for pkg
#

# Resolve a symbolic link to the true file location
resolve_symlink () {
    file="$1"
    while [ -h "$file" ]; do
        ls=`ls -ld "$file"`
        link=`expr "$ls" : '^.*-> \(.*\)$' 2>/dev/null`
        if expr "$link" : '^/' 2> /dev/null >/dev/null; then
            file="$link"
        else
            file=`dirname "$1"`"/$link"
        fi
    done
    echo "$file"
}

# Take a relative path and make it absolute. Pwd -P will
# resolve any symlinks in the path
make_absolute () {
    save_pwd=`pwd`
    cd "$1";
    full_path=`pwd -P`
    cd "$save_pwd"
    echo "$full_path"
}

# Gets proxy information from Updatetool configuration file and sets
# the "http_proxy" environment variable for urllib in python
set_proxy () {
    os_name=`uname -s`
    if [ $os_name = "Darwin" ]; then
        UC_CONFIG_FILE="$HOME/Library/Application Support/updatetool/defaults.cfg"
    else
        UC_CONFIG_FILE="$HOME/.updatetool/defaults.cfg"
    fi

    if [ ! -r "$UC_CONFIG_FILE" ]; then
        return
    fi

    # If http_proxy is already set don't mess with it
    if [ ! -z "$http_proxy" ]; then
        return
    fi

    # Loop through the proxy properties in the config file
    # Remove spaces from line
    for l in `cat "$UC_CONFIG_FILE" | grep "proxy" | grep "=" | tr -d " "`; do

        # Get the property name and value. For the prop names map "." to "_"
        prop=`echo $l | cut -f 1 -d "=" | tr "." "_"`
        valu=`echo $l | cut -f 2 -d "=" `

        # Set the property in our environment
        if [ ! -z $prop ]; then
            eval `echo $prop`='"$valu"'
        fi
    done

    # Fold values for boolean properties to lower case.
    proxy_required=`echo $proxy_required | tr "[A-Z]" "[a-z]"`
        proxy_auth=`echo $proxy_auth     | tr "[A-Z]" "[a-z]"`

    # Set the "http_proxy" environment variable if proxies are set
    if [ ! -z "$proxy_required" -a "$proxy_required" = "true" ]; then
        if [ ! -z "$proxy_auth" -a "$proxy_auth" = "true" ]; then
            http_proxy="http://$proxy_username:$proxy_password@$proxy_host:$proxy_port"
        else
            http_proxy="http://$proxy_host:$proxy_port"
        fi
        export http_proxy
    fi
}


# Since we always use the bundled python runtime, make sure user's
# environment does not mess us up (bug 119)
unset PYTHONSTARTUP
unset PYTHONHOME
unset PYTHONPATH

# Find out where we are installed
cmd=`resolve_symlink "$0"`
my_home_relative=`/usr/bin/dirname "$cmd"`
my_home=`make_absolute "$my_home_relative"`

set_proxy

# this can be removed once cURL is upgrade to 7.19.6
if [ -z "$no_proxy" ]; then
    # NOTE: NO_PROXY was observed to work on Mac while no_proxy was not
    if [ -z "$NO_PROXY" ]; then
        no_proxy="localhost,127.0.0.0/8"
        NO_PROXY="localhost,127.0.0.0/8"
        export no_proxy
        export NO_PROXY
    else
        no_proxy="$NO_PROXY"
        export no_proxy
    fi
fi
NO_PROXY="$no_proxy"
export NO_PROXY

os_name=`uname -s`
if [ $os_name = "Darwin" ]; then
    # Set DYLD_LIBRARY_PATH so we can find libraries that python
    # modules depend on such as libcurl.
    PYTHONHOME=$my_python
    export PYTHONHOME
    DYLD_LIBRARY_PATH=$PYTHONHOME/lib
    export DYLD_LIBRARY_PATH
else
    # Hack to find bundled openssl libraries on Solaris
    unset LD_LIBRARY_PATH_32
    unset LD_LIBRARY_PATH_64
    LD_LIBRARY_PATH=$my_python/lib
    export LD_LIBRARY_PATH

    if [ $os_name = "AIX" ]; then
        # Set LIBPATH on AIX (bug UPDATECENTER2-2184)
        LIBPATH=$LD_LIBRARY_PATH
        export LIBPATH
    fi
fi

PYTHONPATH="$my_home/../custom-lib" python "$my_home/client.py" "$@"
_status=$?

exit $_status