#!/www/python/bin/python

"""$URL: svn+ssh://svn.mems-exchange.org/repos/trunk/dulcinea/bin/start-apache.py $
$Id: start-apache.py 22952 2003-11-03 00:14:26Z dbinger $

Script invoked by site command to start apache.
"""

import os
from dulcinea import site_util

config = site_util.get_config()
sites_directory = config.defaults().get('sites-directory')

var_directory = config.defaults().get('var-directory')
httpd_conf_name = os.path.join(var_directory, 'generated-httpd.conf')

httpd_conf = open(httpd_conf_name, 'w')

def w (*s):
    httpd_conf.write(' '.join(map(str, s)) + '\n')

def include_site_conf (include_file, site):
    file_name = os.path.join(sites_directory,
                             site,
                             'conf',
                             include_file)
    if os.path.exists(file_name):
        w('Include', file_name)

def include_global_conf (conf_file_name):
    full_name = os.path.join(config.defaults().get('conf-directory'),
                             'apache',
                             conf_file_name)
    if os.path.exists(full_name):
        w('Include', full_name)

def common (site):
    servername = config.get(site, 'servername')
    if servername is None:
        servername = site_util.get_hostname()
    w('ServerName %s' % servername)
    scgi_address = config.get(site, 'scgi-address')
    if scgi_address:
        exports = list(site_util.get_root_exports(site))
        exports.sort()
        if '_q_index' in exports: # dynamic root
            exports.append('$')
        w('<LocationMatch "^/(%s)($|/.*)">' % '|'.join(exports))
        w('    SCGIServer %s %s' % site_util.parse_address(scgi_address))
        w('    SCGIHandler On')
        w('</LocationMatch>')
    include_site_conf('apache-common.conf', site)

w('# Generated. Edit site.conf and/or start_apache.py to change.')
w('ServerAdmin', site_util.get_administrator_address()[0])
w('Port', 80)
w('User', config.defaults().get('daemon-uid'))
w('PidFile', site_util.get_pid_file_name('apache'))

is_live = site_util.any_live_sites()

ports = {}
for site in site_util.list_sites():
    for option in ('http-address', 'https-address'):
        if config.has_option(site, option):
            ip, port = site_util.parse_address(config.get(site, option))
            ports[port]=1
for port in ports:
    w('Listen', port)


if not is_live:
    # Directives if not live for any site.
    w('CoreDumpDirectory', config.defaults().get('var-directory'))
    w('LogLevel debug')

include_global_conf('apache-global.conf')

for site in site_util.list_sites():
    if not config.has_option(site, 'http-address'):
        continue
    if (is_live and not site_util.is_live(site)):
        # In live mode, skip sections that aren't live 
        continue

    docroot = '/www/sites/%s/docroot' % site
    if not os.path.exists(docroot):
        continue
    host_iface = config.get(site, 'http-address')
    host_ssl_iface = config.get(site, 'https-address')

    w('\n<VirtualHost %s>' % host_iface, '# %s http' % site)
    w('DocumentRoot ' + docroot)
    common(site)
    include_site_conf('apache-http.conf', site)
    w('</VirtualHost>')
    if host_ssl_iface:
        w('\n<VirtualHost %s>' % host_ssl_iface,
          '# %s https' % site)
        w('DocumentRoot ' + docroot)
        common(site)
        w('SSLEngine on')
        w('SSLOptions +StdEnvVars')
        ssl_cert = config.get(site, 'ssl-certificate')
        ssl_cert_key = ssl_cert.replace('.crt', '.key')
        w('SSLCertificateFile %s' % ssl_cert)
        w('SSLCertificateKeyFile %s' % ssl_cert_key)
        w('</VirtualHost>')
    if site_util.is_live(site):
        include_site_conf('apache-live.conf', site)

httpd_conf.close()

httpd = config.defaults().get('httpd')
os.execv(httpd, [httpd, '-f', httpd_conf_name])

