#!/bin/bash
echo "startup initiated"

INTERFACE_SETTINGS="/home/hydrohub/HydroHub/Settings/InterfaceSettings.json"

if [ ! -f "$INTERFACE_SETTINGS" ]; then
    echo "InterfaceSettings.json not found. Using default settings."
    IPADDRESS="0.0.0.0"
    SUBNET="0.0.0.0"
    GATEWAY="0.0.0.0"
    DNS="0.0.0.0"
    DHCP_ENABLED="true"
else
    IPADDRESS=$(sudo jq -r '.address // "0.0.0.0"' "$INTERFACE_SETTINGS")
    SUBNET=$(sudo jq -r '.subnet // "0.0.0.0"' "$INTERFACE_SETTINGS")
    GATEWAY=$(sudo jq -r '.gateway // "0.0.0.0"' "$INTERFACE_SETTINGS")
    DNS=$(sudo jq -r '.dns // "0.0.0.0"' "$INTERFACE_SETTINGS")
    # If dhcpEnabled is missing, default to false for legacy systems.
    DHCP_ENABLED=$(sudo jq -r '.dhcpEnabled // "false"' "$INTERFACE_SETTINGS")
fi

LANGUAGE=$(sudo jq -r '.culture // "en-US"' /home/hydrohub/HydroHub/Settings/systemSettings.json)

echo "IPADDRESS: $IPADDRESS"
echo "SUBNET: $SUBNET"
echo "GATEWAY: $GATEWAY"
echo "DNS: $DNS"
echo "LANGUAGE: $LANGUAGE"
echo "DHCP_ENABLED: $DHCP_ENABLED"

# If dhcpEnabled is true or the IP is "0.0.0.0", call the script with the -p flag.
if [ "$DHCP_ENABLED" == "true" ] || [ "$IPADDRESS" == "0.0.0.0" ]; then
    sudo /home/hydrohub/HydroHub/change_ip_address.sh -a "$IPADDRESS" -s "$SUBNET" -g "$GATEWAY" -d "$DNS" -p
else
    sudo /home/hydrohub/HydroHub/change_ip_address.sh -a "$IPADDRESS" -s "$SUBNET" -g "$GATEWAY" -d "$DNS"
fi

export LANG=$LANGUAGE

echo $LANG

cd /home/hydrohub/HydroHub

./HydroHub
