FROM ubuntu:20.04

LABEL org.opencontainers.image.authors="Mike Mellor <mike.mellor@avalerehealth.com>"

# Silences bash no tty warning
RUN echo '#! /bin/sh' > /usr/bin/mesg

SHELL ["/bin/bash", "-l", "-c"]

# Set Timezone
ENV TZ=Europe/London
RUN ln -snf /usr/share/zoneinfo/$TZ /etc/localtime && echo $TZ > /etc/timezone

# Update apt-get
RUN apt-get update

# Install curl
RUN apt-get install -y curl git zip unzip vim

# Install wget
RUN apt-get install -y wget

# Install node
RUN curl -sL https://deb.nodesource.com/setup_10.x | bash

RUN apt-get install -y nodejs

# Update npm to latest
RUN npm install npm@6 -g

# Lock npm versions
RUN npm config set save-exact=true

# Install grunt-cli
RUN npm install grunt-cli -g

# Install package.json checker
RUN npm install check-dependencies -g

# Install node_modules checker
RUN npm install are-you-es5 -g

# Install imagemagick
RUN apt-get update && apt-get install -y imagemagick

# Install dart
# RUN apt-get update
# RUN apt-get install -y apt-transport-https
# RUN sh -c 'wget -qO- https://dl-ssl.google.com/linux/linux_signing_key.pub | apt-key add -'
# RUN sh -c 'wget -qO- https://storage.googleapis.com/download.dartlang.org/linux/debian/dart_stable.list > /etc/apt/sources.list.d/dart_stable.list'
# RUN apt-get update
# RUN apt-get install dart
# RUN echo 'export PATH="$PATH:/usr/lib/dart/bin"' >> ~/.profile
# RUN echo 'export PATH="$PATH:$HOME/.pub-cache/bin"' >> ~/.profile
# RUN pub global activate sass

# Install dart sass vm
RUN curl -L -o /sass.tar.gz https://github.com/sass/dart-sass/releases/download/1.34.0/dart-sass-1.34.0-linux-x64.tar.gz
RUN tar -xf /sass.tar.gz -C /
RUN echo 'export PATH="$PATH:/dart-sass/"' >> ~/.profile

# Install python
RUN apt-get update
RUN apt-get install -y python

# Install wine
RUN apt install -y software-properties-common
RUN dpkg --add-architecture i386

RUN wget -nc https://dl.winehq.org/wine-builds/winehq.key
RUN apt-key add winehq.key

RUN add-apt-repository 'deb https://dl.winehq.org/wine-builds/ubuntu/ bionic main'

RUN wget -nv https://download.opensuse.org/repositories/Emulators:/Wine:/Debian/xUbuntu_18.04/Release.key -O /tmp/Release.key
RUN apt-key add - < /tmp/Release.key

RUN apt-add-repository 'deb https://download.opensuse.org/repositories/Emulators:/Wine:/Debian/xUbuntu_18.04/ ./'
RUN apt-get update

RUN apt-get install -y --install-recommends winehq-stable

# Install PHP & composer
RUN add-apt-repository ppa:ondrej/php
RUN apt-get update
RUN apt-get install -y php7.2 php7.2-gd
# RUN apt-get install -y php7.0
# RUN apt-get install -y php7.0-gd
# RUN docker-php-ext-install gd
RUN curl -sS https://getcomposer.org/installer | php -- --install-dir=/usr/local/bin --filename=composer

# Install ftp
RUN apt-get install -y tnftp

# Install lftp
RUN apt-get install -y lftp

# PDF
RUN apt-get install -y g++ build-essential ghostscript
RUN apt install -y default-jre
RUN apt-get install -y xvfb

# Latest chrome version
RUN wget -q -O - https://dl-ssl.google.com/linux/linux_signing_key.pub | apt-key add -
RUN echo 'deb [arch=amd64] http://dl.google.com/linux/chrome/deb/ stable main' | tee /etc/apt/sources.list.d/google-chrome.list
RUN apt-get update
# RUN apt-get install -y google-chrome-stable

# Specific chrome version
# Check available versions here: https://www.ubuntuupdates.org/package/google_chrome/stable/main/base/google-chrome-stable
ARG CHROME_VERSION="105.0.5195.102-1"
RUN wget --no-verbose -O /tmp/chrome.deb http://dl.google.com/linux/chrome/deb/pool/main/g/google-chrome-stable/google-chrome-stable_${CHROME_VERSION}_amd64.deb
RUN apt install -y /tmp/chrome.deb

