FROM homeassistant/armhf-homeassistant:%%VERSION%%

LABEL io.hass.machine raspberrypi3

RUN apk --no-cache add raspberrypi raspberrypi-libs usbutils

##
# Set symlinks for raspberry pi binaries.
RUN ln -sv /opt/vc/bin/raspistill /usr/local/bin/raspistill \
    && ln -sv /opt/vc/bin/raspivid /usr/local/bin/raspivid \
    && ln -sv /opt/vc/bin/raspividyuv /usr/local/bin/raspividyuv \
    && ln -sv /opt/vc/bin/raspiyuv /usr/local/bin/raspiyuv

##
# Build libcec with RPi support for HDMI-CEC
RUN apk del libcec \
    && apk add --no-cache build-base cmake eudev-dev git \
           python3-dev swig raspberrypi-dev \
    && git clone --depth 1 -b p8-platform-2.1.0.1 https://github.com/Pulse-Eight/platform /usr/src/platform \
    && mkdir -p /usr/src/platform/build \
    && cd /usr/src/platform/build \
    && cmake -DCMAKE_INSTALL_PREFIX:PATH=/usr .. \
    && make \
    && make install \
    && cd /usr/src \
    && rm -rf platform \
    && git clone --depth 1 -b libcec-4.0.2 https://github.com/Pulse-Eight/libcec /usr/src/libcec \
    && mkdir -p /usr/src/libcec/build \
    && cd /usr/src/libcec/build \
    && cmake -DCMAKE_INSTALL_PREFIX:PATH=/usr \
        -DRPI_INCLUDE_DIR=/opt/vc/include \
        -DRPI_LIB_DIR=/opt/vc/lib \
        -DPYTHON_LIBRARY="/usr/lib/libpython3.6m.so" \
        -DPYTHON_INCLUDE_DIR="/usr/include/python3.6m" .. \
    && make \
    && make install \
    && echo "cec" > "/usr/lib/python3.6/site-packages/cec.pth" \
    && cd /usr/src \
    && rm -rf libcec \
    && apk del build-base cmake eudev-dev git \
           python3-dev swig raspberrypi-dev
ENV LD_LIBRARY_PATH=/opt/vc/lib:${LD_LIBRARY_PATH}

##
# Install DHT
RUN apk add --no-cache gcc libc-dev python3-dev \
    && curl -L "https://github.com/adafruit/Adafruit_Python_DHT/archive/da8cddf7fb629c1ef4f046ca44f42523c9cf2d11.zip#Adafruit_DHT==1.3.2" \
        -o /usr/src/Adafruit_DHT-1.3.2.zip \
    && cd /usr/src \
    && unzip Adafruit_DHT-1.3.2.zip \
    && cd Adafruit_Python_DHT-da8cddf7fb629c1ef4f046ca44f42523c9cf2d11 \
    && sed -i 's/^pi_version\ =\ None/pi_version\ =\ 3/' setup.py \
    && sed -i 's/^platform\ =\ platform_detect.UNKNOWN/platform\ =\ platform_detect.RASPBERRY_PI/' setup.py \
    && sed -i 's/platform\ =\ platform_detect.platform_detect()/pass/' \
        setup.py \
    && pip3 install . \
    && apk del gcc libc-dev python3-dev \
    && cd /usr/src \
    && rm -rf Adafruit_Python_DHT-da8cddf7fb629c1ef4f046ca44f42523c9cf2d11 \
        Adafruit_DHT-1.3.2.zip
