setup-timezone.in 2.27 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
usage() {
10 11
	cat <<-__EOF__
		usage: setup-timezone [-h] [-k|-i] [-z TIMEZONE]
12

13
		Sets the timezone for the system.
14

15 16 17 18 19 20
		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__
21
	exit 1
22 23
}

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

42
setup_tz() {
43
	local zonepath="$1"
44
	mkdir -p "${ROOT}"etc/zoneinfo
45 46 47 48 49 50 51 52 53 54 55
	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
56 57
	rm -f "${ROOT}"etc/localtime
	ln -s "$zonepath" "${ROOT}"etc/localtime
Natanael Copa's avatar
Natanael Copa committed
58 59
}

60 61 62
INSTALL_TZDATA=false
KEEP_TZDATA=false
while getopts "hikz:" opt; do
63 64
	case $opt in
		h) usage;;
65 66
		i) INSTALL_TZDATA=true;;
		k) KEEP_TZDATA=true;;
67
		z) ZONE="$OPTARG";;
68
	esac
69 70
done

71 72 73 74
if $INSTALL_TZDATA; then
	pkg=tzdata
	apkdel=
else
75
	pkg="--force --virtual .setup-timezone tzdata"
76 77 78 79 80 81
	apkdel=".setup-timezone"
fi

apk add --quiet $pkg

if [ -L "${ROOT}"etc/zoneinfo/localtime ]; then
82 83
	timezone=$(readlink "${ROOT}"etc/zoneinfo/localtime)
	timezone=${timezone#*/zoneinfo/}
84
else
85
	timezone=UTC
Natanael Copa's avatar
Natanael Copa committed
86 87 88 89
fi


while true; do
90 91
	if [ -n "$ZONE" ]; then
		setup_tz "$zroot"/"$ZONE"
92 93 94
		break
	fi

95 96 97
	echo -n "Which timezone are you in? ('?' for list) [$timezone] "
	default_read timezone "$timezone"
	case "$timezone" in
Natanael Copa's avatar
Natanael Copa committed
98 99
	"") continue;;
	"?") show_tz_list; continue;;
100
	esac
Natanael Copa's avatar
Natanael Copa committed
101

102
	while [ -d "$zroot/$timezone" ]; do
Yves Schumann's avatar
Yves Schumann committed
103
		zone=
104
		echo -n "What sub-timezone of '$timezone' are you in? ('?' for list) "
Natanael Copa's avatar
Natanael Copa committed
105 106
		default_read zone
		case "$zone" in
107
		"?") show_tz_list "$timezone"; continue;;
Natanael Copa's avatar
Natanael Copa committed
108
		esac
109
		timezone="$timezone/$zone"
Natanael Copa's avatar
Natanael Copa committed
110 111
	done

112 113
	if [ -f "$zroot/$timezone" ]; then
		setup_tz "$zroot/$timezone"
Natanael Copa's avatar
Natanael Copa committed
114 115
		break
	fi
116
	echo "'$timezone' is not a valid timezone on this system"
Natanael Copa's avatar
Natanael Copa committed
117 118 119
done

if [ -n "$apkdel" ]; then
120
	apk del --quiet $apkdel
121
fi