IMAGE sahana_1.0.3-200621
FROM alpine3.11-python3.8_3.8.2-200621

RUN EOF
    # Install runtime dependencies
    apk --no-cache add ansible at geos nginx py3-dateutil py3-gdal py3-lxml py3-numpy py3-pillow py3-psycopg2 py3-requests py3-sgmllib3k py3-yaml uwsgi-python3 sudo

    # Install build dependencies
    apk --no-cache add --virtual .deps build-base freetype-dev git py3-numpy-dev py3-pip python3-dev ttf-dejavu

    # Install web2py
    git clone --recursive https://github.com/web2py/web2py.git /srv/web2py
    git -C /srv/web2py checkout 59700b8
    git -C /srv/web2py submodule update

    # Symlink WSGI handler
    ln -s handlers/wsgihandler.py /srv/web2py/wsgihandler.py

    # Install Sahana
    git clone --recursive --depth 1 https://github.com/sahana/eden.git /srv/web2py/applications/eden

    # Install python dependencies, exclude old or unnecessary ones
    sed -i 's/^Pillow/#Pillow/' /srv/web2py/applications/eden/optional_requirements.txt     # We have 6.2.1, Sahana requires 6.2.2
    sed -i 's/^boto/#boto/' /srv/web2py/applications/eden/optional_requirements.txt         # Needed for setup on AWS
    sed -i 's/^PyRTF/#PyRTF/' /srv/web2py/applications/eden/optional_requirements.txt       # Ceased to exist
    sed -i 's/^openst/#openst/' /srv/web2py/applications/eden/optional_requirements.txt     # Needed for setup on OpenStack
    pip3 install -r /srv/web2py/applications/eden/optional_requirements.txt
    pip3 install translate-toolkit

    # Hackfix paths for ansible and other modules with hardcoded paths
    ln -s /srv/web2py /home/prod

    # Copy fonts with Czech glyphs
    cp /usr/share/fonts/ttf-dejavu/DejaVuSerif-Bold.ttf /srv/web2py/applications/eden/static/fonts/Helvetica-Bold.ttf
    cp /usr/share/fonts/ttf-dejavu/DejaVuSerif.ttf /srv/web2py/applications/eden/static/fonts/Helvetica.ttf
EOF

COPY image.d

RUN EOF
    # Create OS user
    addgroup -S -g 8080 sahana
    adduser -S -u 8080 -h /srv/web2py -s /bin/false -g sahana -G sahana sahana
    chown -R sahana:sahana /srv/web2py

    # Patch web2py for python 3.8
    cd /srv/web2py
    patch -p0 </srv/web2py/applications/eden/private/eden_deploy/roles/common/files/_compat.patch
    cd /srv/web2py/gluon
    patch -p0 </srv/web2py/applications/eden/private/eden_deploy/roles/common/files/scheduler.diff

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

CMD /bin/s6-svscan /etc/services.d