2013-07-22 17:57:18 +02:00
#!/usr/bin/env bash
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"
_TITLE = "Arch Linux Time And Date Setting"
2008-10-20 22:39:25 +02:00
2011-02-04 18:19:22 +01:00
if [ [ " ${ 1 } " = "--setup" ] ] ; then
2023-01-08 09:08:16 +01:00
_EXIT = "Return to Main Menu"
2009-01-31 19:44:09 +01:00
else
2023-01-08 09:08:16 +01:00
_EXIT = "Exit"
2009-01-31 19:44:09 +01:00
fi
2008-10-20 22:39:25 +02:00
abort( )
{
2011-02-04 18:19:22 +01:00
DIALOG --yesno "Abort Time And Date Setting?" 6 40 || return 0
2023-01-08 09:50:43 +01:00
[ [ -e /tmp/.timezone ] ] && rm -f /tmp/.timezone
2011-02-04 18:19:22 +01:00
[ [ -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
2011-02-04 18:19:22 +01:00
[ [ -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( ) {
2023-01-08 09:08:16 +01:00
dialog --backtitle " ${ _TITLE } " --aspect 15 " $@ "
2011-02-04 18:19:22 +01:00
return $?
2008-10-20 22:39:25 +02:00
}
dohwclock( ) {
2022-11-25 16:33:48 +01:00
echo "0.0 0 0.0" > /etc/adjtime
echo "0" >> /etc/adjtime
2023-01-08 09:08:16 +01:00
[ [ " ${ _HARDWARECLOCK } " = "UTC" ] ] && echo UTC >> /etc/adjtime
[ [ " ${ _HARDWARECLOCK } " = "" ] ] && echo LOCAL >> /etc/adjtime
if [ [ " ${ _HARDWARECLOCK } " = "UTC" ] ] ; then
2013-05-07 18:56:31 +02:00
timedatectl set-local-rtc 0
2013-06-03 16:24:12 +02:00
DATE_PROGRAM = timedatectl
2013-05-07 18:56:31 +02:00
# for setup script
2012-08-06 16:30:44 +02:00
echo UTC > /tmp/.hardwareclock
else
2013-05-07 18:56:31 +02:00
timedatectl set-local-rtc 1
2022-11-25 16:46:42 +01:00
#shellcheck disable=SC2209
2022-11-25 16:33:48 +01:00
DATE_PROGRAM = date
2013-05-07 18:56:31 +02:00
# 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
}
2023-01-08 09:50:43 +01:00
dotimezone ( ) {
2023-01-08 09:08:16 +01:00
_SET_ZONE = ""
while ! [ [ " ${ _SET_ZONE } " = "1" ] ] ; do
_REGIONS = "America - Europe - Africa - Asia - Australia -"
2022-11-25 17:49:23 +01:00
#shellcheck disable=SC2086
2023-01-08 09:08:16 +01:00
DIALOG --menu "Please Select A Region:" 12 40 7 ${ _REGIONS } 2>${ _ANSWER }
_REGION = $( cat ${ _ANSWER } )
_ZONES = ""
2023-01-08 09:50:43 +01:00
for i in $( timedatectl --no-pager list-timezones | grep -w " ${ _REGION } " | cut -d '/' -f 2 | sort -u) ; do
2023-01-08 09:08:16 +01:00
_ZONES = " ${ _ZONES } ${ i } - "
2013-03-27 15:52:30 +01:00
done
2022-01-26 11:42:25 +01:00
#shellcheck disable=SC2086
2023-01-08 09:50:43 +01:00
DIALOG --menu "Please Select A Timezone:" 22 40 16 ${ _ZONES } 2>${ _ANSWER } && _SET_ZONE = "1"
2023-01-08 09:08:16 +01:00
_ZONE = $( cat ${ _ANSWER } )
[ [ " ${ _ZONE } " = = " ${ _REGION } " ] ] || _ZONE = " ${ _REGION } / ${ _ZONE } "
if [ [ " ${ _SET_ZONE } " = "1" ] ] ; then
2023-01-08 09:50:43 +01:00
DIALOG --infobox " Setting Timezone to ${ _ZONE } ... " 0 0
echo " ${ _ZONE } " > /tmp/.timezone
timedatectl set-timezone " ${ _ZONE } "
2023-01-08 09:08:16 +01:00
_S_NEXTITEM = "2"
2011-02-04 18:19:22 +01:00
else
2023-01-08 09:08:16 +01:00
_S_NEXTITEM = "1"
2022-04-04 08:25:15 +02:00
return 1
2011-02-04 18:19:22 +01:00
fi
2009-01-31 19:44:09 +01:00
done
2008-10-20 22:39:25 +02:00
}
dotimeset( ) {
2023-01-08 09:50:43 +01:00
if [ [ ! -s /tmp/.timezone ] ] ; then
DIALOG --msgbox "Error:\nYou have to select timezone first." 0 0
2023-01-08 09:08:16 +01:00
_S_NEXTITEM = "1"
2023-01-08 09:50:43 +01:00
dotimezone || return 1
2008-10-20 22:39:25 +02:00
fi
2023-01-08 09:08:16 +01:00
_SET_TIME = ""
while [ [ " ${ _SET_TIME } " = = "" ] ] ; do
_HARDWARECLOCK = ""
2022-11-25 16:27:03 +01:00
DATE_PROGRAM = ""
2023-01-08 09:08:16 +01: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 may confuse it." 14 60 && _HARDWARECLOCK = "UTC"
2022-11-25 16:24:57 +01:00
dohwclock
2022-11-25 16:17:17 +01:00
# check internet connection
if ping -c1 www.google.com >/dev/null 2>& 1; 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
2022-11-25 21:49:46 +01:00
DIALOG --infobox "Syncing clock with NTP pool ..." 3 45
2022-11-25 16:17:17 +01:00
# sync immediatly with standard pool
2022-12-20 16:14:45 +01:00
if ! systemctl restart systemd-timesyncd; then
2022-11-25 16:17:17 +01:00
DIALOG --msgbox "An error has occured, time was not changed!" 0 0
2023-01-08 09:08:16 +01:00
_S_NEXTITEM = "2"
2022-11-25 16:17:17 +01:00
return 1
fi
# enable background syncing
timedatectl set-ntp 1
2023-01-08 09:08:16 +01:00
_SET_TIME = "1"
2011-02-04 18:19:22 +01:00
fi
2022-11-25 16:17:17 +01:00
fi
2023-01-08 09:08:16 +01:00
if [ [ " ${ _SET_TIME } " = = "" ] ] ; then
2022-11-25 15:34:00 +01:00
timedatectl set-ntp 0
2011-02-04 18:19:22 +01:00
# display and ask to set date/time
2023-01-08 09:08:16 +01:00
_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"
2011-02-04 18:19:22 +01:00
return 1
fi
2023-01-08 09:08:16 +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"
2011-02-04 18:19:22 +01:00
return 1
fi
2023-01-08 09:08:16 +01:00
_TIME = " $( cat ${ _ANSWER } ) "
2011-02-04 18:19:22 +01:00
# save the time
# DD/MM/YYYY hh:mm:ss -> YYYY-MM-DD hh:mm:ss
2023-01-08 09:08:16 +01:00
_DATETIME = " $( echo " ${ _DATE } " " ${ _TIME } " | sed 's#\(..\)/\(..\)/\(....\) \(..\):\(..\):\(..\)#\3-\2-\1 \4:\5:\6#g' ) "
timedatectl set-time " ${ _DATETIME } "
_SET_TIME = "1"
2011-02-04 18:19:22 +01:00
fi
2023-01-08 09:08:16 +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 = ""
2022-11-25 15:34:00 +01:00
done
2023-01-08 09:08:16 +01:00
_S_NEXTITEM = "3"
2008-10-20 22:39:25 +02:00
}
mainmenu( ) {
2023-01-08 09:08:16 +01:00
if [ [ -n " ${ _S_NEXTITEM } " ] ] ; then
DEFAULT = " --default-item ${ _S_NEXTITEM } "
2011-02-04 18:19:22 +01:00
else
DEFAULT = ""
fi
2022-01-26 11:42:25 +01:00
#shellcheck disable=SC2086
2023-01-08 09:08:16 +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" \
2011-02-04 18:19:22 +01:00
"2" "Set Time and Date" \
2023-01-08 09:08:16 +01:00
"3" " ${ _EXIT } " 2>${ _ANSWER }
case $( cat ${ _ANSWER } ) in
2011-02-04 18:19:22 +01:00
"1" )
2023-01-08 09:50:43 +01:00
dotimezone
2011-02-04 18:19:22 +01:00
; ;
"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
2023-01-08 09:50:43 +01:00
: >/tmp/.timezone
2008-10-20 22:39:25 +02:00
: >/tmp/.tz
2011-02-04 18:19:22 +01:00
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
2011-02-04 18:19:22 +01:00
mainmenu
2008-10-20 22:39:25 +02:00
done
clear
exit 0
2011-02-04 18:19:22 +01:00
2013-07-22 17:57:18 +02:00
# vim: set ts=4 sw=4 et: