FROM python:3.9-slim-buster
MAINTAINER Le Filament <https://le-filament.com>

ENV APT_DEPS='build-essential libldap2-dev libpq-dev libsasl2-dev' \
    LANG=C.UTF-8 \
    LC_ALL=C.UTF-8 \
    PGDATABASE=odoo

RUN set -x; \
        apt-get update &&\
        apt-get install -y --no-install-recommends \
            curl \
            git \
            gnupg \
            npm \
            openssh-client \
            xmlsec1 &&\
        echo 'deb http://apt-archive.postgresql.org/pub/repos/apt/ buster-pgdg main' >> /etc/apt/sources.list.d/postgresql.list &&\
        curl -SL https://www.postgresql.org/media/keys/ACCC4CF8.asc | apt-key add - &&\
        curl -o wkhtmltox.deb -SL https://github.com/wkhtmltopdf/wkhtmltopdf/releases/download/0.12.5/wkhtmltox_0.12.5-1.buster_amd64.deb &&\
        echo 'ea8277df4297afc507c61122f3c349af142f31e5 wkhtmltox.deb' | sha1sum -c - &&\
        apt-get update &&\
        apt-get install -y --no-install-recommends ./wkhtmltox.deb &&\
        apt-get install -y --no-install-recommends postgresql-client &&\
        apt-get install -y --no-install-recommends ${APT_DEPS} &&\
        pip3 install -r https://raw.githubusercontent.com/OCA/OCB/14.0/requirements.txt &&\
        pip3 install phonenumbers simplejson gevent==20.12.1 openupgradelib pdfminer.six PyYAML zxcvbn &&\
        apt-get -y purge ${APT_DEPS} &&\
        apt-get -y autoremove &&\
        rm -rf /var/lib/apt/lists/* wkhtmltox.deb

ARG SAVE_COMMITS_DIR='/opt/odoo_commits/'
ENV SAVE_COMMITS_DIR=$SAVE_COMMITS_DIR

# Add Git Known Hosts
COPY ./ssh_known_git_hosts /root/.ssh/known_hosts

# Install Odoo and remove not French translations and .git directory to limit amount of data used by container
ARG SAVE_COMMITS_FILENAME='ocb'
RUN set -x; \
  repo='https://github.com/OCA/OCB' \
  branch='14.0' \
  && useradd --create-home --home-dir /opt/odoo --no-log-init odoo \
  && /bin/bash -c "mkdir -p /opt/odoo/{etc,odoo,additional_addons,private_addons,data,private}" \
  && mkdir /opt/odoo_commits \
  && git config --global user.email "maintenance@le-filament.com" \
  && git config --global user.name "Le Filament" \
  && git clone --filter=tree:0 --branch "${branch}" "${repo}" /tmp/repo \
  && commit="$(git -C /tmp/repo rev-parse HEAD)" \
  # FIX begin: Merge pull request https://github.com/OCA/OCB/pull/1234
  && git -C /tmp/repo fetch origin pull/1234/head:14.0-fix-crm_lead-email \
  && git -C /tmp/repo checkout 14.0-fix-crm_lead-email \
  && git -C /tmp/repo rebase origin/${branch} \
  # FIX end
  && mv /tmp/repo/* /opt/odoo/odoo/ \
  && echo "repo;ref;commit" > $SAVE_COMMITS_DIR/$SAVE_COMMITS_FILENAME \
  && echo "${repo};${branch};${commit}" >> $SAVE_COMMITS_DIR/$SAVE_COMMITS_FILENAME \
  && rm -r /tmp/repo \
  # Only keep French translations.
  && find /opt/odoo/odoo/addons/*/i18n/ /opt/odoo/odoo/odoo/addons/base/i18n/ -type f -not -name 'fr.po' -delete \
  && echo "info: OCB commit:" \
  && cat $SAVE_COMMITS_DIR/$SAVE_COMMITS_FILENAME

# Add script to download Odoo addons.
COPY --chown=root:root --chmod=755 ./get_addons /usr/local/bin

# Install Odoo OCA default dependencies
ARG SAVE_COMMITS_FILENAME='default_addons'
RUN set -x; \
  get_addons 'https://github.com/OCA/account-financial-reporting' '14.0' 'additional_addons' \
    account_tax_balance \
  && get_addons 'https://github.com/OCA/account-financial-tools' '14.0' 'additional_addons' \ 
    account_lock_date_update \
    account_move_name_sequence \
    account_reconcile_show_boolean \
  && get_addons 'https://github.com/OCA/account-invoicing' '14.0' 'additional_addons' \
    sale_timesheet_invoice_description \
  && get_addons 'https://github.com/OCA/account-reconcile' '14.0' 'additional_addons' \
    account_reconciliation_widget \
  && get_addons 'https://github.com/OCA/bank-statement-import' '14.0' 'additional_addons' \
    account_statement_import_base \
    account_statement_import \
    account_statement_import_file_reconciliation_widget \
    account_statement_import_ofx \
  && get_addons 'https://github.com/OCA/crm' '14.0' 'additional_addons' \
    crm_stage_probability \
  && get_addons 'https://github.com/OCA/partner-contact' '14.0' 'additional_addons' \
    partner_disable_gravatar \
    partner_firstname \
  && get_addons 'https://github.com/OCA/project' '14.0' 'additional_addons' \
    project_status \
    project_task_default_stage \
    project_template \
    project_timeline \
  # Until migrated to OCA (https://github.com/OCA/project/pull/1016)
  && get_addons 'https://github.com/lefilament/project' '14.0-enh-project_category' 'additional_addons' \
    project_category \
  && get_addons 'https://github.com/OCA/server-auth' '14.0' 'additional_addons' \
    password_security \
  && get_addons 'https://github.com/OCA/server-brand' '14.0' 'additional_addons' \
    disable_odoo_online \
    remove_odoo_enterprise \
  && get_addons 'https://github.com/OCA/server-tools' '14.0' 'additional_addons' \
    base_search_fuzzy \
    module_change_auto_install \
  && get_addons 'https://github.com/OCA/server-ux' '14.0' 'additional_addons' \
    base_technical_features \
    date_range \
    mass_editing \
  && get_addons 'https://github.com/OCA/social' '14.0' 'additional_addons' \
    base_search_mail_content \
    mail_debrand \
    mail_tracking \
  && get_addons 'https://github.com/OCA/web' '14.0' 'additional_addons' \
    web_environment_ribbon \
    web_responsive \
    web_no_bubble \
    web_timeline \
  && find /opt/odoo/additional_addons/*/i18n/ -type f -not -name 'fr.po' -delete \
  # Install Le Filament default dependency
  && get_addons 'https://sources.le-filament.com/lefilament/remove_login_links.git' '14.0' 'private_addons/remove_login_links' \
  && get_addons 'https://sources.le-filament.com/lefilament/lefilament_release_agent.git' '14.0' 'private_addons/lefilament_release_agent' \
  && echo "info: default addon commits:" \
  && cat $SAVE_COMMITS_DIR/$SAVE_COMMITS_FILENAME

# Copy entrypoint script and Odoo configuration file
COPY ./entrypoint.sh /
COPY ./odoo.conf /opt/odoo/etc/odoo.conf
RUN chown -R odoo:odoo /opt/odoo

# Mount /opt/odoo/data to allow restoring filestore
VOLUME ["/opt/odoo/data/"]

# Expose Odoo services
EXPOSE 8069

# Set default user when running the container
USER odoo

# Start
ENTRYPOINT ["/entrypoint.sh"]
CMD ["odoo"]

# Metadata
LABEL org.label-schema.schema-version="14.0" \
      org.label-schema.vendor=LeFilament \
      org.label-schema.license=Apache-2.0 \
      org.label-schema.vcs-url="https://sources.le-filament.com/lefilament/odoo_docker"