Files
edge/sbin/setup-timezone
2018-11-06 00:13:24 +00:00

122 lines
2.3 KiB
Bash
Executable File

#!/bin/sh
PREFIX=
. "$PREFIX/lib/libalpine.sh"
zroot=/usr/share/zoneinfo
usage() {
cat <<-__EOF__
usage: setup-timezone [-h] [-k|-i] [-z TIMEZONE]
Sets the timezone for the system.
options:
-h Show this help
-i Install tzdata and symlink instead of making a copy
-k Keep previous copies of tzdata
-z Set given timezone. (relative $zroot)
__EOF__
exit 1
}
show_tz_list() {
local i z= list=
local path="$zroot/$1"
[ -d "$path" ] || return 1
for i in $(find $path -maxdepth 1); do
case $i in
*.tab|*/) continue;;
esac
if [ -d "$i" ]; then
z="$z ${i##*/}/"
else
z="$z ${i##*/}"
fi
done
( cd $path && ls --color=never -Cd $z )
}
setup_tz() {
local zonepath="$1"
mkdir -p "${ROOT}"etc/zoneinfo
if ! $INSTALL_TZDATA; then
local zone="${zonepath#*/zoneinfo/}"
local zdir="${zonepath%/*}"/
zdir="${zdir#*/zoneinfo/}"
if ! $KEEP_TZDATA; then
rm -r "${ROOT}"/etc/zoneinfo
fi
mkdir -p "${ROOT}"etc/zoneinfo/$zdir
cp "$zonepath" "${ROOT}"etc/zoneinfo/$zdir/
zonepath=/etc/zoneinfo/$zone
fi
rm -f "${ROOT}"etc/localtime
ln -s "$zonepath" "${ROOT}"etc/localtime
}
INSTALL_TZDATA=false
KEEP_TZDATA=false
while getopts "hikz:" opt; do
case $opt in
h) usage;;
i) INSTALL_TZDATA=true;;
k) KEEP_TZDATA=true;;
z) ZONE="$OPTARG";;
esac
done
if $INSTALL_TZDATA; then
pkg=tzdata
apkdel=
else
pkg="--force --virtual .setup-timezone tzdata"
apkdel=".setup-timezone"
fi
apk add --quiet $pkg
if [ -L "${ROOT}"etc/zoneinfo/localtime ]; then
timezone=$(readlink "${ROOT}"etc/zoneinfo/localtime)
timezone=${timezone#*/zoneinfo/}
else
timezone=UTC
fi
while true; do
if [ -n "$ZONE" ]; then
setup_tz "$zroot"/"$ZONE"
break
fi
echo -n "Which timezone are you in? ('?' for list) [$timezone] "
default_read timezone "$timezone"
case "$timezone" in
"") continue;;
"?") show_tz_list; continue;;
esac
while [ -d "$zroot/$timezone" ]; do
zone=
echo -n "What sub-timezone of '$timezone' are you in? ('?' for list) "
default_read zone
case "$zone" in
"?") show_tz_list "$timezone"; continue;;
esac
timezone="$timezone/$zone"
done
if [ -f "$zroot/$timezone" ]; then
setup_tz "$zroot/$timezone"
break
fi
echo "'$timezone' is not a valid timezone on this system"
done
if [ -n "$apkdel" ]; then
apk del --quiet $apkdel
fi