archboot/usr/bin/archboot-tz.sh

180 lines
5.5 KiB
Bash
Raw Normal View History

#!/usr/bin/env bash
2008-10-20 22:39:25 +02:00
# written by Tobias Powalowski <tpowa@archlinux.org>
ANSWER="/tmp/.tz"
2008-12-17 12:09:15 +01:00
TITLE="Arch Linux Time And Date Setting"
2008-10-20 22:39:25 +02:00
BASEDIR="/usr/share/zoneinfo"
if [[ "${1}" = "--setup" ]]; then
EXIT="Return to Main Menu"
2009-01-31 19:44:09 +01:00
else
EXIT="Exit"
2009-01-31 19:44:09 +01:00
fi
2008-10-20 22:39:25 +02:00
abort()
{
DIALOG --yesno "Abort Time And Date Setting?" 6 40 || return 0
[[ -e /tmp/.timezone ]] && rm -f /tmp/.timezone
[[ -e /tmp/.hardwareclock ]] && rm -f /tmp/.hardwareclock
[[ -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
}
2008-12-17 12:09:15 +01:00
# DIALOG()
# an el-cheapo dialog wrapper
#
# parameters: see dialog(1)
# returns: whatever dialog did
DIALOG() {
dialog --backtitle "${TITLE}" --aspect 15 "$@"
return $?
2008-10-20 22:39:25 +02:00
}
dohwclock() {
if [[ ! -f /etc/adjtime ]]; then
echo "0.0 0 0.0" > /etc/adjtime
echo "0" >> /etc/adjtime
[[ "${HARDWARECLOCK}" = "UTC" ]] && echo UTC >> /etc/adjtime
[[ "${HARDWARECLOCK}" = "" ]] && echo LOCAL >> /etc/adjtime
fi
2012-08-06 16:30:44 +02:00
if [[ "${HARDWARECLOCK}" = "UTC" ]]; then
timedatectl set-local-rtc 0
2013-06-03 16:24:12 +02:00
DATE_PROGRAM=timedatectl
# for setup script
2012-08-06 16:30:44 +02:00
echo UTC > /tmp/.hardwareclock
else
timedatectl set-local-rtc 1
2022-01-21 11:45:13 +01:00
DATE_PROGRAM="$(date)"
# for setup script
2012-08-06 17:06:20 +02:00
echo LOCAL > /tmp/.hardwareclock
2012-08-06 16:30:44 +02:00
fi
2008-10-20 22:39:25 +02:00
}
dotimezone () {
SET_ZONE=""
while ! [[ "${SET_ZONE}" = "1" ]]; do
2013-03-27 15:52:30 +01:00
ZONES=""
for i in $(timedatectl --no-pager list-timezones); do
ZONES="${ZONES} ${i} -"
done
2022-01-21 11:45:13 +01:00
DIALOG --menu "Please Select A Timezone:" 22 60 16 "${ZONES}" 2>${ANSWER} && SET_ZONE="1"
2013-03-27 15:52:30 +01:00
zone=$(cat ${ANSWER})
if [[ "${SET_ZONE}" = "1" ]]; then
DIALOG --infobox "Setting Timezone to ${zone} ..." 0 0
2022-01-21 11:45:13 +01:00
echo "${zone}" > /tmp/.timezone
timedatectl set-timezone "${zone}"
2013-03-27 15:52:30 +01:00
S_NEXTITEM="2"
else
S_NEXTITEM="1"
break
fi
2009-01-31 19:44:09 +01:00
done
2008-10-20 22:39:25 +02:00
}
dotimeset() {
SET_TIME=""
2013-05-08 09:46:36 +02:00
USE_NTPD=""
2012-08-06 16:30:44 +02:00
HARDWARECLOCK=""
DATE_PROGRAM=""
if [[ ! -s /tmp/.timezone ]]; then
2013-03-27 15:52:30 +01:00
DIALOG --msgbox "Error:\nYou have to select timezone first." 0 0
S_NEXTITEM="1"
dotimezone || return 1
2008-10-20 22:39:25 +02:00
fi
2012-08-06 16:30:44 +02:00
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 confuses it." 15 65 && HARDWARECLOCK="UTC"
2008-10-20 22:39:25 +02:00
dohwclock
2013-06-03 16:24:12 +02:00
DIALOG --cr-wrap --yesno "Your current time and date is:\n$(${DATE_PROGRAM})\n\nDo you want to change it?" 0 0 && SET_TIME="1"
if [[ "${SET_TIME}" = "1" ]]; then
2013-05-23 09:15:20 +02:00
timedatectl set-ntp 0
2013-05-08 09:46:36 +02:00
[[ $(which ntpd) ]] && DIALOG --defaultno --yesno "'ntpd' was detected on your system.\n\nDo you want to use 'ntpd' for syncing your clock,\nby using the internet clock pool?\n(You need a working internet connection for doing this!)" 0 0 && USE_NTPD="1"
if [[ "${USE_NTPD}" = "1" ]]; then
2013-05-23 09:15:20 +02:00
# sync immediatly with standard pool
if [[ ! $(ntpdate pool.ntp.org) ]]; then
DIALOG --msgbox "An error has occured, time was not changed!" 0 0
S_NEXTITEM="2"
return 1
fi
2013-05-23 09:15:20 +02:00
# enable background syncing
timedatectl set-ntp 1
2013-06-03 16:24:12 +02:00
DIALOG --cr-wrap --msgbox "Synced clock with internet pool successfully.\n\nYour current time is now:\n$(${DATE_PROGRAM})" 0 0
else
# 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"
if [[ "${CANCEL}" = "1" ]]; then
S_NEXTITEM="2"
return 1
fi
2022-01-21 11:45:13 +01:00
_date="$(cat ${ANSWER})"
dialog --timebox "Set the time.\nUse <TAB> to navigate and up/down to change values." 0 0 2> ${ANSWER} || CANCEL="1"
if [[ "${CANCEL}" = "1" ]]; then
S_NEXTITEM="2"
return 1
fi
2022-01-21 11:45:13 +01:00
_time="$(cat ${ANSWER})"
# save the time
# DD/MM/YYYY hh:mm:ss -> YYYY-MM-DD hh:mm:ss
2022-01-21 11:45:13 +01:00
_datetime="$(echo "${_date}" "${_time}" | sed 's#\(..\)/\(..\)/\(....\) \(..\):\(..\):\(..\)#\3-\2-\1 \4:\5:\6#g')"
timedatectl set-time "${_datetime}"
2013-06-03 16:24:12 +02:00
DIALOG --cr-wrap --msgbox "Your current time is now:\n$(${DATE_PROGRAM})" 0 0
fi
2008-10-20 22:39:25 +02:00
fi
S_NEXTITEM="3"
}
mainmenu() {
if [[ -n "${S_NEXTITEM}" ]]; then
DEFAULT="--default-item ${S_NEXTITEM}"
else
DEFAULT=""
fi
2022-01-22 09:36:13 +01:00
DIALOG ${DEFAULT} --backtitle "${TITLE}" --title " MAIN MENU " \
--menu "Use the UP and DOWN arrows to navigate menus.\nUse TAB to switch between buttons and ENTER to select." 17 58 13 \
2013-03-27 15:52:30 +01:00
"1" "Select Timezone" \
"2" "Set Time and Date" \
"3" "${EXIT}" 2>${ANSWER}
case $(cat ${ANSWER}) in
"1")
dotimezone
;;
"2")
dotimeset
;;
"3")
[[ -e /tmp/.tz-running ]] && rm /tmp/.tz-running
clear
exit 0 ;;
*)
abort ;;
esac
2008-10-20 22:39:25 +02:00
}
: >/tmp/.hardwareclock
: >/tmp/.timezone
: >/tmp/.tz
if [[ ! -d ${BASEDIR} ]]; then
echo "Cannot load timezone data, as none were found in ${BASEDIR}" >&2
exit 1
2008-10-20 22:39:25 +02:00
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
mainmenu
2008-10-20 22:39:25 +02:00
done
clear
exit 0
# vim: set ts=4 sw=4 et: