# Liquid War is a multiplayer wargame
# Copyright (C) 1998-2018 Christian Mauduit
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
#
# Liquid War homepage : https://ufoot.org/liquidwar/v5
# Contact author      : ufoot@ufoot.org

FROM debian
MAINTAINER Christian Mauduit "ufoot@ufoot.org"
LABEL package="liquidwar" version="@VERSION@" org.ufoot.liquidwar.os="debian" org.ufoot.liquidwar.type="metaserver" description="Liquid War metaserver image, runs a metaserver on a LAMP stack (Linux/Apache/MySQL/PHP) listening on port 8053. Homepage https://ufoot.org/liquidwar/v5"

ENV DEBIAN_FRONTEND noninteractive
RUN (apt-get update -y && apt-get upgrade -y && apt-get dist-upgrade -y && apt-get -y autoclean && apt-get -y autoremove)

# typical LAMP install
RUN apt-get install -y apache2 mysql-server libapache2-mod-php7.0 php7.0-mysql
RUN apt-get clean

# set up Apache
COPY 010-metaserver.conf /etc/apache2/sites-available/010-metaserver.conf
RUN cd /etc/apache2/sites-enabled && ln -s ../sites-available/010-metaserver.conf .
RUN cd /etc/apache2/mods-enabled && ln -s ../mods-available/rewrite.load .
COPY metaserver.tar.gz /root/metaserver.tar.gz
RUN cd /var/www && tar xzf /root/metaserver.tar.gz

# set up Mysql, note the clear password, as the MySQL server is sandboxed
# within Docker, revealing this is not that bad, one could typically imagine
# a no-password through UNIX socket connection, but doing this with an exposed
# password represents the same threat and is much easier to handle. Use at your
# own risk anyway, this is not a stronghold, you've been warned.
COPY tables.sql /root/tables.sql
RUN /etc/init.d/mysql start && mysqladmin create metaserver && (mysql < /root/tables.sql metaserver) && (echo "GRANT SELECT, INSERT, UPDATE, DELETE ON metaserver.* TO www IDENTIFIED BY 'dd02c7c2232759874e1c205587017bed';" | mysql) && (echo "FLUSH PRIVILEGES;" | mysql ) && echo "done" ; /etc/init.d/mysql stop

# startup script
RUN install -d /usr/local/bin
COPY lamp.sh /usr/local/bin/lamp.sh

# run server on default port
EXPOSE 8053
ENTRYPOINT /usr/local/bin/lamp.sh
