FROM ubuntu:20.04

LABEL maintainer="suru.dissanaike@iopsys.eu"
LABEL build="docker build -t code-analysis ."
LABEL run="docker run -it --entrypoint /bin/bash --rm -v ${PWD}:/opt/work code-analysis"

# docker build -t code-analysis .
# docker images
# docker tag <image id> iopsys/code-analysis:0.1
# docker push iopsys/code-analysis:0.x


RUN \
      apt-get update && DEBIAN_FRONTEND=noninteractive apt-get install -y \
      # general tools
      apt-utils \
      autoconf \
      automake \
      build-essential \
      clang-format \
      cmake \
      curl \
      gdb \
      gcovr \
      git \
      wget \
      iputils-ping \
      lcov \
      pkg-config \
      net-tools \
      strace \
      sudo \
      supervisor \
      valgrind \
      vim \
      # static code analysis
      unzip \
      python3 \
      python3-setuptools \
      default-jre \
      flawfinder
# Install dependent libraries
RUN \
      apt-get update && DEBIAN_FRONTEND=noninteractive apt-get install -y \
      lua5.1-dev \
      lua5.1 \
      libssl-dev \
      libuv1-dev \
      cmocka-doc \
      libcmocka-dev \
      libcmocka0 \
      libcurl4-openssl-dev\
      libfdt-dev \
      libnl-3-dev \
      libnl-genl-3-dev \
      libssl-dev \
      libnl-route-3-dev \
      libnfnetlink-dev \
      libpcre3-dev \
      libtool \
      libtrace-dev \
      zlib1g-dev  \
      # Install dependencies for obuspa
      sqlite3 \
      libsqlite3-dev \
      libc-ares-dev \
      libz-dev \
      # Install dependencies for ieee1905
      libpcap-dev

