#!/usr/bin/env bash
# 
# Copyright (c) 2008-2011 VMware, Inc.  All rights reserved. 
# Permission is hereby granted, free of charge, to any person obtaining a copy
# of this software and associated documentation files (the "Software"), to deal
# in the Software without restriction, including without limitation the rights
# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
# copies of the Software, and to permit persons to whom the Software is
# furnished to do so, subject to the following conditions:
# 
# The above copyright notice and this permission notice shall be included in
# all copies or substantial portions of the Software.
# 
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
# THE SOFTWARE.
# 

# 
#
export TEXTDOMAINDIR=/opt/vmware/lib/locale
export TEXTDOMAIN=vami_proxy

ENV_FILE=/etc/environment
SYSCFG_PROXY="/etc/sysconfig/proxy"
proxy_server=
proxy_port=

proxy_server()
{
    if [ -f "$SYSCFG_PROXY" ]; then

       # proxy url as http://usr:pw@server:port
       proxy_server=`sed -n  's,^HTTP_PROXY=.*http://\([^:][^:]*\):\([^:][^:]*\)@\([^:][^:]*\):\([0-9][0-9]*\).*,\3,p' $SYSCFG_PROXY`
       if [ "$proxy_server" = "" ]; then
          # proxy url as http://server:port
          proxy_server=`sed -n 's,^HTTP_PROXY=.*http://\([^:][^:]*\):\([0-9][0-9]*\).*,\1,p' $SYSCFG_PROXY`
       fi

    fi
    if [ "$proxy_server" = "" -a -f "$ENV_FILE" ]; then

       # proxy url as http://usr:pw@server:port
       proxy_server=`sed -n  's,^http_proxy=http://\([^:][^:]*\):\([^:][^:]*\)@\([^:][^:]*\):\([0-9][0-9]*\)$,\3,p' $ENV_FILE`
       if [ "$proxy_server" = "" ]; then
          # proxy url as http://server:port
          proxy_server=`sed -n 's,^http_proxy=http://\([^:][^:]*\):\([0-9][0-9]*\)$,\1,p' $ENV_FILE`
       fi

    fi

    echo $proxy_server
}


proxy_port()
{
    if [ -f "$SYSCFG_PROXY" ]; then
       proxy_port=`sed -n 's,^HTTP_PROXY=.*http://.*:\([0-9][0-9]*\).*,\1,p' $SYSCFG_PROXY`
    fi
    if [ "$proxy_port" = "" -a -f "$ENV_FILE" ]; then
       proxy_port=`sed -n 's,^http_proxy=http://.*:\([0-9][0-9]*\)$,\1,p' $ENV_FILE`
    fi

    echo $proxy_port
}


case "`basename $0`" in
    "vami_proxy_server")
        proxy_server
        ;;
    "vami_proxy_port")
        proxy_port
        ;;
    "vami_proxy")
        P=`proxy_server`
        if [ "$P" != "" ]
        then
            echo $P:`proxy_port`
        fi    
        ;;
    *)
        echo `gettext "Unknown program name."`
        echo
        echo `gettext "Usage:"` '[vami_proxy|vami_proxy_server|vami_proxy_port]'
        exit 1
        ;;
esac    
exit 0
