%echo '#!/usr/bin/env python3'

from os import environ
environ['ODOO_RC'] = '/etc/odoo/odoo.conf'

from odoo import registry, SUPERUSER_ID
from odoo.api import Environment


with registry('%%pg_client_database').cursor() as cr:
    ctx = Environment(cr, SUPERUSER_ID, {})["res.users"].context_get()
    env = Environment(cr, SUPERUSER_ID, ctx)
    # Company
    env.company.name = '%%odoo_company_name'
    env.company.street = '%%odoo_company_street'
    env.company.city = '%%odoo_company_city'
    env.company.zip = '%%odoo_company_zip'
    env.company.vat = '%%odoo_company_vat'
    env.company.company_registry = '%%odoo_company_registry'
    env.company.phone = '%%odoo_company_phone'
    env.company.mobile = '%%odoo_company_mobile'
    env.company.email = '%%odoo_company_email'
    env.company.website = '%%odoo_company_website'
    env.company.logo = %%get_logo(%%odoo_company_logo)
    env.company.report_footer = '%%odoo_company_footer'
    env.company.external_report_layout_id = env.ref('web.external_layout_%%odoo_company_layout').id
    doc = env['base.document.layout'].create({'company_id': env.company.id})
    doc._onchange_company_id()
    # Admin
    admin = env['res.users'].search([('name', '=', 'Administrator')])
    admin.email = "%%odoo_admin_email"
    admin.password = '%%odoo_admin_password'
    # URL
    env['ir.config_parameter'].set_param('web.base.url', 'https://%%revprox_client_external_domainnames[0]')
    env['ir.config_parameter'].set_param('web.base.url.freeze', True)
    # LDAP
    env['res.config.settings'].create({'module_auth_ldap': True}).execute()
    ldaps = env.company.ldaps
    if ldaps:
        ldap = ldaps[0]
        ldap.ldap_server = '%%ldap_server_address'
        ldap.ldap_server_port = '636'
        ldap.ldap_binddn = '%%ldapclient_user'
        ldap.ldap_password = '%%ldapclient_user_password'
        ldap.ldap_filter = 'cn=%s'
        ldap.ldap_base = '%%ldapclient_user_dn'
    else:
        ldap = env['res.company.ldap'].create({'company': env.company.id,
                                               'ldap_server': '%%ldap_server_address',
                                               'ldap_server_port': '636',
                                               'ldap_binddn': '%%ldapclient_user',
                                               'ldap_password': '%%ldapclient_user_password',
                                               'ldap_filter': 'cn=%s',
                                               'ldap_base': '%%ldapclient_user_dn',
                                               })
        env.company.ldaps = ldap
    # SMTP
    mail = env['ir.mail_server'].search([('name', '=', 'Silique')])
    if mail.id is False:
        env['ir.mail_server'].create({'name': 'Silique',
                                      'smtp_host': '%%smtp_relay_address',
                                      'smtp_port': '25',
                                      'smtp_authentication': 'login',
                                      'smtp_user': '%%smtp_relay_user@%%ip_eth0',
                                      'smtp_pass': '%%smtp_relay_password',
                                      'smtp_encryption': 'starttls',
                                      })
    else:
        mail.smtp_host = '%%smtp_relay_address'
        mail.smtp_port = '25'
        mail.smtp_authentication = 'login'
        mail.smtp_user = '%%smtp_relay_user@%%ip_eth0'
        mail.smtp_pass = '%%smtp_relay_password'
        mail.smtp_encryption = 'starttls'
    env['ir.config_parameter'].set_param('base_setup.default_external_email_server', True)