# Remove cached packages.
RUN rm -rf /var/lib/apt/lists/*
RUN mkdir /opt/dev
RUN mkdir /etc/config

# install cppcheck
ARG CPPCHECK_VERSION=2.7
RUN \
    cd /opt/dev && \
    git clone -b ${CPPCHECK_VERSION} --depth=1 https://github.com/danmar/cppcheck.git && \
    cd cppcheck && \
    mkdir build && \
    cd build && \
    cmake -j$(nproc) -DHAVE_RULES=ON -DUSE_MATCHCOMPILER=ON .. && \
    cmake --build . && \
    cmake --install . && \
    cd ../.. && \
    rm -rf cppcheck

# install node
ARG NODE_VERSION=14.16.1
ENV PATH="/root/.nvm/versions/node/v${NODE_VERSION}/bin/:${PATH}"
ENV NVM_DIR=/root/.nvm

RUN curl -o- https://raw.githubusercontent.com/nvm-sh/nvm/v0.38.0/install.sh | bash && \
    . "$NVM_DIR/nvm.sh" && \
    nvm install ${NODE_VERSION} && \
    nvm use v${NODE_VERSION} && \
    nvm alias default v${NODE_VERSION} && \
    npm install --global typescript yarn

# install tap-junit
RUN npm i -g tap-junit

# Install JSON-C
RUN \
      cd /opt/dev && \
      git clone https://github.com/json-c/json-c.git && \
      cd json-c && \
      git checkout 9021cdcdd01fc9dbcbe1f06391848c2ac915212f && \
      cmake . && \
      make && \
      make install && \
      sudo ldconfig

# ubox
RUN \
      cd /opt/dev && \
      git clone  git://git.openwrt.org/project/libubox.git && \
      cd libubox && mkdir build && cd build && \
      git checkout b14c4688612c05c78ce984d7bde633bce8703b1e && \
      cmake -DCMAKE_INSTALL_PREFIX=/usr -DCMAKE_BUILD_TYPE:String="Release" .. && \
      make -j2 && \
      make install

# uci
RUN \
      cd /opt/dev && \
      git clone git://git.openwrt.org/project/uci.git && \
      cd uci && \
      git checkout 52bbc99f69ea6f67b6fe264f424dac91bde5016c && \
      cmake -DCMAKE_INSTALL_PREFIX=/usr -DCMAKE_BUILD_TYPE:String="Release" -DBUILD_LUA=OFF . && \
      make -j2 && \
      make install

# ubus
RUN \
      cd /opt/dev && \
      git clone https://git.openwrt.org/project/ubus.git && \
      cd ubus && \
      git checkout 4fc532c8a55ba8217ad67d7fd47c5eb9a8aba044 && \
      cmake -DCMAKE_INSTALL_PREFIX=/usr -DCMAKE_BUILD_TYPE:String="Release" -DBUILD_LUA=OFF -DBUILD_EXAMPLES=OFF . && \
      make -j2 && \
      make install

# rpcd
RUN \
      cd /opt/dev && \
      git clone https://dev.iopsys.eu/fork/rpcd.git && \
      cd rpcd && \
      git checkout 52d280b20e00c9f80624e55cd37f6c68fb5e2389 && \
      cmake -DCMAKE_INSTALL_PREFIX=/usr -DCMAKE_BUILD_TYPE:String="Release" -DIWINFO_SUPPORT=NO . && \
      make -j2 && \
      make install && \
      mkdir /usr/lib/rpcd && \
      cp file.so /usr/lib/rpcd

# json-editor
RUN \
      cd /opt/dev && \
      git clone https://dev.iopsys.eu/iopsys/json-editor.git && \
      cd json-editor && \
      git checkout 44b32937a062ec4ffc9f7355841dc94ab6efa50f && \
      cmake -DCMAKE_INSTALL_PREFIX=/usr -DCMAKE_BUILD_TYPE:String="Release" . && \
      make && \
      sudo make install

# nlohmann
RUN \
     cd /opt/dev && \
     git clone https://github.com/nlohmann/json.git && \
     cd json && \
     git checkout be61ad147000be4802173d0d118fd1b8109b4668 && \
     mkdir build && \
     cd build && \
     cmake .. && \
     cmake --build . && \
     sudo make install

#  json-schema-validator-cpp
RUN \
     cd /opt/dev && \
     git clone https://github.com/pboettch/json-schema-validator.git json-schema-validator-cpp && \
     cd json-schema-validator-cpp && \
     git checkout 3ec0e69a0bce7524f12a865b153b71d0d7ef2484 && \
     mkdir build && \
     cd build && \
     cmake .. -DBUILD_SHARED_LIBS=ON && \
     make && \
     sudo make install && \
     sudo ldconfig


# iopsys json-schema-validator
RUN \
      cd /opt/dev && \
      git clone https://dev.iopsys.eu/iopsys/json-schema-validator.git && \
      cd json-schema-validator && \
      git checkout devel && \
      cmake -DCMAKE_INSTALL_PREFIX=/usr -DCMAKE_BUILD_TYPE:String="Release" . && \
      make && \
      sudo make install && \
      sudo ldconfig

# tap
RUN \
      cd /opt/dev && \
      git clone https://github.com/zorgnax/libtap.git && \
      cd libtap && \
      make && \
      sudo make install && \
      sudo ldconfig

# git ubus-api-validator
RUN \
      cd /opt/dev && \
      git clone https://dev.iopsys.eu/iopsys/ubus-api-validator.git && \
      cd ubus-api-validator && \
      make && \
      sudo make install

#install cpd
WORKDIR /home/user
RUN \
      wget https://github.com/pmd/pmd/releases/download/pmd_releases%2F6.19.0/pmd-bin-6.19.0.zip && \
      unzip pmd-bin-6.19.0.zip

# microxml
RUN \
	cd /opt/dev && \
	git clone https://dev.iopsys.eu/mirror/microxml.git && \
	cd microxml && \
	autoreconf -i; \
	cd /opt/dev/microxml && \
	./configure --prefix=/usr --disable-threads --enable-shared --enable-static && \
	make && \
	sudo make install && \
	sudo ln -sf /usr/lib/libmicroxml.so.1.0 /lib/libmicroxml.so && \
	sudo ln -sf /usr/lib/libmicroxml.so.1.0 /lib/libmicroxml.so.1

WORKDIR /builds/iopsys/

# Prepare supervisor
RUN mkdir -p /var/log/supervisor
RUN mkdir -p /etc/supervisor/conf.d/
COPY supervisord.conf /etc/supervisor/

# Prepare JSON Schemas
RUN mkdir -p /usr/share/rpcd/schemas
RUN mkdir -p /usr/share/rpcd/definitions

RUN node --version
RUN npm --version

# Start entrypoint
COPY entrypoint.sh /usr/local/bin/entrypoint.sh
ENTRYPOINT ["/usr/local/bin/entrypoint.sh"]