# Copyright (c) 2019 Red Hat, Inc.
# This program and the accompanying materials are made
# available under the terms of the Eclipse Public License 2.0
# which is available at https://www.eclipse.org/legal/epl-2.0/
#
# SPDX-License-Identifier: EPL-2.0
#
# Contributors:
#   Red Hat, Inc. - initial API and implementation

FROM registry-proxy.engineering.redhat.com/rh-osbs/codeready-workspaces-theia-rhel8:2.2 as builder

WORKDIR /home/theia

# Apply node libs installed globally to the PATH
ENV PATH=${HOME}/.yarn/bin:${PATH}

# setup extra stuff
USER root
ENV YARN_FLAGS="--offline"
ENV NEXE_FLAGS="--asset ${HOME}/pre-assembly-nodejs-static"
COPY asset-theia-endpoint-runtime-pre-assembly-nodejs-static.tar.gz asset-theia-endpoint-runtime-binary-yarn.tar.gz asset-node-src.tar.gz /tmp/
RUN tar xzf /tmp/asset-theia-endpoint-runtime-binary-yarn.tar.gz -C / && rm -f /tmp/asset-theia-endpoint-runtime-binary-yarn.tar.gz && \
    export NODE_VERSION=$(node --version | sed -s 's/v//') && mkdir -p "/home/theia/.nexe/${NODE_VERSION}" && tar zxf /tmp/asset-node-src.tar.gz --strip-components=1 -C "/home/theia/.nexe/${NODE_VERSION}" && \
    tar zxf /tmp/asset-theia-endpoint-runtime-pre-assembly-nodejs-static.tar.gz -C "/home/theia/"

RUN yum install -y git make cmake gcc gcc-c++ python2 automake autoconf which glibc-devel && \
    yum -y clean all && rm -rf /var/cache/yum && ln -s /usr/bin/python2 /usr/bin/python
RUN yarn global add ${YARN_FLAGS} nexe@3.3.2
RUN nexe -v && \
    # Build remote binary with node runtime 10.16.0 and che-theia node dependencies. nexe icludes to the binary only
    # necessary dependencies.
    nexe node_modules/@eclipse-che/theia-remote/lib/node/plugin-remote.js ${NEXE_FLAGS} -o ${HOME}/plugin-remote-endpoint

# Light image without node. We include remote binary to this image.
# https://access.redhat.com/containers/?tab=tags#/registry.access.redhat.com/ubi8-minimal
FROM ubi8-minimal:8.2-301.1592810506 as runtime
USER 0
RUN microdnf update -y gnutls nodejs npm kernel-headers systemd && microdnf clean all && rm -rf /var/cache/yum && \
    echo "Installed Packages" && rpm -qa | sort -V && echo "End Of Installed Packages"
ENV SUMMARY="Red Hat CodeReady Workspaces - theia-endpoint container" \
    DESCRIPTION="Red Hat CodeReady Workspaces - theia-endpoint container" \
    PRODNAME="codeready-workspaces" \
    COMPNAME="theia-endpoint-rhel8" 

LABEL summary="$SUMMARY" \
      description="$DESCRIPTION" \
      io.k8s.description="$DESCRIPTION" \
      io.k8s.display-name="$DESCRIPTION" \
      io.openshift.tags="$PRODNAME,$COMPNAME" \
      com.redhat.component="$PRODNAME-$COMPNAME-container" \
      name="$PRODNAME/$COMPNAME" \
      version="2.2" \
      license="EPLv2" \
      maintainer="Nick Boldt <nboldt@redhat.com>" \
      io.openshift.expose-services="" \
      usage=""

COPY --from=builder /home/theia/plugin-remote-endpoint /plugin-remote-endpoint

ENTRYPOINT cp -rf /plugin-remote-endpoint /remote-endpoint/plugin-remote-endpoint


# insert generated LABELs below this line
LABEL \
      jenkins.tarball.url="/view/CRW_CI/view/Pipelines \
      #673 \
      / \
      asset-node-src.tar.gz \
      asset-theia-endpoint-runtime-binary-yarn.tar.gz \
      asset-theia-endpoint-runtime-pre-assembly-nodejs-static.tar.gz" \
      jenkins.build.number="673"
