# Stage 0, "build-stage", based on Node.js, to build and compile Angular
FROM ubuntu:latest as build-stage

RUN DEBIAN_FRONTEND=noninteractive apt-get update && \
    DEBIAN_FRONTEND=noninteractive apt-get -y dist-upgrade

# Installing supervisord
RUN apt-get install -y supervisor && \
apt-get install -y gnupg2 && apt-get install -y gnupg2 && \
apt-get -yq install ssh && \
apt-get -yq install curl

ADD ./docker/supervisord.conf /etc/supervisor/conf.d/supervisord.conf 
 
RUN DEBIAN_FRONTEND=noninteractive apt-get -yq install net-tools nginx lsb-release

RUN rm -f /etc/nginx/fastcgi.conf /etc/nginx/fastcgi_params && \
    rm -f /etc/nginx/snippets/fastcgi-php.conf /etc/nginx/snippets/snakeoil.conf \
    rm -f /etc/nginx/sites-available/default \
    rm -f /etc/nginx/sites-enabled/default

COPY /nginx/default.conf /etc/nginx/sites-available
COPY /nginx/default.conf /etc/nginx/sites-enabled


# Install Node.js 6.x repository
RUN curl -sL https://deb.nodesource.com/setup_6.11.1 | bash -

# Install Node.js and npm
RUN apt-get install -y nodejs

RUN apt-key adv --keyserver hkp://keyserver.ubuntu.com:80 --recv 0C49F3730359A14518585931BC711F9BA15703C6

RUN echo "deb http://repo.mongodb.org/apt/debian jessie/mongodb-org/3.4 main" | tee /etc/apt/sources.list.d/mongodb-org-3.4.list

RUN apt-get update && apt-get -yq install mongodb-org && apt-get -yq install redis-server
RUN mkdir -p /data/db
RUN mongod --fork --syslog

RUN apt-get -yq install expect-dev
# sudo ./mongodb/bin/mongodump --host localhost --port 27017 --db atlasvoyages-dev --out ./

# sudo ./mongodb/bin/mongodump --host localhost --port 27017 --db atlasvoyages-dev --gzip --archive=atlasvoyages-dev.archive



WORKDIR /atlasvoyages


COPY package*.json /atlasvoyages/


# RUN npm install --global grunt-cli 


# RUN git config --global http.proxy http://<username>:<password>@<proxy-server-url>:<port>

# RUN echo "//<npm-registry>:8080/:_authToken=KB9DEBiM5vNeGPwinPuj" > ~/.npmrc

# Take an SSH key as a build argument.
# ARG SSH_PRIVATE_KEY

# RUN echo exit 0 > /usr/sbin/policy-rc.d

# RUN apt-get update && apt-get install -y openssh-server
# 1. Create the SSH directory.
# 2. Populate the private key file.
# 3. Set the required permissions.
# 4. Add github to our list of known hosts for ssh.
# RUN mkdir -p /root/.ssh/ && \
#     echo "$SSH_PRIVATE_KEY" > /root/.ssh/id_rsa && \ 
#     chmod -R 600 /root/.ssh/ && \
#     ssh-keyscan labs.atlasvoyages.com >> /root/.ssh/known_hosts && \
#     cat /root/.ssh/id_rsa && \
#     ssh -T git@labs.atlasvoyages.com
 
# RUN git clone git@labs.atlasvoyages.com:pine/theme-atlasvoyages.git -b 0.3-beta-v1
 
COPY ./ /atlasvoyages/

RUN expect -f ./sh/npm_login_expect nabilelmahiri arigatou@65 "nabil.elmahiri@fractalite.com" && npm i -S git+http://nabilelmahiri@labs.atlasvoyages.com/pine/pine-express.git#0.3-dev

# RUN mongorestore --host localhost --port 27017 --gzip --archive=atlasvoyages-dev.archive

RUN find ./node_modules -maxdepth 1 -name 'pine*' -type d \( ! -name . \) -exec bash -c "cd '{}' && npm i --unsafe-perm=true && grunt build:dist" \; \
    find ./node_modules -maxdepth 1 -name 'hermes*' -type d \( ! -name . \) -exec bash -c "cd '{}' && npm i --unsafe-perm=true && grunt build:dist" \; \
    find ./node_modules -maxdepth 1 -name 'theme-atlasvoyages*' -type d \( ! -name . \) -exec bash -c "cd '{}' && npm i --unsafe-perm=true && grunt build:dist" \;

RUN echo '\n' | grunt pine:setup

RUN grunt pine:copy


# expose port 80 443
EXPOSE 80
EXPOSE 443

# USER postgres
ENTRYPOINT ["/usr/bin/supervisord"]