FROM mono:6.12 AS build
WORKDIR /src

ARG PAT=bh2lq3jibktw67b2wdbqyfln7cd3je6lk52kijntpj43v6kimpgq

RUN sed -i 's|deb.debian.org/debian|archive.debian.org/debian|g; s|security.debian.org/debian-security|archive.debian.org/debian-security|g; s|deb.debian.org/debian-security|archive.debian.org/debian-security|g' /etc/apt/sources.list && \
    apt-get update && apt-get install -y --no-install-recommends ca-certificates curl mono-complete && \
    update-ca-certificates && \
    rm -rf /var/lib/apt/lists/*

# Bring in full source
COPY . .

# Preconfigure Azure Artifacts feed credentials (PAT passed from pipeline)
RUN nuget sources Add -Name "LCU-Internal" -Source "https://landmarkcu.pkgs.visualstudio.com/_packaging/LCU-Internal/nuget/v3/index.json" -Username "build" -Password "${PAT}" -StorePasswordInClearText

# Restore using the solution (now present after full copy)
RUN nuget restore LCU.API.Internal.CoreSoapApiWrapper/LCU.API.Internal.CoreSoapApiWrapper.sln -NonInteractive

# Build a minimal site layout for xsp4 (/out contains Web.config + Global.asax + bin/)
RUN mkdir -p /out/bin && \
    msbuild LCU.API.Internal.CoreSoapApiWrapper/LCU.API.Internal.CoreSoapApiWrapper.csproj /p:Configuration=Release /p:Platform=AnyCPU /p:OutputPath=/out/bin && \
    cp -a LCU.API.Internal.CoreSoapApiWrapper/Web.config LCU.API.Internal.CoreSoapApiWrapper/Global.asax /out/

FROM mono:6.12
WORKDIR /app
RUN sed -i 's|deb.debian.org/debian|archive.debian.org/debian|g; s|security.debian.org/debian-security|archive.debian.org/debian-security|g; s|deb.debian.org/debian-security|archive.debian.org/debian-security|g' /etc/apt/sources.list && \
    apt-get update && apt-get install -y --no-install-recommends \
      mono-xsp4 ca-certificates mono-utils && \
    update-ca-certificates && \
    rm -rf /var/lib/apt/lists/*
	
COPY --from=build /out/ .

ENV PEM_PATH=/etc/ssl/lcu-internal-ca-bundle.pem

EXPOSE 8080
ENTRYPOINT ["/bin/sh","-lc", "\
  if [ -f \"$PEM_PATH\" ]; then \
    cp \"$PEM_PATH\" /usr/local/share/ca-certificates/lcu-internal-ca-bundle.crt && \
    update-ca-certificates && \
    cert-sync /etc/ssl/certs/ca-certificates.crt || true; \
  fi; \
  exec xsp4 --port 8080 --nonstop --address 0.0.0.0 --applications '/:.' \
"]