archboot/usr/bin/archboot-tz.sh

180 lines
6 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-01-08 09:08:16 +01:00
_ANSWER="/tmp/.tz"
2023-03-02 17:58:58 +01:00
_TITLE="Archboot Arch Linux --> Time And Date Setting"
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 $?
}
_abort()
2008-10-20 22:39:25 +02:00
{
2023-01-08 22:03:15 +01:00
_dialog --yesno "Abort Time And Date Setting?" 6 40 || return 0
[[ -e /tmp/.tz ]] && rm -f /tmp/.tz
[[ -e /etc/localtime ]] && rm -f /etc/localtime
2012-08-06 17:06:20 +02:00
[[ -e /etc/adjtime ]] && rm -f /etc/adjtime
[[ -e /tmp/.tz-running ]] && rm /tmp/.tz-running
clear
exit 1
2008-10-20 22:39:25 +02:00
}
2023-01-09 09:05:40 +01:00
_dohwclock() {
2023-01-09 18:17:27 +01:00
echo 0.0 0 0.0 > /etc/adjtime
echo 0 >> /etc/adjtime
2023-01-09 09:05:40 +01:00
[[ "${_HARDWARECLOCK}" = "UTC" ]] && echo UTC >> /etc/adjtime
[[ "${_HARDWARECLOCK}" = "" ]] && echo LOCAL >> /etc/adjtime
if [[ "${_HARDWARECLOCK}" = "UTC" ]]; then
timedatectl set-local-rtc 0
_DATE_PROGRAM=timedatectl
else
timedatectl set-local-rtc 1
#shellcheck disable=SC2209
_DATE_PROGRAM=date
fi
2008-10-20 22:39:25 +02:00
}
2023-01-09 09:05:40 +01:00
_dotimezone () {
_SET_ZONE=""
2023-01-09 18:17:27 +01:00
while [[ -z "${_SET_ZONE}" ]]; do
2023-01-09 09:05:40 +01:00
_REGIONS="America - Europe - Africa - Asia - Australia -"
#shellcheck disable=SC2086
_dialog --menu "Please Select A Region:" 12 40 7 ${_REGIONS} 2>${_ANSWER}
_REGION=$(cat ${_ANSWER})
_ZONES=""
for i in $(timedatectl --no-pager list-timezones | grep -w "${_REGION}" | cut -d '/' -f 2 | sort -u); do
_ZONES="${_ZONES} ${i} -"
done
#shellcheck disable=SC2086
_dialog --menu "Please Select A Timezone:" 22 40 16 ${_ZONES} 2>${_ANSWER} && _SET_ZONE="1"
_ZONE=$(cat ${_ANSWER})
[[ "${_ZONE}" == "${_REGION}" ]] || _ZONE="${_REGION}/${_ZONE}"
2023-01-09 18:17:27 +01:00
if [[ -n "${_SET_ZONE}" ]]; then
2023-01-19 08:04:48 +01:00
_dialog --infobox "Setting Timezone to ${_ZONE}..." 0 0
2023-01-09 09:05:40 +01:00
timedatectl set-timezone "${_ZONE}"
_S_NEXTITEM="2"
else
_S_NEXTITEM="1"
return 1
fi
done
2008-10-20 22:39:25 +02:00
}
2023-01-09 09:05:40 +01:00
_dotimeset() {
if [[ -z "${_SET_ZONE}" ]]; then
2023-01-09 09:05:40 +01:00
_dialog --msgbox "Error:\nYou have to select timezone first." 0 0
2023-01-08 09:08:16 +01:00
_S_NEXTITEM="1"
2023-01-09 09:05:40 +01:00
dotimezone || return 1
fi
2023-01-09 09:05:40 +01:00
_SET_TIME=""
while [[ -z "${_SET_TIME}" ]]; do
_HARDWARECLOCK=""
_DATE_PROGRAM=""
_dialog --yesno "Do you want to use UTC for your clock?\n\nIf you choose 'YES' UTC (recommended default) is used,\nwhich ensures daylightsaving is set automatically.\n\nIf you choose 'NO' Localtime is used, which means\nthe system will not change the time automatically.\nLocaltime is also prefered on dualboot machines,\nwhich also run Windows, because UTC may confuse it." 14 60 && _HARDWARECLOCK="UTC"
_dohwclock
# check internet connection
2023-01-19 08:12:54 +01:00
if ping -c1 www.google.com &>/dev/null; then
2023-01-09 09:05:40 +01:00
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
2023-01-19 08:04:48 +01:00
_dialog --infobox "Syncing clock with NTP pool..." 3 45
2023-01-09 09:05:40 +01:00
# sync immediatly with standard pool
if ! systemctl restart systemd-timesyncd; then
_dialog --msgbox "An error has occured, time was not changed!" 0 0
_S_NEXTITEM="2"
return 1
fi
# enable background syncing
timedatectl set-ntp 1
_SET_TIME="1"
fi
fi
2023-01-09 18:17:27 +01:00
if [[ -z "${_SET_TIME}" ]]; then
2023-01-09 09:05:40 +01:00
timedatectl set-ntp 0
# display and ask to set date/time
_CANCEL=""
dialog --calendar "Set the date.\nUse <TAB> to navigate and arrow keys to change values." 0 0 0 0 0 2> ${_ANSWER} || _CANCEL="1"
2023-01-09 18:17:27 +01:00
if [[ -n "${_CANCEL}" ]]; then
2023-01-09 09:05:40 +01:00
_S_NEXTITEM="2"
return 1
fi
_DATE="$(cat ${_ANSWER})"
dialog --timebox "Set the time.\nUse <TAB> to navigate and up/down to change values." 0 0 2> ${_ANSWER} || _CANCEL="1"
2023-01-09 18:17:27 +01:00
if [[ -n "${_CANCEL}" ]]; then
2023-01-08 09:08:16 +01:00
_S_NEXTITEM="2"
return 1
fi
2023-01-09 09:05:40 +01:00
_TIME="$(cat ${_ANSWER})"
# 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}"
2023-01-08 09:08:16 +01:00
_SET_TIME="1"
fi
2023-01-09 09:05:40 +01:00
_dialog --cr-wrap --defaultno --yesno "Your current time and date is:\n$(${_DATE_PROGRAM})\n\nDo you want to change it?" 0 0 && _SET_TIME=""
done
_S_NEXTITEM="3"
2008-10-20 22:39:25 +02:00
}
2023-01-09 09:05:40 +01:00
_mainmenu() {
2023-01-08 09:08:16 +01:00
if [[ -n "${_S_NEXTITEM}" ]]; then
2023-01-09 09:05:40 +01:00
_DEFAULT="--default-item ${_S_NEXTITEM}"
else
2023-01-09 09:05:40 +01:00
_DEFAULT=""
fi
2022-01-26 11:42:25 +01:00
#shellcheck disable=SC2086
2023-01-09 09:05:40 +01:00
_dialog ${_DEFAULT} --backtitle "${_TITLE}" --title " MAIN MENU " \
2022-04-04 08:30:02 +02:00
--menu "Use the UP and DOWN arrows to navigate menus.\nUse TAB to switch between buttons and ENTER to select." 11 58 13 \
2023-01-08 09:50:43 +01:00
"1" "Select Timezone" \
"2" "Set Time and Date" \
2023-01-08 09:08:16 +01:00
"3" "${_EXIT}" 2>${_ANSWER}
case $(cat ${_ANSWER}) in
"1")
2023-01-09 09:05:40 +01:00
_dotimezone
;;
"2")
2023-01-09 09:05:40 +01:00
_dotimeset
;;
"3")
[[ -e /tmp/.tz-running ]] && rm /tmp/.tz-running
clear
exit 0 ;;
*)
2023-01-09 09:05:40 +01:00
_abort ;;
esac
2008-10-20 22:39:25 +02:00
}
: >/tmp/.tz
if [[ "${1}" = "--setup" ]]; then
2023-03-07 17:23:48 +01:00
if ! _dotimezone; then
[[ -e /tmp/.km-running ]] && rm /tmp/.km-running
clear
exit 1
fi
if ! _dotimeset; then
[[ -e /tmp/.km-running ]] && rm /tmp/.km-running
clear
exit 1
fi
exit 0
else
_EXIT="Exit"
fi
if [[ -e /tmp/.tz-running ]]; then
echo "tz already runs on a different console!"
echo "Please remove /tmp/.tz-running first to launch tz!"
exit 1
2008-10-20 22:39:25 +02:00
fi
: >/tmp/.tz-running
2008-12-17 12:09:15 +01:00
while true; do
2023-01-09 09:05:40 +01:00
_mainmenu
2008-10-20 22:39:25 +02:00
done
clear
exit 0
# vim: set ts=4 sw=4 et: