|
279 | 279 |
|
280 | 280 | CURRENT=`cat tz` |
281 | 281 |
|
282 | | -echo "Timezone (leave blank for ${CURRENT})" |
283 | | -read -p 'Enter Timezone: ' TZ |
| 282 | +# Function to display a (filtered) paginated list of timezones |
| 283 | +show_timezones() { |
| 284 | + local FILTER="$1" |
| 285 | + local LIST_CMD="" |
| 286 | + if command -v timedatectl >/dev/null 2>&1; then |
| 287 | + LIST_CMD="timedatectl list-timezones" |
| 288 | + elif [ -f /usr/share/zoneinfo/zone.tab ]; then |
| 289 | + # Escape $3 so shell doesn't expand it before awk runs |
| 290 | + LIST_CMD="awk '!/^#/ {print \\$3}' /usr/share/zoneinfo/zone.tab" |
| 291 | + else |
| 292 | + LIST_CMD="find /usr/share/zoneinfo -type f -maxdepth 4 2>/dev/null | sed 's#^/usr/share/zoneinfo/##'" |
| 293 | + fi |
| 294 | + |
| 295 | + # Build list (apply optional filter) - allow zero matches without aborting |
| 296 | + if [ -n "$FILTER" ]; then |
| 297 | + TZ_LIST=$( { eval "$LIST_CMD" | grep -F "$FILTER" || true; } | sort ) |
| 298 | + else |
| 299 | + TZ_LIST=$(eval "$LIST_CMD" | sort) |
| 300 | + fi |
| 301 | + |
| 302 | + if [ -z "$TZ_LIST" ]; then |
| 303 | + echo "No timezones match filter '$FILTER'." |
| 304 | + return 0 |
| 305 | + fi |
| 306 | + |
| 307 | + # If less is available and stdout is a TTY, use it for paging |
| 308 | + if command -v less >/dev/null 2>&1 && [ -t 1 ]; then |
| 309 | + echo "$TZ_LIST" | less |
| 310 | + return 0 |
| 311 | + fi |
| 312 | + |
| 313 | + # Manual pagination fallback |
| 314 | + local PAGE_SIZE=30 |
| 315 | + local count=0 |
| 316 | + while IFS= read -r line; do |
| 317 | + printf '%s\n' "$line" |
| 318 | + count=$((count+1)) |
| 319 | + if (( count % PAGE_SIZE == 0 )); then |
| 320 | + read -p "--More-- (Enter=continue, q=quit) " ans |
| 321 | + [[ "$ans" == "q" || "$ans" == "Q" ]] && break |
| 322 | + fi |
| 323 | + done <<< "$TZ_LIST" |
| 324 | +} |
| 325 | + |
| 326 | + |
| 327 | +# Timezone input validation |
| 328 | +echo "Timezone (leave blank for ${CURRENT} or ? to browse)" |
| 329 | +while true; do |
| 330 | + read -p 'Enter Timezone: ' TZ |
| 331 | + if [ "$TZ" = "?" ]; then |
| 332 | + read -p 'Optional filter (e.g., America/ or Europe/Paris): ' TZFILT |
| 333 | + show_timezones "$TZFILT" |
| 334 | + continue |
| 335 | + fi |
| 336 | + if [ -z "$TZ" ]; then |
| 337 | + TZ="$CURRENT" |
| 338 | + break |
| 339 | + fi |
| 340 | + |
| 341 | + # 1) timedatectl (most accurate, includes multi-level names) |
| 342 | + if command -v timedatectl >/dev/null 2>&1; then |
| 343 | + if timedatectl list-timezones 2>/dev/null | grep -Fx "$TZ" >/dev/null; then |
| 344 | + break |
| 345 | + fi |
| 346 | + fi |
| 347 | + |
| 348 | + # 2) Direct zoneinfo file path (supports multi-level e.g. America/Argentina/Buenos_Aires) |
| 349 | + if [ -f "/usr/share/zoneinfo/$TZ" ]; then |
| 350 | + break |
| 351 | + fi |
| 352 | + |
| 353 | + # 3) Check zone.tab third column (if present) – some systems lack timedatectl |
| 354 | + if [ -f /usr/share/zoneinfo/zone.tab ]; then |
| 355 | + if awk '{print $3}' /usr/share/zoneinfo/zone.tab | grep -Fx "$TZ" >/dev/null; then |
| 356 | + break |
| 357 | + fi |
| 358 | + fi |
| 359 | + |
| 360 | + # 4) POSIX / RFC style TZ strings (e.g. GMT, UTC, GMT+5, EST5EDT, etc.) |
| 361 | + # Only accept if it contains at least one alphabetic character to avoid numeric garbage like '1'. |
| 362 | + if [[ "$TZ" =~ [A-Za-z] ]]; then |
| 363 | + if TZ="$TZ" date +%Z >/dev/null 2>&1; then |
| 364 | + echo "Note: '$TZ' accepted as POSIX TZ string (not an Olson zone identifier)." |
| 365 | + break |
| 366 | + fi |
| 367 | + fi |
| 368 | + |
| 369 | + echo "" |
| 370 | + echo "WARNING: '$TZ' is not a recognized timezone." |
| 371 | + echo -n "Do you wish to use this timezone anyway? [y/N] " |
| 372 | + read -r response |
| 373 | + if [[ "$response" =~ ^([yY][eE][sS]|[yY])$ ]]; then |
| 374 | + break |
| 375 | + fi |
| 376 | + echo "" |
| 377 | +done |
284 | 378 | echo "" |
285 | 379 |
|
286 | 380 | # Powerwall Credentials |
|
0 commit comments