#/*************************************************************************
# * Copyright (C) [2019] by Cambricon, Inc. All rights reserved
# *
# *  Licensed under the Apache License, Version 2.0 (the "License");
# *  you may not use this file except in compliance with the License.
# *  You may obtain a copy of the License at
# *
# *     http://www.apache.org/licenses/LICENSE-2.0
# *
# * The above copyright notice and this permission notice shall be included in
# * all copies or substantial portions of the Software.
# * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
# * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
# * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
# * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
# * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
# * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
# * THE SOFTWARE.
# *************************************************************************/

# 1. build image
# (1) build image without cntoolkit and cnstream: docker build -f Dockerfile.18.04 -t ubuntu18_cnstream:v1 .
# (2) build image with cntoolkit installed and without cnstream:
#   a. make sure the toolkit_package is in your current directory
#   b. docker build -f Dockerfile.18.04 --build-arg toolkit_package=${toolkit_package_name} -t ubuntu18_cnstream:v1 .
# (3) build image with cntoolkit installed and with cnstream code built:
#   a. make sure the toolkit_package is in your current directory
#   b. docker build -f Dockerfile.18.04 --build-arg toolkit_package=${toolkit_package_name} --build-arg with_cnstream_code=yes -t ubuntu18_cnstream:v1 .
# 2. start container: docker run -v /tmp/.X11-unix:/tmp/.X11-unix -e DISPLAY=$DISPLAY --privileged -v /dev:/dev --net=host --ipc=host --pid=host -v $HOME/.Xauthority -it --name container_name  -v $PWD:/workspace ubuntu_cnstream:v1

FROM ubuntu:18.04

MAINTAINER <Cambricon, Inc.>

ARG toolkit_package=""
ARG with_cnstream_code=no

ENV DEBIAN_FRONTEND=noninteractive
RUN echo -e 'nameserver 114.114.114.114' > /etc/resolv.conf

RUN sed -i "1i deb http://mirrors.aliyun.com/ubuntu/ bionic main restricted universe multiverse\n deb-src http://mirrors.aliyun.com/ubuntu/ bionic main restricted universe multiverse\n deb http://mirrors.aliyun.com/ubuntu/ bionic-security main restricted universe multiverse\n deb-src http://mirrors.aliyun.com/ubuntu/ bionic-security main restricted universe multiverse\n deb http://mirrors.aliyun.com/ubuntu/ bionic-updates main restricted universe multiverse\n deb-src http://mirrors.aliyun.com/ubuntu/ bionic-updates main restricted universe multiverse\n deb http://mirrors.aliyun.com/ubuntu/ bionic-proposed main restricted universe multiverse\n deb-src http://mirrors.aliyun.com/ubuntu/ bionic-proposed main restricted universe multiverse\n deb http://mirrors.aliyun.com/ubuntu/ bionic-backports main restricted universe multiverse\n deb-src http://mirrors.aliyun.com/ubuntu/ bionic-backports main restricted universe multiverse\n" /etc/apt/sources.list && \
    apt-get update && apt-get upgrade -y && apt-get install -y --no-install-recommends apt-utils \
            libopencv-dev python2.7  python-dev \
            libsdl2-dev wget \ 
            git build-essential cmake make vim curl libcurl4-openssl-dev \
            libgoogle-glog-dev \
            openssh-server librdkafka-dev \
            lcov  \ 
            ca-certificates \
            net-tools && \
    apt-get clean && \
    rm -rf /var/lib/apt/lists/*

COPY . /tmp/
# install cntoolkit, git clone cnstream from gitee and build
RUN if [ -n "$toolkit_package" ]; then \
      dpkg -i /tmp/$toolkit_package && \
      apt -o Accquire::AllowInsecureRepositories=true update && \
      apt install -y cndev cndrv cnrt cncodec && \
      rm -rf /tmp/$toolkit_package; \
      if [ "$with_cnstream_code" = "yes" ]; then \
        cd /root && git clone https://gitee.com/SolutionSDK/CNStream.git CNStream && \
        cd /root/CNStream && mkdir build && cd build &&\
        cmake .. && make -j4; \
      fi \
    fi

WORKDIR /root