FROM alpine:3.20 AS ptouch-build

RUN apk add --no-cache build-base cmake gd-dev gettext-dev git libusb-dev pkgconf
RUN git clone --depth 1 --branch farix-main https://github.com/farixembedded/ptouch-print.git /src/ptouch
COPY patches/ptouch-print-cut-options.patch /tmp/ptouch-print-cut-options.patch
WORKDIR /src/ptouch
RUN patch -p1 < /tmp/ptouch-print-cut-options.patch \
    && cmake -S . -B build -DCMAKE_BUILD_TYPE=Release \
    && cmake --build build \
    && install -m 0755 build/ptouch-print /usr/local/bin/ptouch-print

FROM alpine:3.20

ENV PYTHONUNBUFFERED=1 \
    FLASK_APP=app.app \
    LABEL_DIR=/data/labels \
    PRINT_ENABLED=1 \
    PRINT_COMMAND="ptouch-print --image {image}"

RUN apk add --no-cache python3 py3-pip py3-flask py3-pillow gd gettext-libs libusb ttf-dejavu
COPY --from=ptouch-build /usr/local/bin/ptouch-print /usr/local/bin/ptouch-print

WORKDIR /srv/ptouch-web
COPY requirements.txt ./requirements.txt
COPY app ./app
COPY start.sh /usr/local/bin/ptouch-web-start
RUN mkdir -p /data/labels
RUN chmod +x /usr/local/bin/ptouch-web-start

EXPOSE 8080
CMD ["/usr/local/bin/ptouch-web-start"]
