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

ARG DISTRO=fedora
ARG FEDORA_VERSION=36
ARG REF=unknown

FROM ${DISTRO}:${FEDORA_VERSION} AS dev

ARG DISTRO

# Toolchain
RUN dnf -y install \
	gcc gcc-c++ \
	pkgconfig cmake make \
	autoconf automake autogen libtool \
	texinfo git curl tar \
	flex bison \
	protobuf-compiler protobuf-c-compiler \
	clang-tools-extra

# Several tools only needed for developement and testing
RUN dnf -y install \
	openssh-clients \
	jq nmap-ncat \
	iproute iproute-tc \
	python python-devel python-pip \
	gdb gdb-gdbserver \
	cppcheck \
	xmlto dblatex rubygem-asciidoctor \
	psmisc procps-ng \
	rabbitmq-server mosquitto

# Tools for debugging, coverage, profiling
RUN pip install \
	gcovr \
	protobuf

# Dependencies
RUN dnf -y install \
	openssl-devel \
	libuuid-devel \
	libconfig-devel \
	libwebsockets-devel \
	libcurl-devel \
	jansson-devel \
	spdlog-devel \
	fmt-devel \
	libnl3-devel \
	graphviz-devel \
	protobuf-devel \
	protobuf-c-devel \
	zeromq-devel \
	nanomsg-devel \
	librabbitmq-devel \
	mosquitto-devel \
	librdkafka-devel \
	libibverbs-devel \
	librdmacm-devel \
	libusb-devel \
	lua-devel \
	hiredis-devel \
	libnice-devel \
	libmodbus-devel

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

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

# Workaround for libnl3's search path for netem distributions
RUN ln -s /usr/lib64/tc /usr/lib/tc

COPY ./python /python
RUN python -m venv /venv && \
	source /venv/bin/activate && \
	pip install /python[dev] && \
	rm -r /python

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

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

WORKDIR /villas

FROM dev AS dev-vscode

# create a non-root user for vscode to use
ARG USERNAME=villas
ARG USER_UID=1000
ARG USER_GID=$USER_UID

RUN groupadd --gid $USER_GID $USERNAME \
	&& useradd --uid $USER_UID --gid $USER_GID -m $USERNAME \
	&& echo $USERNAME ALL=\(root\) NOPASSWD:ALL > /etc/sudoers.d/$USERNAME \
	&& chmod 0440 /etc/sudoers.d/$USERNAME

FROM dev AS app

ARG CMAKE_OPTS

COPY . /villas/

WORKDIR /villas/build
RUN --mount=type=cache,id=${DISTRO}-${FEDORA_VERSION}-${ARCH}-${REF},target=/villas/build \
	cmake ${CMAKE_OPTS} .. && \
	make -j$(nproc) install && \
	ldconfig

WORKDIR /villas

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"
