IMAGE gnuhealth_3.6.3-200403
FROM alpine3.11-python3.8_3.8.2-200403

RUN EOF
    # Install runtime dependencies
    apk --no-cache add bash coreutils libffi libjpeg-turbo libpq nodejs

    # Install build dependencies
    apk --no-cache add --virtual .deps build-base git libffi-dev libjpeg-turbo-dev libxml2-dev libxslt-dev ncurses npm patch postgresql-dev python3-dev sudo

    # Download GNU Health
    wget http://ftp.gnu.org/gnu/health/gnuhealth-3.6.3.tar.gz -O - | tar xzf - -C /srv
    mv /srv/gnuhealth-* /srv/install

    # Create OS user
    addgroup -S -g 8080 gnuhealth
    adduser -S -u 8080 -h /srv/gnuhealth -s /bin/bash -g gnuhealth -G gnuhealth gnuhealth
    chown -R gnuhealth:gnuhealth /srv/gnuhealth

    # Hackfix to install specific version of python dependency which doesn't fail during setup
    # https://github.com/sdispater/pendulum/issues/457
    sed -i 's/pendulum/pendulum==2.0.3/' /srv/install/gnuhealth-setup

    # Install GNU Health
    cd /srv/install
    sudo -u gnuhealth ./gnuhealth-setup install

    # Create symlink for the current version of trytond
    ln -s /srv/gnuhealth/gnuhealth/tryton/server/trytond-* /srv/gnuhealth/gnuhealth/tryton/server/trytond-current

    # Clone Sao (Tryton web client) repository
    # Note: Sao version needs to be the same as Tryton version installed by GNU Health
    git clone -b 5.0 --single-branch --depth 1 https://github.com/tryton/sao /srv/gnuhealth/sao
    chown -R gnuhealth:gnuhealth /srv/gnuhealth/sao

    # Install Sao (Tryton web client) dependencies
    cd /srv/gnuhealth/sao
    sudo -u gnuhealth npm install --production
    sudo -u gnuhealth ./node_modules/grunt-cli/bin/grunt

    # Download Demo database
    wget http://health.gnu.org/downloads/postgres_dumps/gnuhealth-36-demo.sql.gz -O /srv/gnuhealth/gnuhealth_demo.sql.gz

    # Cleanup
    apk --no-cache del .deps
    find /srv/gnuhealth -name '.git*' -exec rm -rf {} +
    rm -rf /srv/install
EOF

ENV PATH /srv/gnuhealth/gnuhealth/tryton/server/trytond-current/bin:/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin
ENV TRYTOND_CONFIG /srv/gnuhealth/gnuhealth/tryton/server/config/trytond.conf
ENV PYTHONPATH /srv/gnuhealth/gnuhealth/tryton/server/trytond-current:/srv/gnuhealth/gnuhealth/tryton/server/config

USER gnuhealth
CMD /srv/gnuhealth/gnuhealth/tryton/server/trytond-current/bin/trytond --verbose