#!/usr/bin/env python
# $FreeBSD: ports/Tools/portbuild/scripts/buildproxy-client,v 1.3 2010/06/25 23:02:09 linimon Exp $
#
# Client for communicating proxy requests to the buildproxy

import sys, socket, os, commands

from freebsd import *
from freebsd_config import *

CONFIG_DIR="/var/portbuild"
CONFIG_SUBDIR="conf"
CONFIG_FILENAME="server.conf"

config = getConfig( CONFIG_DIR, CONFIG_SUBDIR, CONFIG_FILENAME )
BUILDPROXY_SOCKET_FILE = config.get( 'BUILDPROXY_SOCKET_FILE' )

try:
    s = socket.socket(socket.AF_UNIX, socket.SOCK_STREAM)
    s.connect(BUILDPROXY_SOCKET_FILE)

    sockfile = s.makefile()
    sockfile.write("%s\n" % " ".join(sys.argv[1:]))
    sockfile.flush()
    code = sockfile.readline().strip()
    out = "".join(sockfile.readlines())

    if out:
        print out

    sockfile.close()
    s.close()

    sys.exit(int(code))
except Exception, e:
    print "buildproxy-client: exception:"
    print e
    try:
        if code == None:
            print "buildproxy-client: error: code was None"
        else:
            print "buildproxy-client: error: code was '" + code + "'"
    except Exception, e2:
        print "buildproxy-client: exception 2:"
        print e2
    raise e  # XXX debug
    sys.exit(254)
