archboot/usr/bin/archboot-clock.sh

129 lines
4 KiB
Bash
Raw Normal View History

#!/usr/bin/env bash
# SPDX-License-Identifier: GPL-2.0-only
2008-10-20 22:39:25 +02:00
# written by Tobias Powalowski <tpowa@archlinux.org>
2023-07-15 09:15:48 +02:00
LANG=C
2023-07-14 09:36:44 +02:00
_ANSWER="/tmp/.clock"
2023-06-29 19:40:07 +02:00
_RUNNING_ARCH="$(uname -m)"
_TITLE="Archboot ${_RUNNING_ARCH} | Arch Linux Setup | Clock Configuration"
2023-01-09 09:05:40 +01:00
# _dialog()
# an el-cheapo dialog wrapper
#
# parameters: see dialog(1)
# returns: whatever dialog did
_dialog() {
dialog --backtitle "${_TITLE}" --aspect 15 "$@"
return $?
}
2023-07-13 16:29:56 +02:00
_abort() {
2023-07-14 12:32:09 +02:00
if _dialog --yesno "Abort Arch Linux Clock Configuration?" 5 45; then
2023-07-14 09:36:44 +02:00
[[ -e /tmp/.clock-running ]] && rm /tmp/.clock-running
[[ -e /tmp/.clock ]] && rm /tmp/.clock
2023-07-13 14:56:53 +02:00
clear
exit 1
else
2023-07-14 15:31:43 +02:00
_CONTINUE=""
2023-07-13 14:56:53 +02:00
fi
}
2023-07-14 15:34:35 +02:00
_hwclock() {
2023-07-14 15:48:10 +02:00
_DATE_PROGRAM=timedatectl
2023-01-09 18:17:27 +01:00
echo 0.0 0 0.0 > /etc/adjtime
echo 0 >> /etc/adjtime
echo UTC >> /etc/adjtime
timedatectl set-local-rtc 0
2008-10-20 22:39:25 +02:00
}
2023-07-14 15:34:35 +02:00
_timezone () {
2023-07-14 15:31:43 +02:00
_SET_ZONE=""
while [[ -z "${_SET_ZONE}" ]]; do
_CONTINUE=""
while [[ -z "${_CONTINUE}" ]]; do
_REGIONS="America - Europe - Africa - Asia - Australia -"
#shellcheck disable=SC2086
2023-07-15 13:53:54 +02:00
if _dialog --cancel-label "${_LABEL}" --title " Timezone Region " --menu "" 11 30 6 ${_REGIONS} 2>${_ANSWER}; then
2023-07-14 15:31:43 +02:00
_REGION=$(cat ${_ANSWER})
_ZONES=""
_CONTINUE=1
else
_abort
fi
done
2023-07-14 15:42:43 +02:00
_ZONES=""
for i in $(timedatectl --no-pager list-timezones | grep -w "${_REGION}" | cut -d '/' -f 2 | sort -u); do
_ZONES="${_ZONES} ${i} -"
2023-07-14 15:31:43 +02:00
done
2023-07-14 15:42:43 +02:00
#shellcheck disable=SC2086
2023-07-15 08:35:49 +02:00
if _dialog --cancel-label "Back" --title " Timezone " --menu "" 21 30 16 ${_ZONES} 2>${_ANSWER}; then
2023-07-14 15:42:43 +02:00
_SET_ZONE="1"
_ZONE=$(cat ${_ANSWER})
[[ "${_ZONE}" == "${_REGION}" ]] || _ZONE="${_REGION}/${_ZONE}"
_dialog --infobox "Setting Timezone to ${_ZONE}..." 3 50
timedatectl set-timezone "${_ZONE}"
sleep 3
else
_SET_ZONE=""
fi
2023-01-09 09:05:40 +01:00
done
2008-10-20 22:39:25 +02:00
}
2023-07-14 15:34:35 +02:00
_timeset() {
2023-07-14 15:47:11 +02:00
_hwclock
# check internet connection
if ping -c1 www.google.com &>/dev/null; then
if _dialog --yesno \
"Do you want to use the Network Time Protocol (NTP) for syncing your clock, by using the internet clock pool?" 6 60; then
_dialog --infobox "Syncing clock with NTP pool..." 3 45
# sync immediatly with standard pool
if ! systemctl restart systemd-timesyncd; then
_dialog --msgbox "An error has occured, time was not changed!" 0 0
2023-07-14 16:37:04 +02:00
_SET_TIME=""
else
# enable background syncing
timedatectl set-ntp 1
2023-07-14 16:39:04 +02:00
_SET_TIME="1"
2023-01-09 09:05:40 +01:00
fi
2023-07-14 15:12:42 +02:00
fi
2023-07-14 15:47:11 +02:00
fi
if [[ -z "${_SET_TIME}" ]]; then
timedatectl set-ntp 0
# display and ask to set date/time
2023-07-15 08:00:29 +02:00
_dialog --title " Date " --no-cancel --calendar "Use <TAB> to navigate and arrow keys to change values." 0 0 0 0 0 2> ${_ANSWER}
_DATE="$(cat ${_ANSWER})"
2023-07-15 08:00:29 +02:00
_dialog --title " Time " --no-cancel --timebox "Use <TAB> to navigate and up/down to change values." 0 0 2> ${_ANSWER}
_TIME="$(cat ${_ANSWER})"
2023-07-14 15:47:11 +02:00
# save the time
# DD/MM/YYYY hh:mm:ss -> YYYY-MM-DD hh:mm:ss
_DATETIME="$(echo "${_DATE}" "${_TIME}" | sed 's#\(..\)/\(..\)/\(....\) \(..\):\(..\):\(..\)#\3-\2-\1 \4:\5:\6#g')"
timedatectl set-time "${_DATETIME}"
fi
2023-07-15 08:00:29 +02:00
if _dialog --cr-wrap --title " Summary " --yesno "$(${_DATE_PROGRAM})" 0 0; then
2023-07-14 15:47:11 +02:00
_dialog --infobox "Clock configuration completed successfully." 3 50
2023-07-14 16:20:37 +02:00
_SET_TIME="1"
2023-07-14 15:47:11 +02:00
sleep 3
else
_SET_TIME=""
fi
2008-10-20 22:39:25 +02:00
}
2023-03-07 17:29:18 +01:00
2023-07-14 09:36:44 +02:00
if [[ -e /tmp/.clock-running ]]; then
echo "clock already runs on a different console!"
echo "Please remove /tmp/.clock-running first to launch clock!"
2023-03-07 17:29:18 +01:00
exit 1
fi
2023-07-14 09:36:44 +02:00
: >/tmp/.clock-running
2023-07-14 15:47:11 +02:00
_SET_TIME=""
2023-07-15 13:53:54 +02:00
if [[ -n "${1}" ]]; then
_LABEL="Back"
else
_LABEL="Exit"
fi
2023-07-14 15:47:11 +02:00
while [[ -z "${_SET_TIME}" ]]; do
2023-07-14 16:34:34 +02:00
_timezone
_timeset
2023-07-14 15:47:11 +02:00
done
2023-07-14 09:36:44 +02:00
[[ -e /tmp/.clock-running ]] && rm /tmp/.clock-running
2008-10-20 22:39:25 +02:00
clear
exit 0
# vim: set ts=4 sw=4 et: