#
#  Copyright (C) 2008 National Institute For Space Research (INPE) - Brazil.
#
#  This file is part of the TerraLib - a Framework for building GIS enabled applications.
#
#  TerraLib is free software: you can redistribute it and/or modify
#  it under the terms of the GNU Lesser General Public License as published by
#  the Free Software Foundation, either version 3 of the License,
#  or (at your option) any later version.
#
#  TerraLib 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 Lesser General Public License for more details.
#
#  You should have received a copy of the GNU Lesser General Public License
#  along with TerraLib. See COPYING. If not, write to
#  TerraLib Team at <terralib-team@terralib.org>.
#

#
## Author: TerraLib Team <terralib-team@terralib.org>
##
##
## Description
## ---
## It creates a environment for Continuous Integration Server (Jenkins) for TerraLib Linux Ubuntu systems using Qt4.
##

# Deriving from Ubuntu 14.04 trusty edition
FROM ubuntu:trusty

# It exports Brazilian Ubuntu repository to apt. It is important statement due some of package dependencies are not defined in english repositories
# In other words, it may generate a unstable package since the versions does not mismatch. For example: swig3.0 and libhdf4-alt-dev
RUN echo "deb http://br.archive.ubuntu.com/ubuntu/ trusty-backports main restricted universe multiverse" >> /etc/apt/sources.list
RUN echo "deb-src http://br.archive.ubuntu.com/ubuntu/ trusty-backports main restricted universe multiverse" >> /etc/apt/sources.list

# Update container
RUN apt-get update

# Installing required dependencies for Jenkins Communication, Qt4 dependencies and useful commands
RUN apt-get install -y wget curl git openssh-server openjdk-7-jdk libxext-dev software-properties-common python-software-properties unzip libglu1-mesa-dev build-essential

## Global docker variables
ARG TL_BRANCH=release-5.2 
ARG TL_USER=terralib
ARG DEV_DIR=/home/$TL_USER/mydevel
ARG TL_HOME=$DEV_DIR/terralib
ARG TL_CODEBASE=$TL_HOME/codebase
ARG MYLIBS=/home/$TL_USER/mylibs

# Creating user terralib and group ans setting to sudo
RUN groupadd $TL_USER
RUN useradd $TL_USER -s /bin/bash -m -g $TL_USER
RUN usermod -aG sudo $TL_USER

# Defining TerraLib credential to connect through SSH
RUN echo $TL_USER:$TL_USER | chpasswd
# Creating folders
RUN mkdir -p $DEV_DIR $TL_CODEBASE $MYLIBS
RUN chown -R $TL_USER:$TL_USER $MYLIBS $TL_HOME $DEV_DIR
# Granting $TL_USER to execute these commands without password
RUN echo "$TL_USER ALL=(ALL) NOPASSWD: /usr/bin/apt-get" >> /etc/sudoers
RUN echo "$TL_USER ALL=(ALL) NOPASSWD: /usr/bin/dpkg" >> /etc/sudoers
RUN echo "$TL_USER ALL=(ALL) NOPASSWD: /usr/bin/make" >> /etc/sudoers

# Setting SSHD context to auto start in order to Jenkins connect through SSH
RUN mkdir /var/run/sshd
RUN chmod 0755 /var/run/sshd
RUN ssh-keygen -A

# Defining current container folder scope
WORKDIR $MYLIBS
USER $TL_USER

# Downloading Qt4
RUN curl -L -O http://download.qt-project.org/official_releases/qt/4.8/4.8.6/qt-everywhere-opensource-src-4.8.6.tar.gz
# Installing Qt4
RUN tar zxf qt-everywhere-opensource-src-4.8.6.tar.gz
WORKDIR $MYLIBS/qt-everywhere-opensource-src-4.8.6
# Agreeing with LGPL EULA
RUN printf 'y\n' | ./configure -bindir /usr/bin -plugindir /usr/lib/qt4/plugins -importdir /usr/lib/qt4/imports -headerdir /usr/include/qt4 -datadir /usr/share/qt4 -opensource -no-multimedia -no-audio-backend -no-webkit -no-javascript-jit -no-script -opengl
RUN make -j 4
RUN sudo make install

# Disabling Git SSL verification
RUN git config --global http.sslVerify false
# Cloning TerraLib to compile dependencies
RUN git clone -b $TL_BRANCH https://gitlab.dpi.inpe.br/terralib/terralib.git $TL_CODEBASE

# Setting current scope to $MYLIBS folder
WORKDIR $MYLIBS
# Download 3rdparty files for compilation
RUN curl -O http://www.dpi.inpe.br/terralib5-devel/3rdparty/src/terralib-3rdparty-linux-ubuntu-14.04.tar.gz
# Copying custom TerraLib Qt4 3rdparty script to 
RUN cp $TL_CODEBASE/install/install-3rdparty-linux-ubuntu-14.04.sh .

# Compile TerraLib dependencies and install them
RUN TE_USE_QT="qt4" TERRALIB_DEPENDENCIES_DIR=$MYLIBS ./install-3rdparty-linux-ubuntu-14.04.sh

# Defining current scope as TerraLib home.
WORKDIR $TL_HOME
USER root

# Every docker container must have a final execution that determines a container state. In other words, we must
# specify final behavior whenever a container starts and stop. You can even pass arguments to pre-configure tasks
# like database passwords, etc. Thus, it is common to expose a entrypoint file containing start services rules and "trap"
# unix directive to finalize all. But, Jenkins Docker plugin does not working with entrypoint properly. In this way,
# we are just exporting a service ssh initialization. After all, Jenkins will use it to instantiate a container and connect
# through SSH. See more about entrypoints in https://docs.docker.com/engine/reference/builder/#entrypoint
CMD ["/etc/init.d/ssh", "start"]