# Debian Multiarch Dockerfile
#
# Author: Steffen Vogel <post@steffenvogel.de>
# SPDX-FileCopyrightText: 2014-2023 Institute for Automation of Complex Power Systems, RWTH Aachen University
# SPDX-License-Identifier: Apache-2.0

# You can choose between Debian and Ubuntu here
ARG DISTRO=debian
ARG DEBIAN_VERSION=bullseye
ARG ARCH=armhf
ARG TRIPLET=arm-linux-gnueabihf
ARG PREFIX=/app

FROM --platform=${BUILDPLATFORM} ${DISTRO}:${DEBIAN_VERSION} AS dev

ARG DISTRO
ARG ARCH
ARG TRIPLET
ARG PREFIX

ENV DEBIAN_FRONTEND=noninteractive

# Enable ${ARCH} architecture
RUN dpkg --add-architecture ${ARCH}

# Toolchain
RUN apt-get update && \
	apt-get install -y \
		crossbuild-essential-${ARCH} \
		pkg-config cmake make \
		autoconf automake autogen libtool \
		texinfo git curl tar wget diffutils \
		flex bison \
		protobuf-compiler protobuf-c-compiler \
		clang-format clangd

# Build-time dependencies
RUN apt-get update && \
	apt-get install -y \
		libssl-dev:${ARCH} \
		libgraphviz-dev:${ARCH} \
		libprotobuf-dev:${ARCH} \
		libprotobuf-c-dev:${ARCH} \
		uuid-dev:${ARCH} \
		libconfig-dev:${ARCH} \
		libnl-3-dev \
		libnl-route-3-dev:${ARCH} \
		libcurl4-openssl-dev:${ARCH} \
		libjansson-dev:${ARCH} \
		libzmq3-dev:${ARCH} \
		libnanomsg-dev:${ARCH} \
		librabbitmq-dev:${ARCH} \
		libmosquitto-dev:${ARCH} \
		librdkafka-dev:${ARCH} \
		libcomedi-dev:${ARCH} \
		libibverbs-dev:${ARCH} \
		librdmacm-dev:${ARCH} \
		libusb-1.0-0-dev:${ARCH} \
		liblua5.3-dev:${ARCH} \
		libhiredis-dev:${ARCH} \
		libmodbus-dev:${ARCH}

# Add local and 64-bit locations to linker paths
ENV echo /usr/local/lib >> /etc/ld.so.conf && \
	echo /usr/local/lib64 >> /etc/ld.so.conf

ADD cmake/toolchains/debian-${ARCH}.cmake /

ENV PKG_CONFIG_PATH=/usr/lib/${TRIPLET}/pkgconfig:/usr/local/lib/${TRIPLET}/pkgconfig

ENV CMAKE_EXTRA_OPTS="-DCMAKE_TOOLCHAIN_FILE=/debian-${ARCH}.cmake \
	-DCMAKE_BUILD_TYPE=Release \
	-DCMAKE_PREFIX_PATH=${PREFIX} \
	-DCMAKE_INSTALL_PREFIX=${PREFIX} \
	-DCMAKE_INSTALL_LIBDIR=${PREFIX}/lib/${TRIPLET} \
	-DLWS_INSTALL_LIB_DIR=${PREFIX}/lib/${TRIPLET}"

ENV CONFIGURE_OPTS="--libdir=${PREFIX}/lib/${TRIPLET}"

RUN mkdir -p /usr/local/lib/${TRIPLET}

ENV CPP=${TRIPLET}-cpp
ENV CC=${TRIPLET}-gcc
ENV CXX=${TRIPLET}-g++
ENV LD=${TRIPLET}-ld
ENV AS=${TRIPLET}-as
ENV AR=${TRIPLET}-ar
ENV RANLIB=${TRIPLET}-ranlib

RUN mkdir ${PREFIX}

# Install unpackaged dependencies from source
ADD packaging/deps.sh /
RUN bash deps.sh

# Expose ports for HTTP and WebSocket frontend
EXPOSE 80
EXPOSE 443

WORKDIR /villas

ENV LC_ALL C.UTF-8
ENV LANG C.UTF-8

FROM dev AS builder

ARG CMAKE_OPTS

COPY . /villas/

RUN rm -rf /villas/build && mkdir /villas/build
WORKDIR /villas/build

RUN cmake ${CMAKE_OPTS} ${CMAKE_EXTRA_OPTS} .. && \
	make -j$(nproc) install

FROM --platform=${TARGETPLATFORM} ${DISTRO}:${DEBIAN_VERSION} AS app

ARG DISTRO
ARG ARCH
ARG TRIPLET
ARG PREFIX

ENV DEBIAN_FRONTEND=noninteractive

# Enable ${ARCH} architecture
RUN dpkg --add-architecture ${ARCH}

# Run-time dependencies
RUN apt-get update && \
	apt-get install -y \
		libgomp1:${ARCH} \
		libprotobuf-c1:${ARCH} \
		libssl1.1:${ARCH} \
		libcgraph6:${ARCH} \
		libcdt5:${ARCH} \
		libgvc6:${ARCH} \
		libuuid1:${ARCH} \
		libconfig9:${ARCH} \
		libnl-3-200:${ARCH} \
		libnl-route-3-200:${ARCH} \
		libcurl4:${ARCH} \
		libjansson4:${ARCH} \
		libzmq5:${ARCH} \
		libnanomsg5:${ARCH} \
		librabbitmq4:${ARCH} \
		libmosquitto1:${ARCH} \
		librdkafka1:${ARCH} \
		libcomedi0:${ARCH} \
		libibverbs1:${ARCH} \
		librdmacm1:${ARCH} \
		libusb-1.0-0:${ARCH} \
		liblua5.3-0:${ARCH} \
		libhiredis0.14:${ARCH} \
		libmodbus5:${ARCH} && \
	rm -rf /var/lib/apt/lists/*

COPY --from=builder ${PREFIX} ${PREFIX}
RUN echo "${PREFIX}/lib" >> /etc/ld.so.conf && \
	echo "${PREFIX}/lib/${TRIPLET}" >> /etc/ld.so.conf && \
	ldconfig

ENV PATH=${PREFIX}/bin:${PATH}

# Test if it runs
RUN villas node -h 2>&1 > /dev/null

ENTRYPOINT ["villas"]

LABEL \
	org.label-schema.schema-version="1.0" \
	org.label-schema.name="VILLASnode" \
	org.label-schema.license="Apache-2.0" \
	org.label-schema.vendor="Institute for Automation of Complex Power Systems, RWTH Aachen University" \
	org.label-schema.author.name="Steffen Vogel" \
	org.label-schema.author.email="post@steffenvogel.de" \
	org.label-schema.description="A image containing all build-time dependencies for VILLASnode based on Fedora" \
	org.label-schema.url="http://fein-aachen.org/projects/villas-framework/" \
	org.label-schema.vcs-url="https://git.rwth-aachen.de/acs/public/villas/node" \
	org.label-schema.usage="https://villas.fein-aachen.org/doc/node-installation.html#node-installation-docker"
