FROM node:20-bookworm-slim as web-builder

WORKDIR /ente

# Clone the repository for web app building
RUN apt-get update && apt-get install -y git && \
    git clone --depth=1 https://github.com/ente-io/ente.git . && \
    apt-get clean && apt-get autoremove && \
    rm -rf /var/cache/apt /var/lib/apt/lists

# Will help default to yarn version 1.22.22
RUN corepack enable

# Set environment variables for web app build
# Will be overridden at runtime with the actual server endpoint
ENV NEXT_PUBLIC_ENTE_ENDPOINT="https://localhost:8080"

# Build all web apps
RUN cd web && \
    yarn cache clean && \
    yarn install --network-timeout 1000000000 && \
    yarn build:photos && \
    yarn build:accounts && \
    yarn build:auth && \
    yarn build:cast

FROM cloudron/base:5.0.0@sha256:04fd70dbd8ad6149c19de39e35718e024417c3e01dc9c6637eaf4a41ec4e596c

# Install necessary packages for both Go and Node.js
RUN apt-get update && \
    apt-get install -y curl git nodejs npm golang libsodium23 libsodium-dev pkg-config && \
    npm install -g yarn serve && \
    apt-get clean && apt-get autoremove && \
    rm -rf /var/cache/apt /var/lib/apt/lists

# Set up directory structure
RUN mkdir -p /app/code /app/data/config /app/web

WORKDIR /app/code

# Clone the ente repository during build (for the Museum server)
RUN git clone --depth=1 https://github.com/ente-io/ente.git .

# Copy the web app built files from the first stage
COPY --from=web-builder /ente/web/apps/photos/out /app/web/photos
COPY --from=web-builder /ente/web/apps/accounts/out /app/web/accounts
COPY --from=web-builder /ente/web/apps/auth/out /app/web/auth
COPY --from=web-builder /ente/web/apps/cast/out /app/web/cast

# Copy configuration and startup scripts
ADD start.sh /app/pkg/
ADD config.template.yaml /app/pkg/

# Set proper permissions
RUN chmod +x /app/pkg/start.sh

# Expose the web port (Cloudron expects port 8080)
EXPOSE 8080

# Start the application
CMD ["/app/pkg/start.sh"] 