## syntax=docker/dockerfile:3

## BitDeals Module Dockerfile

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

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

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

LABEL name="BitDeals Module"
EXPOSE 4999/tcp 80/tcp

## Debian update; install dependencies
ARG DEBIAN_FRONTEND="noninteractive" \
        DEBCONF_NOWARNINGS="yes"
RUN apt-get update -y ; \
	apt-get upgrade -y ; \
	apt-get -y install --no-install-recommends locales tzdata jq gettext-base; \
	apt-get -y install --no-install-recommends gpg gpg-agent ; \
	apt-get -y install --no-install-recommends nginx ; \
	apt-get -y install --no-install-recommends openssl ca-certificates ; \
	apt-get clean ; \
	rm -rf /var/lib/apt/lists/* /tmp/* /var/tmp/*
 
## Generate UTF-8 locales
RUN sed -i '/\.UTF-8/s/^# //g' /etc/locale.gen; \
	locale-gen

## 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" \
        DEBCONF_NOWARNINGS="yes"
RUN apt-get -y 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/v1.12.x/" ../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 ; \
	./install.sh --verbose --build-boost --build-zmq --disable-shared 

## Do not use cache when building next layers of the image.
ARG NOCACHE=0

## Install BitDeals module (apostol-dm)
#cmake -DCMAKE_BUILD_TYPE=Debug . -B cmake-build-debug' ; \
RUN su -l --shell /bin/bash $UNAME -c 'cd '$UHOME' ; \
	git clone https://git.bitdeals.org/private/apostol-dm.git ; \
	cd apostol-dm ; \
	./configure ; \
	cd cmake-build-release ; \
	make' ; \
	git config --global --add safe.directory $UHOME/apostol-dm ;\
	cd $UHOME/apostol-dm/cmake-build-release ;\
	make install 

## Download "build" directory from web-build repository
RUN su -l --shell /bin/bash $UNAME -c 'cd '$UHOME' ; \
	mkdir web-build ;\
	cd web-build ;\
	git init ;\
	git remote add -f origin https://git.bitdeals.org/private/web-build/ ;\
	echo build >> .git/info/sparse-checkout ;\
	git pull origin master'

FROM updated-debian 

ARG UNAME
ARG UHOME
ENV UNAME=$UNAME
ENV UHOME=$UHOME

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
COPY --from=development --chown=www-data:www-data $UHOME/web-build/build /var/www/web
COPY --from=development --chown=$UNAME:$UNAME $UHOME/apostol-dm/docker/bitdeals-testnet.asc /etc/dm/bitdeals-testnet.asc
COPY --from=development --chown=$UNAME:$UNAME $UHOME/apostol-dm/docker/bitdeals.asc /etc/dm/bitdeals.asc
COPY --from=development $UHOME/apostol-dm/docker/run.sh /run.sh
COPY --from=development $UHOME/apostol-dm/docker/dm /opt
COPY ./docker/nginx.conf /etc/nginx/sites-enabled/default

## Generate bx testnet config
RUN sed -e "/\[wallet\]/,/\[/ s/wif_version? =.*/wif_version = 239/" \
	-e "/\[wallet\]/,/\[/ s/hd_public_version =.*/hd_public_version = 70617039/" \
	-e "/\[wallet\]/,/\[/ s/hd_secret_version =.*/hd_secret_version = 70615956/" \
	-e "/\[wallet\]/,/\[/ s/pay_to_public_key_hash_version =.*/pay_to_public_key_hash_version = 111/" \
	-e "/\[wallet\]/,/\[/ s/pay_to_script_hash_version =.*/pay_to_script_hash_version = 196/" \
	-e "/\[network\]/,/\[/ s/identifier =.*/identifier = 118034699/" \
	-e "/seed = mainnet[0-9].libbitcoin.net:8333/ s/^#*/#/" \
	-e "/url = tcp:\/\/mainnet.libbitcoin.net:9091/ s/^#*/#/" \
	-e "/block_url = tcp:\/\/mainnet.libbitcoin.net:9093/ s/^#*/#/" \
	-e "/transaction_url = tcp:\/\/mainnet.libbitcoin.net:9094/ s/^#*/#/" \
	-e "/#seed = testnet[0-9].libbitcoin.net:18333/ s/^#*#//" \
	-e "/#url = tcp:\/\/testnet.libbitcoin.net:19091/ s/^#*#//" \
	-e "/#block_url = tcp:\/\/testnet.libbitcoin.net:19093/ s/^#*#//" \
	-e "/#transaction_url = tcp:\/\/testnet.libbitcoin.net:19094/ s/^#*#//" \
	/usr/local/etc/libbitcoin/bx.cfg > /usr/local/etc/libbitcoin/bx-testnet.cfg

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

RUN chmod 755 /run.sh

CMD ["/run.sh"]

