#!/usr/bin/env python
"""
  Very simple client script used to get nearest squid server using the RESTful API.
"""

import urllib2
import sys
import json
import logging
from shoal_client import config as config

from urllib2 import urlopen

config.setup()
url = config.shoal_server_url
cvmfs_config = config.cvmfs_config
default_conf = config.default_config_format

try:
    f = urlopen(url)
    data = f.read()
except urllib2.URLError as e:
    logging.error("Unable to open url. %s" % e)
    sys.exit(1)

closest_http_proxy = ''
cvmfs_http_proxy = "\nCVMFS_HTTP_PROXY="
default_http_proxy = config.default_squid_proxy

if data:
    data = json.loads(data)
    for i in data:
        closest_http_proxy += ('http://{};').format(data[i]['public_ip'])

if closest_http_proxy:
    cvmfs_http_proxy += "\"" + closest_http_proxy + "\""
else:
    cvmfs_http_proxy += default_http_proxy

default_conf += cvmfs_http_proxy
with open(cvmfs_config, "w") as f:
    f.write(default_conf)
