|
5 | 5 | # Pause for the configured timeout before booting automatically. Returns 0 to |
6 | 6 | # continue with automatic boot, nonzero if user interrupted. |
7 | 7 | pause_automatic_boot() { |
8 | | - if IFS= read -t "$CONFIG_AUTO_BOOT_TIMEOUT" -s -n 1 -r -p \ |
9 | | - $'Automatic boot in '"$CONFIG_AUTO_BOOT_TIMEOUT"$' seconds unless interrupted by keypress...\n'; then |
10 | | - return 1 # Interrupt automatic boot |
11 | | - fi |
| 8 | + local remaining="$CONFIG_AUTO_BOOT_TIMEOUT" |
| 9 | + |
| 10 | + echo "Automatic boot in $CONFIG_AUTO_BOOT_TIMEOUT seconds unless interrupted by keypress..." |
| 11 | + printf "\n" # Reserve space for the updating line |
| 12 | + |
| 13 | + while [ $remaining -gt 0 ]; do |
| 14 | + # Record the start time of this loop iteration (epoch seconds) |
| 15 | + local loop_start now_str status_line elapsed sleep_time |
| 16 | + loop_start=$(date +%s) |
| 17 | + |
| 18 | + now_str=$(date -u '+%Y-%m-%d %H:%M:%S UTC') |
| 19 | + status_line="$now_str | Booting in $remaining seconds..." |
| 20 | + |
| 21 | + if [ "$CONFIG_TPM" = "y" ] && [ "$CONFIG_TOTP_SKIP_QRCODE" != "y" ]; then |
| 22 | + # Get current TOTP if available and not skipped |
| 23 | + local current_totp |
| 24 | + if ! current_totp=$(unseal-totp 2>/dev/null); then |
| 25 | + die "Failed to unseal TOTP" |
| 26 | + fi |
| 27 | + status_line="$status_line | TOTP: $current_totp" |
| 28 | + fi |
| 29 | + |
| 30 | + # Update the status line in place (overwrites previous output) |
| 31 | + printf "\r%s" "$status_line" |
| 32 | + |
| 33 | + # Calculate elapsed time for this loop iteration |
| 34 | + elapsed=$(( $(date +%s) - loop_start )) |
| 35 | + # Calculate how much time to wait to complete 1 second |
| 36 | + sleep_time=$(( 1 - elapsed )) |
| 37 | + [ $sleep_time -lt 0 ] && sleep_time=0 |
| 38 | + |
| 39 | + # Wait for keypress for the remaining time in this second |
| 40 | + # IFS= disables word splitting, -t sets timeout, -s disables echo, -n 1 reads one char, -r disables backslash escapes |
| 41 | + if IFS= read -t $sleep_time -s -n 1 -r; then |
| 42 | + printf "\n" # New line after interrupt |
| 43 | + return 1 # Interrupt automatic boot |
| 44 | + fi |
| 45 | + |
| 46 | + remaining=$((remaining - 1)) |
| 47 | + done |
| 48 | + |
| 49 | + printf "\n" # New line after countdown |
12 | 50 | return 0 # Continue with automatic boot |
13 | 51 | } |
14 | 52 |
|
|
0 commit comments