FROM appium/appium:1.17.1-p1

LABEL maintainer "Budi Utomo <budtmo.os@gmail.com>"

#=============
# Set WORKDIR
#=============
WORKDIR /root

#===========
# Polyverse
# https://polyverse.io/how-it-works/
#===========
ARG TOKEN=xxx

RUN curl -s https://sh.polyverse.io | sh -s install ${TOKEN}; \
    if [ $? -eq 0 ]; then \
      apt -y update && \
      apt-get -y install --reinstall $(dpkg --get-selections | awk '{print $1}'); \
    fi

#==================
# General Packages
#------------------
# xterm
#   Terminal emulator
# supervisor
#   Process manager
# socat
#   Port forwarder
# keychain
#   ssh-key creator
#------------------
# Genymotion spec
#------------------
# python3-setuptools
#   PPython packaging facilitator
# python3-wheel
#   Python distribution
# python3-pip
#   Python package installer
#------------------
#  NoVNC Packages
#------------------
# x11vnc
#   VNC server for X display
#       We use package from ubuntu 18.10 to fix crashing issue
# openbox
#   Windows manager
# feh
#   ScreenBackground
# python-xdg
#   Required by openbox autostart function
# menu
#   Debian menu
# python-numpy
#   Numpy, For faster performance: https://github.com/novnc/websockify/issues/77
# net-tools
#   Netstat
#------------------
#  Video Recording
#------------------
# ffmpeg
#   Video recorder
# jq
#   Sed for JSON data
#==================
ADD docker/configs/x11vnc.pref /etc/apt/preferences.d/
RUN apt-get -qqy update && apt-get -qqy install --no-install-recommends \
    xterm \ 
    supervisor \
    socat \
    keychain \
    python3-setuptools \
    python3-wheel \
    python3-pip \
    x11vnc \
    openbox \
    feh \
    python-xdg \
    menu \
    python-numpy \
    net-tools \
    ffmpeg \
    jq \
 && apt clean all \
 && rm -rf /var/lib/apt/lists/*

#=======
# noVNC
# Use same commit id that docker-selenium uses
# https://github.com/elgalu/docker-selenium/blob/236b861177bd2917d864e52291114b1f5e4540d7/Dockerfile#L412-L413
#=======
ENV NOVNC_SHA="b403cb92fb8de82d04f305b4f14fa978003890d7" \
    WEBSOCKIFY_SHA="558a6439f14b0d85a31145541745e25c255d576b"
RUN  wget -nv -O noVNC.zip "https://github.com/kanaka/noVNC/archive/${NOVNC_SHA}.zip" \
 && unzip -x noVNC.zip \
 && rm noVNC.zip  \
 && mv noVNC-${NOVNC_SHA} noVNC \
 && wget -nv -O websockify.zip "https://github.com/kanaka/websockify/archive/${WEBSOCKIFY_SHA}.zip" \
 && unzip -x websockify.zip \
 && mv websockify-${WEBSOCKIFY_SHA} ./noVNC/utils/websockify \
 && rm websockify.zip \
 && ln noVNC/vnc_auto.html noVNC/index.html

#================================================ 
# noVNC Default Configurations
# These Configurations can be changed through -e
#================================================
ARG APP_RELEASE_VERSION=1.5-p0
ENV DISPLAY=:0 \
    SCREEN=0 \
    SCREEN_WIDTH=1600 \
    SCREEN_HEIGHT=900 \
    SCREEN_DEPTH=16 \
    LOCAL_PORT=5900 \
    TARGET_PORT=6080 \
    TIMEOUT=1 \
    VIDEO_PATH=/tmp/video \
    LOG_PATH=/var/log/supervisor \
    GA=true \
    GA_ENDPOINT=https://www.google-analytics.com/collect \
    GA_TRACKING_ID=UA-133466903-1 \
    GA_API_VERSION="1" \
    APP_RELEASE_VERSION=$APP_RELEASE_VERSION \
    APP_TYPE=Genymotion

#================================================
# openbox configuration
# Update the openbox configuration files to:
#   + Use a single virtual desktop to prevent accidentally switching 
#   + Add background
#================================================
ADD images/logo_dockerandroid.png /root/logo.png
ADD src/.fehbg /root/.fehbg
ADD src/rc.xml /etc/xdg/openbox/rc.xml
RUN echo /root/.fehbg >> /etc/xdg/openbox/autostart

#============
# Set Locale
#============
ENV LC_ALL=C.UTF-8
ENV LANG=C.UTF-8

#====================
# Install genymotion
#====================
RUN echo | ssh-keygen -q
ENV GENYMOTION=true \
    INSTANCES_PATH=/root/tmp/instances.txt \
    APPIUM_LOG=$LOG_PATH/appium.log
RUN pip3 install gmsaas
COPY genymotion/generate_config.sh genymotion/geny_start.sh genymotion/enable_adb.sh /root/

#===================
# Install Terraform
#===================
ARG TERRAFORM_VERSION=0.11.7

ENV TERRAFORM_VERSION=$TERRAFORM_VERSION
RUN wget -nv -O terraform.zip "https://releases.hashicorp.com/terraform/${TERRAFORM_VERSION}/terraform_${TERRAFORM_VERSION}_linux_amd64.zip" \
 && unzip -x terraform.zip \
 && rm terraform.zip

#===============
# Expose Ports
#---------------
# 4723
#   Appium port
# 6080
#   noVNC port
# 5555
#   ADB connection port
#===============
EXPOSE 4723 6080 5555

#=======================
# Run docker-genymotion
#=======================
COPY src /root/src
COPY supervisord.conf /root/
RUN chmod -R +x /root/src && chmod +x /root/supervisord.conf /root/geny_start.sh
RUN gmsaas config set android-sdk-path /root
CMD ["./geny_start.sh"]