# Kymoscope collector (ignore rules: Dockerfile.dockerignore). Build context is the repo root:
#   docker build -f packages/collector/Dockerfile -t ghcr.io/javijur/kymoscope-collector .
#   docker run -d --restart unless-stopped -v /path/to/site:/data ghcr.io/javijur/kymoscope-collector
# The container runs as uid 10001: a bind-mounted host folder must be writable by it (sudo chown -R 10001:10001 /srv/kymoscope).
# /data holds collector.yaml, tags.csv and the collector's state (set state_dir: /data/state).
# Clock rules (cycles at: 06:00) and split_every cuts use the container's time zone: -e TZ=Europe/Madrid (default UTC).
# GenICam cameras (type: genicam): mount the vendor's GenTL producer read-only (-v /opt/vendor:/opt/vendor:ro, cti: under it);
# GigE Vision finds cameras by broadcast, so run with --network host. libexpat1 is there for producers that need it.
# Intel RealSense (type: realsense): pass the USB device (--privileged -v /dev:/dev); a recording (bag:) needs only a mount.
FROM python:3.12-slim
LABEL org.opencontainers.image.source="https://github.com/Javijur/kymoscope" \
      org.opencontainers.image.title="Kymoscope collector" \
      org.opencontainers.image.description="Site collector: OPC UA, logger files and custom sources to end-to-end encrypted Kymoscope streams."
COPY packages/python /src/python
COPY packages/collector /src/collector
# gcc only while installing: some readers (asammdf's zstd) ship no arm64 wheel
RUN apt-get update && apt-get install -y --no-install-recommends unixodbc tzdata ffmpeg libexpat1 libusb-1.0-0 gcc libc6-dev  && pip install --no-cache-dir /src/python "/src/collector[files,sql,genicam,realsense]"  && apt-get purge -y gcc libc6-dev && apt-get autoremove -y && rm -rf /var/lib/apt/lists/* /src
RUN useradd --create-home --uid 10001 collector && mkdir -p /data && chown collector /data
RUN printf '%s\n' '#!/bin/sh' \
    '[ -w /data ] || { echo "kymoscope-collector: /data is not writable by uid $(id -u). On the host run: sudo chown -R 10001:10001 <the folder mounted at /data> (or start the container with --user)." >&2; exit 1; }' \
    'exec kymoscope-collector "$@"' > /usr/local/bin/collector-entrypoint && chmod +x /usr/local/bin/collector-entrypoint
USER collector
WORKDIR /data
VOLUME /data
ENTRYPOINT ["collector-entrypoint"]
CMD ["run", "--config", "/data/collector.yaml"]
