FROM debian:bookworm

# Add user and group with same IDs as host system
RUN groupadd -g 1000 hub && useradd -u 1000 -g 1000 -m hub

# Install required packages and configure certificates
RUN apt-get update && apt-get install -y \
    ca-certificates \
    libssl3 \
    libcjson1 \
    libjson-c5 \
    libglib2.0-0 \
    libdbus-1-3 \
    libsqlite3-0 \
    libsystemd0 \
    libcap2 \
    libgcrypt20 \
    libgpg-error0 \
    procps \
    psmisc \
    udev \
    libusb-1.0-0 \
    bridge-utils \
    iproute2 \
    net-tools \
    dbus \
    && update-ca-certificates \
    # Configure JSON library links
    && ln -sf /lib/aarch64-linux-gnu/libjson-c.so.5.2.0 /lib/aarch64-linux-gnu/libjson-c.so.5 \
    && ln -sf /lib/aarch64-linux-gnu/libjson-c.so.5 /lib/aarch64-linux-gnu/libjson-c.so \
    && ldconfig \
    # Clean up package lists to reduce image size
    && rm -rf /var/lib/apt/lists/*

# Create all required directories
RUN mkdir -p \
    /data/aws/iotmi \
    /dev/aipc \
    /data/ace/kvstorage \
    /var/log/awsiotmi \
    /data/aws/greengrass/v2/work/ace/aipc \
    /data/aws/greengrass/v2/work/ace/data/kvstorage \
    /usr/local/etc \
    /usr/local/sbin \
    /usr/local/var/lib/zipgateway \
    /var/run \
    /var/log

COPY zipgateway_package/zipgateway /usr/local/sbin/
COPY zipgateway_package/*.cfg /usr/local/etc/
COPY zipgateway_package/*.fin /usr/local/etc/
COPY zipgateway_package/*.tun /usr/local/etc/
COPY zipgateway_package/*.sh /usr/local/etc/
COPY zipgateway_package/*.pem /usr/local/etc/


RUN chmod 755 /usr/local/sbin/zipgateway && \
    chmod 755 /usr/local/etc/*.cfg && \
    chmod 755 /usr/local/etc/*.fin && \
    chmod 755 /usr/local/etc/*.tun && \
    chmod 755 /usr/local/etc/*.sh && \
    chmod 600 /usr/local/etc/*.pem && \
    chmod -R 777 /usr/local/var/lib/zipgateway && \
    chown -R root:root /usr/local/etc && \
    chown -R root:root /usr/local/sbin

RUN chown -R hub:hub /data/aws/greengrass/v2/work/ace/aipc/ && \
    chown -R hub:hub /data/aws/greengrass/v2/work/ace/data/kvstorage/ && \
    chmod -R 777 /data/aws/greengrass

RUN echo "/data/aws/iotmi/middleware/AceZwave/lib" >> /etc/ld.so.conf.d/ace.conf && \
    echo "/data/aws/iotmi/middleware/AceCommon/lib" >> /etc/ld.so.conf.d/ace.conf && \
    echo "/data/aws/iotmi/middleware/AceZigbee/lib" >> /etc/ld.so.conf.d/ace.conf && \
    echo "/data/aws/iotmi/middleware/AceWifi/lib" >> /etc/ld.so.conf.d/ace.conf && \
    echo "/data/aws/iotmi/middleware/AceCli/lib" >> /etc/ld.so.conf.d/ace.conf && \
    ldconfig

# Create startup script that handles network and then runs start_hub_sdk.sh
RUN echo '#!/bin/bash\n\
# Remove existing devices first\n\
rm -f /dev/ttyUSB0 /dev/ttyUSB1\n\
\n\
# Create device nodes based on sysfs info\n\
for tty in /sys/class/tty/ttyUSB*; do\n\
    if [ -e "$tty" ]; then\n\
        dev_path="$tty/device"\n\
        if [ -f "$dev_path/../../product" ]; then\n\
            product=$(cat "$dev_path/../../product")\n\
            dev_info=$(cat "$tty/dev")\n\
            major=$(echo $dev_info | cut -d: -f1)\n\
            minor=$(echo $dev_info | cut -d: -f2)\n\
            \n\
            if [[ "$product" == *"Zigbee"* ]] || [[ "$product" == *"SMLIGHT"* ]]; then\n\
                mknod /dev/ttyUSB0 c $major $minor\n\
                chmod 666 /dev/ttyUSB0\n\
                chown root:dialout /dev/ttyUSB0\n\
            elif [[ "$product" == *"CP2102N"* ]]; then\n\
                mknod /dev/ttyUSB1 c $major $minor\n\
                chmod 666 /dev/ttyUSB1\n\
                chown root:dialout /dev/ttyUSB1\n\
            fi\n\
        fi\n\
    fi\n\
done\n\
\n\
# Bridge setup\n\
ip link add name br-lan type bridge 2>/dev/null || true\n\
ip link set dev br-lan up\n\
ip link set dev eth0 master br-lan 2>/dev/null || true\n\
bridge link set dev eth0 cost 4 2>/dev/null || true\n\
\n\
# Start zipgateway\n\
cd /usr/local/sbin\n\
/usr/local/sbin/zipgateway >> /var/log/zipgateway.log 2>&1 &\n\
\n\
cd /data/aws/iotmi\n\
export LD_LIBRARY_PATH="$(pwd)/middleware/AceZwave/lib:$(pwd)/middleware/AceCommon/lib:$(pwd)/middleware/AceZigbee/lib:$(pwd)/middleware/AceWifi/lib"\n\
./middleware/AceCommon/bin/ace_eventmgr > /dev/null 2>&1 &\n\
sleep 3\n\
\n\
./middleware/AceZigbee/bin/ace_zigbee_service > /dev/null 2>&1 &\n\
sleep 5\n\
\n\
echo "--Checking Zigbee network information--"\n\
NET_INFO=$(./middleware/AceZigbee/bin/ace mw zigbee_cli net_info 2>/dev/null)\n\
\n\
# Wait while adapter is TURNING_ON\n\
while echo "$NET_INFO" | grep -q "TURNING_ON"; do\n\
    echo "Waiting for adapter..."\n\
    sleep 2\n\
    NET_INFO=$(./middleware/AceZigbee/bin/ace mw zigbee_cli net_info 2>/dev/null)\n\
done\n\
\n\
if echo "$NET_INFO" | grep -q "Network Not Formed"; then\n\
    echo "Network not formed, forming network..."\n\
    if ./middleware/AceZigbee/bin/ace mw zigbee_cli formnet > /dev/null 2>&1; then\n\
        echo "Network formation successful"\n\
        sleep 5\n\
        ./middleware/AceZigbee/bin/ace mw zigbee_cli net_info 2>/dev/null\n\
    else\n\
        echo "Network formation failed, restart container"\n\
    fi\n\
fi\n\
\n\
# Now run the Hub SDK startup script with bash\n\
cd /data/aws/iotmi\n\
bash start_hub_sdk.sh\n\
\n\
# Keep container running\n\
tail -f /dev/null' > /startup.sh && chmod +x /startup.sh

CMD ["/startup.sh"]