setup-timezone.in 1.59 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
usage() {
        cat <<__EOF__
11
usage: setup-timezone [-h] [-z subdir of $zroot]
12 13 14 15 16

Sets the timezone for the system.

options:
 -h  Show this help
17
 -z  Specify the timezone as a subdirectory of $zroot
18 19 20 21
__EOF__
        exit 1
}

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

40 41 42
setup_tz() {
	mkdir -p "${ROOT}"etc/zoneinfo
	cp "$1" "${ROOT}"etc/zoneinfo/localtime
Natanael Copa's avatar
Natanael Copa committed
43 44
}

45
while getopts "hz:" opt; do
46 47
        case $opt in
                h) usage;;
48
		z) ZONEINFODIR="$OPTARG";;
49 50 51
        esac
done

52 53
if ! apk info --quiet --installed tzdata; then
	apk add --quiet tzdata && apkdel="tzdata" || exit 1
Natanael Copa's avatar
Natanael Copa committed
54 55
fi

56
zonepath=UTC
Natanael Copa's avatar
Natanael Copa committed
57 58

while true; do
59
	if [ -n "$ZONEINFODIR" ]; then
60
		setup_tz "$ZONEINFODIR"
61 62 63
		break
	fi

64
	echo -n "Which timezone are you in? ('?' for list) [$zonepath] "
Natanael Copa's avatar
Natanael Copa committed
65 66 67 68
	default_read zonepath "$zonepath"
	case "$zonepath" in
	"") continue;;
	"?") show_tz_list; continue;;
69
	esac
Natanael Copa's avatar
Natanael Copa committed
70 71

	while [ -d "$zroot/$zonepath" ]; do
Yves Schumann's avatar
Yves Schumann committed
72 73
		zone=
		echo -n "What sub-timezone of '$zonepath' are you in? ('?' for list) "
Natanael Copa's avatar
Natanael Copa committed
74 75 76 77 78 79 80
		default_read zone
		case "$zone" in
		"?") show_tz_list "$zonepath"; continue;;
		esac
		zonepath="$zonepath/$zone"
	done

81 82
	if [ -f "$zroot/$zonepath" ]; then
		setup_tz "$zroot/$zonepath"
Natanael Copa's avatar
Natanael Copa committed
83 84
		break
	fi
85
	echo "'$zonepath' is not a valid timezone on this system"
Natanael Copa's avatar
Natanael Copa committed
86 87 88
done

if [ -n "$apkdel" ]; then
89
	apk del --quiet $apkdel
90 91
fi