# Build this from the parent directory, like:
# > docker build \
#   -f docker/accounting.Dockerfile \
#   -t orchard/monolith-cli \
#   --build-arg GITHUB_ACCESS_TOKEN=$GITHUB_ACCESS_TOKEN \
#   --build-arg DB_HOST=$DB_HOST \
#   --build-arg DB_USER=$DB_USER \
#   --build-arg DB_PASS=$DB_PASS \
#   --build-arg AWS_KEY=$AWS_KEY \
#   --build-arg AWS_SECRET=$AWS_SECRET \
#   --build-arg AWS_REGION=$AWS_REGION \
#   .

FROM centos:centos7
ARG GITHUB_ACCESS_TOKEN
ARG DB_HOST
ENV DB_HOST=$DB_HOST
ARG DB_USER
ENV DB_USER=$DB_USER
ARG DB_PASS
ENV DB_PASS=$DB_PASS
ENV DB_NAME=art_relations

ARG AWS_KEY
ARG AWS_SECRET
ARG AWS_REGION
ENV AWS_REGION ${AWS_REGION:-us-east-1}

ENV COMPOSER_AUTH="{\"github-oauth\": {\"github.com\": \"${GITHUB_ACCESS_TOKEN}\"}}"
RUN yum -y update
# Install required packages for making and building php extensions and packages
RUN yum -y install https://dl.fedoraproject.org/pub/epel/epel-release-latest-7.noarch.rpm && \
    yum -y install http://rpms.remirepo.net/enterprise/remi-release-7.rpm
RUN yum -y update && \
    yum -y install --enablerepo=remi-php56 \
        make \
        curl \
        gcc \
        gettext \
        git \
        gnupg \
        unzip \
        php php-common php-pear php-pecl-yaml php-gd php-devel php-mysqlnd php-pecl-memcached php-pecl-memcache php-mcrypt php-pecl-imagick php-soap php-xml php-mbstring ImageMagick ImageMagick-devel php-magicwand pcre-devel

# Customize php config
RUN touch /etc/php.d/00-override.ini && \
    echo 'memory_limit = 256M' >> /etc/php.d/00-override.ini && \
    echo 'short_open_tag = On' >> /etc/php.d/00-override.ini && \
    echo 'date.timezone = America/New_York' >> /etc/php.d/00-override.ini

# Install composer
RUN php -r "copy('https://getcomposer.org/installer', 'composer-setup.php');" \
    && echo "Skipping composer checksum" \
    && php ./composer-setup.php --quiet \
    && php -r "unlink('composer-setup.php');" \
    && mv ./composer.phar /usr/local/bin/composer

# Setup the directory and operate on config files.
WORKDIR /usr/src/oa
COPY . .

RUN composer install --quiet

