FROM ghcr.io/tpm2-software/ubuntu-20.04:latest AS base

FROM base AS rust-toolchain
# Install Rust toolchain
RUN (curl https://sh.rustup.rs -sSf || exit 1) | bash -s -- -y
ENV PATH="/root/.cargo/bin:${PATH}"

FROM rust-toolchain AS tpm2-tss
# Download and install the TSS library
ENV TPM2_TSS_BINDINGS_VERSION=4.1.3
ARG TPM2_TSS_VERSION=$TPM2_TSS_BINDINGS_VERSION
ENV TPM2_TSS_VERSION=$TPM2_TSS_VERSION
ENV PKG_CONFIG_PATH=/usr/local/lib/pkgconfig

RUN git clone https://github.com/tpm2-software/tpm2-tss.git --branch $TPM2_TSS_VERSION
RUN cd tpm2-tss \
	&& ./bootstrap \
	&& ./configure \
	&& make -j$(nproc) \
	&& make install \
	&& ldconfig

FROM tpm2-tss AS tpm2-tools
# Download and install TPM2 tools
RUN git clone https://github.com/tpm2-software/tpm2-tools.git --branch 5.7
RUN cd tpm2-tools \
	&& ./bootstrap \
	&& ./configure \
	&& make install

FROM tpm2-tools AS tpm2-tss-install-dir
# TPM2_TSS_PATH is the env variable build.rs looks for
# an installation.
ENV TPM2_TSS_PATH=/tpm2-tss-install-dir
RUN mkdir -p /tpm2-tss-install-dir/lib /tpm2-tss-install-dir/include
COPY --from=tpm2-tss tpm2-tss/src/tss2-*/.libs/libtss2-*.so /tpm2-tss-install-dir/lib/
COPY --from=tpm2-tss tpm2-tss/include/ /tpm2-tss-install-dir/include/
COPY --from=tpm2-tss tpm2-tss/VERSION /tpm2-tss-install-dir/