#!/bin/bash

# Define the player path
playerpath="/home/smg/smgplayer"

# Function to get the deviceID from config.ini
get_id() {
    cat "$playerpath/config.ini" | grep "deviceID=" | awk -F '=' '{print $2}'
}

# Function to split and display the ID in 4-character chunks, adding dots if necessary
display_id() {
    local id="$1"
    local id_length=${#id}

    if [ $id_length -le 4 ]; then
        # If ID is 4 characters or less, display as is
        echo "#m2$id" > /sys/devices/platform/openvfd/attr/led
    else
        # Split the ID into chunks of 4 characters and handle remainder
        local first_part=${id:0:4}
        local second_part=${id:4}

        echo "#m2$first_part" > /sys/devices/platform/openvfd/attr/led
        sleep 1  # Give the device time to process

        if [ ${#second_part} -le 4 ]; then
            # If second part is less than or equal to 4 characters, add dots to complete
            second_part=$(printf "%-4s" "$second_part" | tr ' ' '.')
            echo "#m2$second_part" > /sys/devices/platform/openvfd/attr/led
        else
            # Handle longer IDs (more than 8 characters total)
            local third_part=${id:8:4}
            second_part=$(printf "%-4s" "$second_part" | tr ' ' '.')
            echo "#m2$second_part" > /sys/devices/platform/openvfd/attr/led
            sleep 1
            echo "#m2$third_part" > /sys/devices/platform/openvfd/attr/led
        fi
    fi
}

# Function to check if an IP address exists and return "NOIP" or the IP address
get_ip() {
    # Check if there is an active IP address (excluding loopback)
    local ip_count=$(ip address | grep "inet.*brd" | wc -l)
    
    if [ "$ip_count" -ge 1 ]; then
        # If there is an IP, return hostname -I
        local ip=$(hostname -I | awk '{print $1}')
        echo "$ip"
    else
        # If no IP, return "NOIP"
        echo "NOIP"
    fi
}

# Format the IP octets (for single-digit octets, add two dots, otherwise pad to 3 digits and add a dot)
format_octet() {
    local octet="$1"
    if [ ${#octet} -eq 1 ]; then
        printf "..%s." "$octet"
    else
        printf "%-4s" "$octet." | tr ' ' '.'
    fi
}

# Function to write to the VFD screen
display_on_vfd() {
    local message="$1"
    echo "$message" > /sys/devices/platform/openvfd/attr/led
}

# Turn off the screen
turn_off_vfd() {
    echo "#c0" > /sys/devices/platform/openvfd/attr/led
}

# Turn on the screen (to display the time)
turn_on_vfd() {
    echo "#s1" > /sys/devices/platform/openvfd/attr/led
}

# Main loop
while true; do
    # Retrieve the ID and IP information
    id=$(get_id)
    ip_address=$(get_ip)

    # 1. Display the ID on the screen (in .ID. format)
    display_on_vfd "#m2.ID."
    sleep 1

    # Display the formatted ID
    display_id "$id"
    sleep 2

    # Check if IP address is "NOIP"
    if [ "$ip_address" == "NOIP" ]; then
        display_on_vfd "#m2NOIP"
        sleep 2
    else
        # Split the IP address into octets
        IFS='.' read -r octet1 octet2 octet3 octet4 <<< "$ip_address"

        # 2. Display the IP address (in .IP. format)
        display_on_vfd "#m2.IP."
        sleep 1
        display_on_vfd "#m2$(format_octet ${octet1})"
        sleep 1
        display_on_vfd "#m2$(format_octet ${octet2})"
        sleep 1
        display_on_vfd "#m2$(format_octet ${octet3})"
        sleep 1
        display_on_vfd "#m2$(format_octet ${octet4})"
        sleep 2
    fi

    # 3. Turn off the screen and wait for 0.5 seconds
    turn_off_vfd
    sleep 0.5

    # 4. Turn on the screen to display the time
    turn_on_vfd
    sleep 5

    # Repeat the loop every minute
    sleep 60
done
