# Alpine Base
FROM alpine:latest AS builder

# Staging area
RUN apk add --no-cache git cmake make clang build-base llvm-static llvm-dev clang-static clang-dev json-c-dev lua5.1-dev

RUN mkdir -p /opt/dev

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

# uci
RUN \
    cd /opt/dev && \
    git clone https://git.openwrt.org/project/uci.git && \
    cd uci && \
    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 && \
    cmake -DCMAKE_INSTALL_PREFIX=/usr -DCMAKE_BUILD_TYPE:String="Release" -DBUILD_LUA=OFF -DBUILD_EXAMPLES=OFF . && \
    make -j2 && \
    make install

# Integrate own applications
COPY src src
RUN make -C /src

# Final image
FROM alpine:latest

MAINTAINER Vivek Dutta "vivek.dutta@iopsys.eu"
ENV VERSION=devel
LABEL vendor=IOPSYS \
      org.opencontainers.image.version=$VERSION\
      org.opencontainers.image.vendor="IOPSYS" \
      org.opencontainers.image.description="IOWRT Application container"

RUN apk add --no-cache json-c libxml2 mosquitto mosquitto-clients

# Install own application
COPY --from=builder \
      /src/hello \
      /usr/bin/hello

COPY --from=builder \ 
      /usr/lib/libuci.so \
      /usr/lib/libblobmsg_json.so \
      /usr/lib/libubus.so \
      /usr/lib/libubox.so \
      /usr/lib/

COPY --from=builder \
       /usr/share/libubox/jshn.sh /usr/share/libubox/jshn.sh

COPY --from=builder \
       /usr/bin/ubus \
       /usr/bin/uci  \
       /usr/bin/jshn \
       /usr/bin/

CMD ["/bin/sh"]
