###
### Meta Information
###
FROM localhost/debian

# deploy options
# -p (port) and -v (volume) both go host:container
LABEL config_default="\
-p 9082:80 \
-v /srv/vol/startpage/data:/vol/data"

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

###
### General Setup
###

# install packages we want
RUN apt update -y && apt install -y apache2 libapache2-mod-php php-json

# autodetect php version and put it in /etc/environment
RUN echo "PHPV=$(echo $(php -r 'echo PHP_VERSION;') | cut -d '.' -f 1-2)" >> /etc/environment

# change www-data's UID to the file owner UID
RUN usermod --uid $FILESUID www-data && \
    groupmod --gid $FILESUID www-data && \
    chown -R www-data:www-data /var/www

###
### Apache
###

# enable modules we need
RUN a2enmod php${PHPV:?}

# copy site config
COPY assets/site.conf /etc/apache2/sites-available/
WORKDIR /etc/apache2/sites-enabled
RUN rm 000-default.conf && ln -s ../sites-available/site.conf

###
### browserStartpage
###

# download app
WORKDIR /root
RUN wget https://github.com/saschadiercks/browserStartpage/archive/master.zip && \
    echo "Unzipping ..." && \
    unzip -q master.zip && \
    mv browserStartpage-master/htdocs /var/www/html/startpage && \
    chown -R www-data:www-data /var/www/html && \
    rm -r browserStartpage-master

# make symlinks
WORKDIR /var/www/html
RUN rm startpage/config/config.php && \
    ln -s /vol/data/config.php startpage/config/config.php && \
    rm startpage/data/data.json && \
    ln -s /vol/data/data.json startpage/data/data.json && \
    rm -r startpage/assets/thumbnails && \
    ln -s /vol/data/thumbnails startpage/assets/thumbnails && \
    rm -r startpage/assets/wallpaper && \
    ln -s /vol/data/wallpaper startpage/assets/wallpaper