# Set flags to allow chrome to run as root user
RUN sed -i '49s/.*/exec -a "$0" "$HERE\/chrome" "$@" --no-sandbox --disable-setuid-sandbox --disable-dev-shm-usage/' /opt/google/chrome/google-chrome

# RUN apt-get install -y firefox
ARG FIREFOX_VERSION=84.0
RUN apt-get update -qqy \
  && apt-get -qqy --no-install-recommends install firefox \
#   && rm -rf /var/lib/apt/lists/* /var/cache/apt/* \
  && wget --no-verbose -O /tmp/firefox.tar.bz2 https://download-installer.cdn.mozilla.net/pub/firefox/releases/$FIREFOX_VERSION/linux-x86_64/en-US/firefox-$FIREFOX_VERSION.tar.bz2 \
  && apt-get -y purge firefox \
  && rm -rf /opt/firefox \
  && tar -C /opt -xjf /tmp/firefox.tar.bz2 \
  && rm /tmp/firefox.tar.bz2 \
  && mv /opt/firefox /opt/firefox-$FIREFOX_VERSION \
  && ln -fs /opt/firefox-$FIREFOX_VERSION/firefox /usr/bin/firefox

RUN apt-get install -y dos2unix

RUN apt-get install -y locales

# Install AWS Elastic Beanstalk cli
RUN apt-get install -y zlib1g-dev libssl-dev libncurses-dev libffi-dev libsqlite3-dev libreadline-dev libbz2-dev

RUN git clone https://github.com/aws/aws-elastic-beanstalk-cli-setup.git --depth 1 --branch v0.1.2
RUN ./aws-elastic-beanstalk-cli-setup/scripts/bundled_installer
RUN rm -rf ./aws-elastic-beanstalk-cli-setup

RUN sed -i '/en_US.UTF-8/s/^# //g' /etc/locale.gen && \
    locale-gen

# Install AWS-CLI@2
RUN curl "https://awscli.amazonaws.com/awscli-exe-linux-x86_64.zip" -o "awscliv2.zip"
RUN unzip awscliv2.zip
RUN ./aws/install
RUN rm -rf ./aws && rm -rf awscliv2.zip

