setup-timezone.in 2.19 KB
Newer Older
1 2 3 4 5 6
#!/bin/sh

PREFIX=
. "$PREFIX/lib/libalpine.sh"


Natanael Copa's avatar
Natanael Copa committed
7
zroot=/usr/share/zoneinfo
8

9 10 11 12 13 14 15 16 17 18 19 20 21 22
usage() {
        cat <<__EOF__
usage: setup-timezone [-h] [-p posixtz format] [-z subfolder of $zroot]

Sets the timezone for the system.

options:
 -h  Show this help
 -p  Specify the timezone in PosixTZ format
 -z  Specify the timezone as a subfolder of $zroot
__EOF__
        exit 1
}

Natanael Copa's avatar
Natanael Copa committed
23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45
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 )
}

valid_tz() {
	find $zroot -type f -a -not -name '*.tab' -a -not -name 'Factory' \
		| xargs posixtz | sort | uniq | grep -q -w "$1"
}

46 47 48 49 50 51 52 53
while getopts "hp:z:" opt; do
        case $opt in
                h) usage;;
                p) POSIXTZ="$OPTARG";;
		t) ZONEINFOFOLDER="$OPTARG";;
        esac
done

Natanael Copa's avatar
Natanael Copa committed
54
if ! apk info -q -e tzdata; then
55
	apk add -q tzdata && apkdel="tzdata" || exit 1
Natanael Copa's avatar
Natanael Copa committed
56 57 58 59 60 61
fi

zonepath=$(cat /etc/TZ 2>/dev/null)
[ -z "$zonepath" ] && zonepath="UTC"

while true; do
62 63 64 65 66 67 68 69 70 71 72 73 74
	if [ -n "$POSIXTZ" ]; then
		echo $POSIXTZ > /etc/TZ || rm -f /etc/TZ
		break
	fi

	if [ -n "$ZONEINFOFOLDER" ]; then
		TZ=$(posixtz "$ZONEINFOFOLDER") || echo "Failed to convert '$ZONEINFOFOLDER' to POSIX TZ"
		if [ -n "$TZ" ]; then
			echo $TZ > /etc/TZ || rm -f /etc/TZ
		fi
		break
	fi

75
	echo -n "Which timezone are you in? ('?' for list) [$zonepath] "
Natanael Copa's avatar
Natanael Copa committed
76 77 78 79
	default_read zonepath "$zonepath"
	case "$zonepath" in
	"") continue;;
	"?") show_tz_list; continue;;
80
	esac
Natanael Copa's avatar
Natanael Copa committed
81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108

	while [ -d "$zroot/$zonepath" ]; do
		local zone=
		echo -n "What sub-timezone of '$zonepath' are you in? ('?' for list)"
		default_read zone
		case "$zone" in
		"?") show_tz_list "$zonepath"; continue;;
		esac
		zonepath="$zonepath/$zone"
	done

	TZ=
	if valid_tz "$zonepath"; then
		TZ="$zonepath"
	elif [ -f "$zroot/$zonepath" ]; then
		TZ=$(posixtz "$zroot/$zonepath") \
			|| echo "Failed to convert '$zroot/$zonepath' to POSIX TZ"
	fi

	if [ -n "$TZ" ]; then
		echo $TZ > /etc/TZ || rm -f /etc/TZ
		break
	fi
	echo "'$zonepath' is not a vaild timezone on this system"
done

if [ -n "$apkdel" ]; then
	apk del -q $apkdel
109 110
fi