#!/usr/bin/python
# Wrapper script to get proxy information out of gconf and launch
# things with it in the environment.
#
# Copyright 2007, Red Hat, Inc.
# Jeremy Katz <katzj@redhat.com>
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; version 2 only
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU Library General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.


import gconf
import os, sys
import subprocess

if len(sys.argv) < 2:
    print >> sys.stderr, "Need a command to run"
    sys.exit(1)

try:
    client = gconf.client_get_default()
except:
    client = None

if client and client.get_bool("/system/http_proxy/use_http_proxy"):
    host = client.get_string("/system/http_proxy/host")
    port = client.get_int("/system/http_proxy/port")
    if port == 0:  # default to something reasonable
        port = 8080
    # don't set the environment variable if using authentication as
    # that can lead to it being snooped
    if host and not client.get_bool("/system/http_proxy/use_authentication"):
        os.environ["http_proxy"] = "%s:%d" %(host, port)


if client and client.get_string("/system/proxy/ftp_host"):
    host = client.get_string("/system/proxy/ftp_host")
    port = client.get_int("/system/proxy/ftp_port")
    if port == 0:
        os.environ["ftp_proxy"] = "%s" %(host,)
    else:
        os.environ["ftp_proxy"] = "%s:%d" %(host, port)

rc = subprocess.call(sys.argv[1:])
sys.exit(rc)