#!/bin/bash

# Function to show the current timezone, with two alternative methods
show_timezone() {
    # Check if the /etc/timezone file exists
    if [ -f /etc/timezone ]; then
        timezone=$(cat /etc/timezone)
    elif [ -f /etc/localtime ]; then
        timezone=$(readlink /etc/localtime)
        timezone=${timezone/\/usr\/share\/zoneinfo\//}
    else
        timezone="Cannot determine timezone."
    fi
    echo "$timezone"
}

# Function to set the timezone
set_timezone() {
    new_timezone="$1"
    echo "$new_timezone" | sudo tee /etc/timezone >/dev/null
    sudo ln -sf /usr/share/zoneinfo/"$new_timezone" /etc/localtime
    if [ -f /etc/environment ]; then sudo sed -i "/TZ/c\TZ=$new_timezone" /etc/environment; fi
    if [ -d /var/run/s6/container_environment ]; then sudo echo "$new_timezone" > /var/run/s6/container_environment/TZ; fi
    echo "$new_timezone"
}

# Main script
case "$1" in
    "set-ntp")
        case "$2" in
            "false")
                sudo systemctl disable systemd-timesyncd
                echo "NTP disabled"
                ;;
            "true")
                sudo systemctl enable systemd-timesyncd
                sudo systemctl start systemd-timesyncd
                echo "NTP enabled"
                ;;
            *)
                echo "Invalid argument for set-ntp. Use 'false' or 'true'."
                ;;
        esac
        ;;
    "show")
        case "$2" in
            "--value"|"--property=Timezone")
                show_timezone
                ;;
            *)
                echo "Invalid argument for show. Use '--value --property=Timezone'."
                ;;
        esac
        ;;
    "set-timezone")
        set_timezone "$2"
        ;;
    *)
        echo "Usage: $0 {set-ntp [false|true] | show [--value --property=Timezone] | set-timezone <timezone>}"
        ;;
esac
