# syntax=docker/dockerfile:3

## BitDeals Module Dockerfile

## the Module user
ARG UNAME="module"
ARG UHOME="/home/$UNAME"

## Base image
FROM debian:bullseye AS updated-debian

SHELL ["/bin/bash", "-exo", "pipefail", "-c"]

LABEL name="BitDeals Module"
EXPOSE 4999 80

## Debian update; install dependencies for GPG
ARG DEBIAN_FRONTEND=noninteractive
RUN apt-get update ; \
	apt-get upgrade -y ; \
	apt-get -y install --no-install-recommends gpg gpg-agent ; \
	apt-get clean ; \
	rm -rf /var/lib/apt/lists/* /tmp/* /var/tmp/*
 
## Add the Module user
ARG UNAME
ARG UHOME
RUN useradd --create-home --home-dir $UHOME $UNAME 

FROM updated-debian AS development

## Debian update; install dependencies for: C++ compiler; OpenPGP; Libbitcoin
ARG DEBIAN_FRONTEND=noninteractive
RUN apt-get update 
RUN apt-get -y install --no-install-recommends build-essential libssl-dev libcurl4-openssl-dev make cmake gcc g++ git 
RUN apt-get -y install --no-install-recommends libgmp-dev libbz2-dev libzip-dev 
RUN apt-get -y install --no-install-recommends autoconf automake libtool pkg-config wget 

## Avoid error: "Server Certificate Verification Failed. CRLfile: none"
RUN apt-get install -y --reinstall ca-certificates 

ARG UNAME
ARG UHOME

## Install OpenPGP
RUN su -l --shell /bin/bash  $UNAME -c 'git clone https://github.com/calccrypto/OpenPGP ; \
	mkdir OpenPGP/build ; cd OpenPGP/build ; \
	sed -i "s/master/main/" ../contrib/cmake/GoogleTest.txt.in ; \
	cmake -DUSE_OPENSSL=ON -DGPG_COMPATIBLE=ON .. ; \
	make' ; \
	cd $UHOME/OpenPGP/build/ ; \
	make install 

## Install Yaml-cpp
RUN su -l --shell /bin/bash $UNAME -c 'git clone https://github.com/jbeder/yaml-cpp ; \
	mkdir yaml-cpp/build ; cd yaml-cpp/build ; \
	cmake .. ; \
	make' ; \
	cd $UHOME/yaml-cpp/build/ ; \
	make install 

## Install Libbitcoin explorer
RUN su -l --shell /bin/bash $UNAME -c 'wget https://raw.githubusercontent.com/libbitcoin/libbitcoin-explorer/version3/install.sh ; \
	chmod +x install.sh' ; \
	cd $UHOME ; \
	sed -i '/git clone / s/git.*$/while true; do & \&\& break; done/' install.sh ; \
	sed -i '/wget / s/wget.*$/while true; do & \&\& break; done/' install.sh ; \
	./install.sh --build-boost --build-zmq --disable-shared 

## Install BitDeals dm
RUN su -l --shell /bin/bash $UNAME -c 'cd '$UHOME' ; \
	git clone https://bitbucket.org/bitdeals/apostol-dm.git ; \
	cd apostol-dm ; \
	./configure ; \
	cd cmake-build-release ; \
	make' ; \
	cd $UHOME/apostol-dm/cmake-build-release ;\
	make install 

FROM updated-debian 

COPY --from=development /usr/local/ /usr/local/
COPY --from=development /etc/dm/ /etc/dm/
COPY --from=development /usr/sbin/dm /usr/sbin/dm
COPY --from=development /etc/init.d/dm /etc/init.d/dm

#dm: error while loading shared libraries: libOpenPGP.so: cannot open shared object file: No such file or directory
RUN ldconfig

## Copy configuration helper script
#ADD https://bitbucket.org/bitdeals/apostol-dm/raw/master/docker/entrypoint.sh /entrypoint.sh
COPY ./entrypoint.sh /entrypoint.sh
RUN chmod 755 /entrypoint.sh

ENTRYPOINT ["/entrypoint.sh"]

