FROM ubuntu:18.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 \
      libtool \
      pkg-config \
      net-tools \
      strace \
      sudo \
      supervisor \
      valgrind \
      vim \
      # static code analysis
      unzip \
      python3 \
      python3-setuptools \
      default-jre \
      cppcheck \
      flawfinder \
      libcurl4-openssl-dev

# 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 \
      libnl-3-dev \
      libnl-genl-3-dev \
      libssl-dev \
      libnl-route-3-dev \
      libnfnetlink-dev \
      libtrace-dev \
      zlib1g-dev

# Remove cached packages.
RUN rm -rf /var/lib/apt/lists/*

RUN mkdir /opt/dev
RUN mkdir /etc/config

# install node
ENV NODE_VERSION=12.14.1
RUN curl -o- https://raw.githubusercontent.com/creationix/nvm/v0.34.0/install.sh | bash
ENV NVM_DIR=/root/.nvm
RUN . "$NVM_DIR/nvm.sh" && nvm install ${NODE_VERSION}
RUN . "$NVM_DIR/nvm.sh" && nvm use v${NODE_VERSION}
RUN . "$NVM_DIR/nvm.sh" && nvm alias default v${NODE_VERSION}
ENV PATH="/root/.nvm/versions/node/v${NODE_VERSION}/bin/:${PATH}"
RUN node --version
RUN npm --version

# 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 && \
      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 fd57eea9f37e447814afbf934db626288aac23c4 && \
      cmake -DCMAKE_INSTALL_PREFIX=/usr -DCMAKE_BUILD_TYPE:String="Release" .. && \
      make -j2 && \
      make install

# uci
RUN \
      cd /opt/dev && \
      git clone git://nbd.name/uci.git && \
      cd uci && \
      git checkout a536e300370cc3ad7b14b052b9ee943e6149ba4d && \
      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 5f87f5480ebf004d735dbf44259d08cf8affd305 && \
      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://git.openwrt.org/project/rpcd.git && \
      cd rpcd && \
      git checkout cfe1e75c91bc1bac82e6caab3e652b0ebee59524 && \
      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

# libwifi and libdsl
RUN \
      cd /opt/dev && \
      rm -fr easy-soc-libs && \
      git clone https://dev.iopsys.eu/iopsys/easy-soc-libs.git && \
      cd easy-soc-libs && \
      git checkout 7264e5d9d8ebf21e6f4ffabf474bdb17c71a5bb8 && \
      cd libeasy && \
      make CFLAGS+="-I/usr/include/libnl3" && \
      mkdir -p /usr/include/easy && \
      cp easy.h event.h utils.h /usr/include/easy && \
      cp -a libeasy*.so* /usr/lib && \
      cd ../libwifi && \
      make PLATFORM=TEST && \
      cp wifi.h /usr/include && \
      cp -a libwifi-5.so* /usr/lib && \
      cd ../libdsl && \
      make PLATFORM=TEST && \
      cp common.h xdsl.h xtm.h /usr/include && \
      cp -a libdsl*.so* /usr/lib && \
      sudo ldconfig

# libwebsockets
RUN \
     cd /opt/dev && \
     git clone https://github.com/warmcat/libwebsockets.git && \
     cd libwebsockets && \
     git checkout 89eedcaa94e1c8a97ea3af10642fd224bcea068f && \
     mkdir build && \
     cd build && \
     cmake .. -DCMAKE_SKIP_RPATH=TRUE -DLWS_IPV6=ON -DLWS_WITHOUT_TESTAPPS=ON -DLWS_WITH_SSL=ON -DLWS_OPENSSL_CLIENT_CERTS=/etc/ssl/certs -DLWS_OPENSSL_SUPPORT=ON -DLWS_WITH_SSL=ON -DLWS_WITH_LIBUV=ON -DLWS_WITH_PLUGINS=ON -DLWS_WITH_SERVER_STATUS=ON -DLWS_WITH_ACCESS_LOG=ON -DLWS_WITH_CGI=ON && \
     make && \
     sudo make install && \
     sudo ldconfig


# 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 --enable-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

#bbf	
RUN \
	cd /opt/dev && \
	git clone https://dev.iopsys.eu/iopsys/bbf.git &&\ 
	cd /opt/dev/bbf && \
	autoreconf -i && \
	./configure --enable-tr181 --enable-tr104 --enable-tr143 --enable-tr157 CPPFLAGS=-DCUSTOM_PREFIX=\\\"X_IOPSYS_EU_\\\" && \
	make && \
	cp bin/.libs/libbbfdm.so* /usr/lib/ && \
	cp bin/.libs/libbbf_api.so* /usr/lib/ && \
	mkdir /usr/include/libbbfdm && \
	mkdir /usr/include/libbbf_api && \
	cp *.h /usr/include/libbbfdm/ && \
	cp dmtree/tr181/*.h /usr/include/libbbfdm/ && \
	cp dmtree/tr157/*.h /usr/include/libbbfdm/ && \
	cp libbbf_api/*.h /usr/include/libbbf_api/

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"]