#!%(python_path)s
import urllib

import ssl
if hasattr(ssl, '_create_unverified_context'):
  ssl._create_default_https_context = ssl._create_unverified_context

def isSuccess(response):
  return 200 <= response.code < 300

base_url = %(base_url)r
response = urllib.urlopen(base_url + '/isERP5SitePresent')
if isSuccess(response) and response.read() == '':
  mysql_url = %(sql_connection_string)r
  response = urllib.urlopen(
    base_url + '/manage_addProduct/ERP5/manage_addERP5Site',
    data=urllib.urlencode({
      'id': %(site_id)r,
      'erp5_catalog_storage': 'erp5_mysql_innodb_catalog',
      'erp5_sql_connection_string': mysql_url,
      'cmf_activity_sql_connection_string': mysql_url,
    }),
  )
  if not isSuccess(response):
    raise ValueError('Failed creating site, status=%%i: %%s' %% (response.code, response.read()))
  print "ERP5 site created."
