From 34e70f3a9f4567557422549504b4e9445a744713 Mon Sep 17 00:00:00 2001 From: nagumii Date: Tue, 21 Jan 2025 13:14:26 +0200 Subject: [PATCH 1/2] Prevent time menu from showing during usb-config --- .../usr/local/bin/femto-set-time.sh | 87 ++++++++++--------- 1 file changed, 46 insertions(+), 41 deletions(-) diff --git a/foxbuntu/sysdrv/out/rootfs_uclibc_rv1106/usr/local/bin/femto-set-time.sh b/foxbuntu/sysdrv/out/rootfs_uclibc_rv1106/usr/local/bin/femto-set-time.sh index 3d73f7c..caa7a91 100755 --- a/foxbuntu/sysdrv/out/rootfs_uclibc_rv1106/usr/local/bin/femto-set-time.sh +++ b/foxbuntu/sysdrv/out/rootfs_uclibc_rv1106/usr/local/bin/femto-set-time.sh @@ -1,5 +1,8 @@ #!/bin/bash -export NCURSES_NO_UTF8_ACS=1 # prevents weirdness over tty +# prevents weirdness over tty +export NCURSES_NO_UTF8_ACS=1 +export TERM=xterm-256color +export LANG=C.UTF-8 if [[ $EUID -ne 0 ]]; then echo -e "This script must be run as root. Try \`sudo femto-set-timezone\`." @@ -26,7 +29,7 @@ log_message() { set_timezone() { if ! timedatectl set-timezone "$1"; then - log_message "\nFailed to set the time zone to $1." + log_message "\nFailed to set the time zone to $1." exit 1 fi } @@ -42,47 +45,49 @@ while getopts ":t:h" opt; do esac done -# Fetch available time zones -echo "Loading timezones..." -timezones=$(timedatectl list-timezones) -# Build the options array -options=() -while IFS= read -r timezone; do - options+=("$timezone" "x") -done <<< "$timezones" +if [ $arg_count -eq 0 ]; then # if the script was launched with no arguments, then load the UI. + # Fetch available time zones + echo "Loading timezones..." + timezones=$(timedatectl list-timezones) + # Build the options array + options=() + while IFS= read -r timezone; do + options+=("$timezone" "x") + done <<< "$timezones" -# Convert options array to string -options_str=$(printf '%s\n' "${options[@]}") + # Convert options array to string + options_str=$(printf '%s\n' "${options[@]}") -# Show timezone selection menu with preselection of current timezone -selected_timezone=$(dialog --title "Select Timezone" \ - --default-item "$(cat /etc/timezone)" \ - --menu "Current timezone: $(cat /etc/timezone) (UTC$(date +%z))" 20 60 10 \ - $(printf "%s " "${options[@]}") 3>&1 1>&2 2>&3) -exit_status=$? -if [[ $exit_status -eq 0 && -n "$selected_timezone" ]]; then - # Set the selected time zone - set_timezone $selected_timezone -else - exit 1 -fi - -DATE=$(echo "$(dialog --title "Select Date" --calendar "Choose a date:\nCurrent date: $(date "+%B %d, %Y")\nPress [TAB] to select." 0 0 $(date +%d) $(date +%m) $(date +%Y) 3>&1 1>&2 2>&3)" | awk -F/ '{printf "%04d-%02d-%02d", $3, $2, $1}') -if [ $? -eq 1 ]; then #if cancel/no - exit 1 -fi -TIME=$(dialog --title "Set Time" --timebox "Select time:\nCurrent time: $(date +%H:%M:%S)\nPress [TAB] to select." 0 0 3>&1 1>&2 2>&3) # Dialog timebox for time -if [ $? -eq 1 ]; then #if cancel/no - exit 1 -fi -if date -s "$DATE $TIME"; then # set time - if hwclock --systohc; then - rtc="New time successfully saved to RTC." + # Show timezone selection menu with preselection of current timezone + selected_timezone=$(dialog --title "Select Timezone" \ + --default-item "$(cat /etc/timezone)" \ + --menu "Current timezone: $(cat /etc/timezone) (UTC$(date +%z))" 20 60 10 \ + $(printf "%s " "${options[@]}") 3>&1 1>&2 2>&3) + exit_status=$? + if [[ $exit_status -eq 0 && -n "$selected_timezone" ]]; then + # Set the selected time zone + set_timezone $selected_timezone else - rtc="Unable to communicate with RTC module. An RTC module can save system time between reboots/power outages." + exit 1 + fi + + DATE=$(echo "$(dialog --title "Select Date" --calendar "Choose a date:\nCurrent date: $(date "+%B %d, %Y")\nPress [TAB] to select." 0 0 $(date +%d) $(date +%m) $(date +%Y) 3>&1 1>&2 2>&3)" | awk -F/ '{printf "%04d-%02d-%02d", $3, $2, $1}') + if [ $? -eq 1 ]; then #if cancel/no + exit 1 + fi + TIME=$(dialog --title "Set Time" --timebox "Select time:\nCurrent time: $(date +%H:%M:%S)\nPress [TAB] to select." 0 0 3>&1 1>&2 2>&3) # Dialog timebox for time + if [ $? -eq 1 ]; then #if cancel/no + exit 1 + fi + if date -s "$DATE $TIME"; then # set time + if hwclock --systohc; then + rtc="New time successfully saved to RTC." + else + rtc="Unable to communicate with RTC module. An RTC module can save system time between reboots/power outages." + fi + log_message "System time updated to:\n$(date)\n\n$rtc\nTime & date are also set automatically from internet." + else + log_message "Failed to set system time." + exit 1 fi - log_message "System time updated to:\n$(date)\n\n$rtc\nTime & date are also set automatically from internet." -else - log_message "Failed to set system time." - exit 1 fi \ No newline at end of file From f640845f6ccb16067997e4e7a039c942fd76c637 Mon Sep 17 00:00:00 2001 From: nagumii Date: Tue, 21 Jan 2025 13:37:44 +0200 Subject: [PATCH 2/2] move time-setting to function --- .../usr/local/bin/femto-set-time.sh | 37 +++++++++++++------ 1 file changed, 25 insertions(+), 12 deletions(-) diff --git a/foxbuntu/sysdrv/out/rootfs_uclibc_rv1106/usr/local/bin/femto-set-time.sh b/foxbuntu/sysdrv/out/rootfs_uclibc_rv1106/usr/local/bin/femto-set-time.sh index caa7a91..bc3a166 100755 --- a/foxbuntu/sysdrv/out/rootfs_uclibc_rv1106/usr/local/bin/femto-set-time.sh +++ b/foxbuntu/sysdrv/out/rootfs_uclibc_rv1106/usr/local/bin/femto-set-time.sh @@ -15,6 +15,7 @@ help=$(cat </dev/null 2>&1; then + if hwclock --systohc >/dev/null 2>&1; then + rtc="New time successfully saved to RTC.\n" + else + rtc="Unable to communicate with RTC module. An RTC module can save system time between reboots/power outages.\n" + fi + echo "System time updated to:\n$(date)\n\n${rtc}Time & date are also set automatically from internet." + return 0 + else + echo "Failed to set system time." + return 1 + fi +} + + +while getopts ":t:T:h" opt; do case ${opt} in t) # Option -t (timezone) set_timezone "$OPTARG" ;; + T) # Option -t (timestamp) + set_timestamp "$OPTARG" + ;; h) # Option -h (help) echo -e "$help" ;; @@ -79,15 +99,8 @@ if [ $arg_count -eq 0 ]; then # if the script was launched with no arguments, th if [ $? -eq 1 ]; then #if cancel/no exit 1 fi - if date -s "$DATE $TIME"; then # set time - if hwclock --systohc; then - rtc="New time successfully saved to RTC." - else - rtc="Unable to communicate with RTC module. An RTC module can save system time between reboots/power outages." - fi - log_message "System time updated to:\n$(date)\n\n$rtc\nTime & date are also set automatically from internet." - else - log_message "Failed to set system time." - exit 1 - fi + + log_message "$(set_timestamp $(date -d "$DATE $TIME" +%s))" + exit $? #exit status matches set_timestamp exit status + fi \ No newline at end of file