# Cleanup apt-get install folders
RUN apt-get clean && \
    rm -rf /var/lib/apt/lists/* /tmp/* /var/tmp/*

# Add known hosts to stop npm from stopping when hitting a git dependency
#RUN ssh-keyscan bitbucket.org > /etc/ssh/ssh_known_hosts

RUN echo $'\
parse_git_branch() {\n\
    git branch 2> /dev/null | sed -e "/^[^*]/d" -e "s/* \(.*\)/ @\\1/"\n\
}\n\
export OVERRIDE=true\n\
git(){(\n\
    set -e\n\
    if [[ ${OVERRIDE} == true && "$1" == "rev-parse" && "$2" == "--show-toplevel" ]]; then\n\
        echo ${REPO}\n\
    elif [[ ${OVERRIDE} == true && "$1" == "rev-parse" && "$2" == "--short" && "$3" == "HEAD" && ! $(git rev-parse --git-dir 2>/dev/null) ]]; then\n\
        echo "$((100000 + $RANDOM % 999999))"\n\
    else\n\
        command git "$@" || exit 1\n\
    fi\n\
)}\n\
npm(){(\n\
    set -e\n\
    if [[ "$1" != "start" && "$1" != "test" && "$1" != "run" && "$1" != "version" ]]; then\n\
        export OVERRIDE=false\n\
        command npm "$@" || exit 1\n\
        echo "Adding the --upload-older flag to lftp deploy to stop vendor files being ignored"\n\
        sed -i \'s/--parallel=10/--upload-older --parallel=10/g\' $PWD/node_modules/@fishawack/core/_Tasks/deploy.js 2>/dev/null || true\n\
        echo "Replacing parallel flag of lftp push as uploads can sometimes fail if the server rejects"\n\
        sed -i \'s/--parallel=10/--parallel=1/g\' $PWD/node_modules/@fishawack/config-grunt/_Tasks/options/shell.js 2>/dev/null || true\n\
        sed -i \'s/--parallel=10/--parallel=1/g\' $PWD/node_modules/@fishawack/core/_Tasks/options/shell.js 2>/dev/null || true\n\
        echo "Replacing any npm variables with the full values"\n\
        perl -pi -e \'s/\$npm_package_vars_config/--prefix node_modules\/\@fishawack\/config-grunt\//g\' package.json 2>/dev/null || true\n\
        echo "Replace line in ftpscript that toggles passive which these days toggles it to off mode which breaks the docker setup"\n\
        sed -i "s/cmds.push \'passive\' if opts.passive/\#remove passive line/g" $PWD/node_modules/grunt-ftpscript/tasks/ftpscript.coffee 2>/dev/null || true\n\
        echo "Add line to browser-sync to enable polling as file events not fired on windows docker projects"\n\
        sed -i \'s/watchTask: true,/watchTask: true,watchOptions: { usePolling: true },port: process.env.PORT || 3000, ui: {port: +process.env.PORT_OPT || 3001},/g\' $PWD/node_modules/@fishawack/core/_Tasks/options/browserSync.js 2>/dev/null || true\n\
        sed -i \'s/watchTask: true,/watchTask: true,watchOptions: { usePolling: true },port: process.env.PORT || 3000, ui: {port: +process.env.PORT_OPT || 3001},/g\' $PWD/node_modules/@fishawack/config-grunt/_Tasks/options/browserSync.js 2>/dev/null || true\n\
        echo "Overwrite webdriver browsers to fix them to specific browser versions"\n\
        sed -i \'s/exports.config = {.*/exports.config = { seleniumArgs: { drivers: {chrome: { version: "105.0.5195.52" },firefox: { version: "0.28.0" }} },/\' $PWD/node_modules/@fishawack/core/wdio.conf.js 2>/dev/null || true\n\
        sed -i \'s/exports.config = {.*/exports.config = { seleniumArgs: { drivers: {chrome: { version: "105.0.5195.52" },firefox: { version: "0.28.0" }} },/\' $PWD/node_modules/@fishawack/config-grunt/wdio.conf.js 2>/dev/null || true\n\
        sed -i \'s/87.0.4280.88/105.0.5195.52/\' $PWD/node_modules/@fishawack/core/wdio.conf.js 2>/dev/null || true\n\
        sed -i \'s/puppeteer.launch({headless: true}).*/puppeteer.launch({headless: true, args: [ "--no-sandbox", "--disable-setuid-sandbox", "--disable-dev-shm-usage" ]}).then(async browser => {/\' $PWD/node_modules/sprinkle/bin/commands/screenshots.js 2>/dev/null || true\n\
        npx --no-install selenium-standalone --singleDriverInstall=chrome install --drivers.chrome.version=105.0.5195.52 2>/dev/null || true\n\
        npx --no-install selenium-standalone --singleDriverInstall=firefox install --drivers.firefox.version=0.28.0 2>/dev/null || true\n\
        export OVERRIDE=true\n\
    else\n\
        command npm "$@" || exit 1\n\
    fi\n\
)}\n\
export -f git\n\
export -f npm\n\
export bb="git@bitbucket.org:fishawackdigital"\n\
export gl="git@diggit01.fw.local:"\n\
export mm="git@github.com-mikemellor11:mikemellor11"\n\
export s="staging"\n\
export d="development"\n\
export m="master"\n\
export p="production"\n\
export vh="195.238.175.226"\n\
export ny1="167.99.153.125"\n\
export ny2="198.199.84.153"\n\
export gal="31.170.122.20"\n\
export grunt="grunt --gruntfile node_modules/@fishawack/core/Gruntfile.js"\n\
export gruntOld="grunt --gruntfile node_modules/@fishawack/config-grunt/Gruntfile.js"\n\
export PS1="\[\033[36m\]\u\[\033[m\]@\[\033[32m\]\h:\[\033[33;1m\]\w\[\033[m\]\[\033[31m\]\$(parse_git_branch)\[\033[00m\]\$ "\n\
export PATH="/root/.ebcli-virtual-env/executables:$PATH"\n\
alias ls="ls -l -GFh"\n\
# Load in enviroment variables if they exist in the root of the project\n\
if [ -f ./env.sh ]; then . ./env.sh; fi\n\
'\
>> ~/.profile

RUN mkdir ~/.ssh/

RUN echo $'\
Host *\n\
  StrictHostKeyChecking no\n\
  UserKnownHostsFile=/dev/null\n\
'\
>> ~/.ssh/config

# make /bin/sh symlink to bash instead of dash:
RUN echo "dash dash/sh boolean false" | debconf-set-selections
RUN DEBIAN_FRONTEND=noninteractive dpkg-reconfigure dash

WORKDIR /app

EXPOSE 3000
EXPOSE 3001

ENV LANG en_US.UTF-8
ENV LANGUAGE en_US:en
ENV LC_ALL en_US.UTF-8

COPY entrypoint.sh /bin/entrypoint.sh
RUN chmod +x /bin/entrypoint.sh
RUN dos2unix /bin/entrypoint.sh
ENTRYPOINT ["/bin/entrypoint.sh"]

CMD ["bash", "-l"]