# Setup configs
RUN for f in ./application/configs/*.shadow.*; do TMP_FILENAME=$(basename -- "$f") && TMP_DIR=$(dirname -- "$f") && TMP_TRIMMED_NAME=$(echo "${TMP_FILENAME}" | sed s/shadow\.//g) && cp "$f" "${TMP_DIR}/${TMP_TRIMMED_NAME}"; done

# Replace {{ TOKENS }}
RUN cat ./public/db.shadow.inc | \
    awk -v DB_HOST="$DB_HOST" '{gsub(/{{ MYSQL_HOST_ART_RELATIONS }}/,DB_HOST)}1' | \
    awk -v DB_USER="$DB_USER" '{gsub(/{{ MYSQL_USER_ART_RELATIONS }}/,DB_USER)}1' | \
    awk -v DB_PASS="$DB_PASS" '{gsub(/{{ MYSQL_PASSWORD_ART_RELATIONS }}/,DB_PASS)}1' \
    > ./public/db.inc

RUN cat ./public/db_read_only.shadow.inc | \
    awk -v DB_HOST="$DB_HOST" '{gsub(/{{ MYSQL_HOST_ART_RELATIONS }}/,DB_HOST)}1' | \
    awk -v DB_USER="$DB_USER" '{gsub(/{{ MYSQL_USER_ART_RELATIONS }}/,DB_USER)}1' | \
    awk -v DB_PASS="$DB_PASS" '{gsub(/{{ MYSQL_PASSWORD_ART_RELATIONS }}/,DB_PASS)}1' \
    > ./public/db_read_only.inc

RUN cat ./application/configs/application.shadow.ini | \
    awk -v DB_HOST="$DB_HOST" '{gsub(/{{ MYSQL_HOST_ART_RELATIONS }}/,DB_HOST)}1' | \
    awk -v DB_USER="$DB_USER" '{gsub(/{{ MYSQL_USER_ART_RELATIONS }}/,DB_USER)}1' | \
    awk -v DB_PASS="$DB_PASS" '{gsub(/{{ MYSQL_PASSWORD_ART_RELATIONS }}/,DB_PASS)}1' \
    > ./application/configs/application.ini

RUN cat ./application/configs/dd_db.shadow.yml | \
    awk -v DB_HOST="$DB_HOST" '{gsub(/{{ MYSQL_HOST_ART_RELATIONS }}/,DB_HOST)}1' | \
    awk -v DB_USER="$DB_USER" '{gsub(/{{ MYSQL_USER_ART_RELATIONS }}/,DB_USER)}1' | \
    awk -v DB_PASS="$DB_PASS" '{gsub(/{{ MYSQL_PASSWORD_ART_RELATIONS }}/,DB_PASS)}1' \
    > ./application/configs/dd_db.yml

RUN cat ./application/configs/art_db.shadow.yml | \
    awk -v DB_HOST="$DB_HOST" '{gsub(/{{ MYSQL_HOST_ART_RELATIONS }}/,DB_HOST)}1' | \
    awk -v DB_USER="$DB_USER" '{gsub(/{{ MYSQL_USER_ART_RELATIONS }}/,DB_USER)}1' | \
    awk -v DB_PASS="$DB_PASS" '{gsub(/{{ MYSQL_PASSWORD_ART_RELATIONS }}/,DB_PASS)}1' | \
    awk -v DB_HOST="$DB_HOST" '{gsub(/{{ MYSQL_HOST_PUBLISHING }}/,DB_HOST)}1' | \
    awk -v DB_USER="$DB_USER" '{gsub(/{{ MYSQL_USER_PUBLISHING }}/,DB_USER)}1' | \
    awk -v DB_PASS="$DB_PASS" '{gsub(/{{ MYSQL_PASSWORD_PUBLISHING }}/,DB_PASS)}1' \
    > ./application/configs/art_db.yml

RUN cat ./application/configs/aws.shadow.yml | \
    awk -v AWS_KEY="$AWS_KEY" '{gsub(/{{ AWS_KEY }}/,AWS_KEY)}1' | \
    awk -v AWS_SECRET="$AWS_SECRET" '{gsub(/{{ AWS_SECRET }}/,AWS_SECRET)}1' | \
    awk -v AWS_SECRET="$AWS_REGION" '{gsub(/{{ AWS_REGION }}/,AWS_REGION)}1' | \
    awk '{gsub(/{{ AWS_ENV_PREFIX }}/,"")}1' \
    > ./application/configs/aws.yml

RUN cat ./application/configs/config.shadow.ini | \
    sed s/\{\{\ MEMCACHE_HOST\ \}\}/"localhost"/g | \
    sed s/\{\{\ MEMCACHE_PORT\ \}\}/"11211"/g \
    > ./application/configs/config.ini

RUN cat ./application/configs/doctrine.shadow.ini | \
    sed s/\{\{\ DOCTRINE_RESULTS_CACHE_ON\ \}\}/'true'/g | \
    sed s/\{\{\ MEMCACHE_HOST\ \}\}/"localhost"/g | \
    sed s/\{\{\ MEMCACHE_PORT\ \}\}/"11211"/g \
    > ./application/configs/doctrine.ini

RUN cat ./application/configs/log.shadow.yml | \
    sed s/\{\{\ SENTRY_DSN\ \}\}//g | \
    sed s/\{\{\ LOGGLY_TOKEN\ \}\}//g \
    > ./application/configs/log.yml

ENTRYPOINT [ "make", "test_calc" ]
