###
### Build Variables
###
FROM localhost/debian:latest

# deploy options
# -p (port) and -v (volume) both go host:container
LABEL config_default="\
-p 9081:80 \
-p 9022:22 \
-v /srv/vol/gitea/git:/vol/git \
-v /srv/vol/gitea/data:/vol/data \
-v /srv/vol/gitea/log:/vol/log \
-v /srv/vol/gitea/keys:/vol/keys \
"

# Build variables
# uid that the files owner user should have
ARG FILESUID=5000

# Container Variables
# database name and user
ENV DBUSER=gtadmin
ENV DBNAME=gitea

###
### General Setup
###

# install packages we want
RUN apt update -y && apt install -y postgresql postgresql-doc git openssh-server

# create directories for volumes
RUN mkdir -p /vol/git /vol/data /vol/log /vol/keys

# put database variables in /etc/environment so anyone can access them
# also autodetect versions of postgres and gitea and put them in /etc/environment as well
RUN echo "DBUSER=$DBUSER" >> /etc/environment && \
    echo "DBNAME=$DBNAME" >> /etc/environment && \
    echo "PSQLV=$(psql -V | cut -d ' ' -f 3 | cut -d '.' -f 1)" >> /etc/environment && \
    echo "GITEAV=$(wget -q -O - https://dl.gitea.io/gitea/ | grep -m 1 'Current Release' \
                 | perl -pe 's/.*Current Release ([\d.]+).*/\1/')" >> /etc/environment

# create gitea user with file owner UID
RUN addgroup --gid $FILESUID gitea && \
    adduser gitea --ingroup gitea --uid $FILESUID --disabled-password --gecos "Gitea Server" --shell /bin/bash --home /var/lib/gitea

# copy our custom scripts
COPY assets/bin/ /usr/local/bin/

# replace /var/log with symlink to /vol/log
RUN mkdir -p /vol/log && \
    rm -r /var/log && \
    ln -s /vol/log /var/log

###
### SSH Server ###
###

# copy sshd config
COPY assets/sshd_config /etc/ssh/sshd_config

# make link to keys volume
RUN ln -s /vol/keys /etc/ssh/keys

###
### PostgreSQL ###
###

# configure PostgreSQL access
COPY --chown=postgres:postgres assets/pg_hba.conf ./
RUN mv pg_hba.conf /etc/postgresql/${PSQLV:?}/main/pg_hba.conf

###
### Gitea
###

# dowload gitea
RUN wget https://dl.gitea.io/gitea/${GITEAV:?}/gitea-${GITEAV:?}-linux-amd64 && \
    mv gitea-${GITEAV:?}-linux-amd64 /usr/local/bin/gitea && \
    chmod +x /usr/local/bin/gitea

# make directories gitea needs
RUN mkdir -p /var/lib/gitea/custom /var/lib/gitea/data/ /var/log/gitea && \
    chown -R gitea:gitea /var/lib/gitea /var/log/gitea && \
    chmod -R 750 /var/lib/gitea /var/log/gitea && \
    ln -s /var/log/gitea /var/lib/gitea/log

# copy gitea config template
COPY assets/app.ini.esh /etc/gitea/
# template config file
RUN cd /etc/gitea/ && \
    esh app.ini.esh > app.ini && \
    rm app.ini.esh && \
    chmod -R +r /etc/gitea/

COPY assets/gitea.service /etc/systemd/system/

# make alias for running admin commands from command line easily
RUN echo "alias gitea='sudo -u gitea gitea --config /etc/gitea/app.ini'" >> /root/.bashrc

###
### Crontab
###
COPY assets/crontab /root/
RUN crontab -u gitea /root/crontab

###
### Bugfix
###

# bugfix for cron
COPY bugfix/cronfix /root/
RUN chmod +x /root/cronfix && /root/cronfix