FROM ubuntu:24.04

LABEL maintainer="alexz"
LABEL description="NAS Samba + CloudNAS file manager"

ENV DEBIAN_FRONTEND=noninteractive

# Install runtime: Samba, supervisor, tini, Node.js 20.x, build tools for native modules
RUN apt-get update && \
    apt-get install -y --no-install-recommends \
        samba \
        samba-common-bin \
        supervisor \
        curl \
        ca-certificates \
        tini \
        python3 \
        make \
        g++ \
    && curl -fsSL https://deb.nodesource.com/setup_20.x | bash - \
    && apt-get install -y --no-install-recommends nodejs \
    && rm -rf /var/lib/apt/lists/*

# Copy app source
COPY app/ /app/

WORKDIR /app

# Install production dependencies (compiles better-sqlite3 native module)
RUN npm ci --omit=dev && npm cache clean --force

# Clean up
RUN rm -rf /var/lib/apt/lists/* /usr/share/doc /usr/share/man

# Create directories
RUN mkdir -p /nas /config /var/log/supervisor /var/log/samba /run/samba

# Samba configuration
COPY smb.conf /etc/samba/smb.conf

# Supervisord configuration
COPY supervisord.conf /etc/supervisor/conf.d/supervisord.conf

# Entrypoint
COPY entrypoint.sh /entrypoint.sh
RUN chmod +x /entrypoint.sh

EXPOSE 8080 139 445

ENTRYPOINT ["tini", "--"]
CMD ["/entrypoint.sh"]
