libalpine.sh.in 5.49 KB
Newer Older
Natanael Copa's avatar
Natanael Copa committed
1 2
#!/bin/sh

Natanael Copa's avatar
Natanael Copa committed
3
PREFIX=@PREFIX@
Natanael Copa's avatar
Natanael Copa committed
4

5
PROGRAM=$(basename $0)
Natanael Copa's avatar
Natanael Copa committed
6

Natanael Copa's avatar
Natanael Copa committed
7 8 9 10
: ${ROOT:=/}
[ "${ROOT}" = "${ROOT%/}" ] && ROOT="${ROOT}/"
[ "${ROOT}" = "${ROOT#/}" ] && ROOT="${PWD}/${ROOT}"

Natanael Copa's avatar
Natanael Copa committed
11 12 13 14
echon () {
	if [ X"$ECHON" = X ]; then
		# Determine how to "echo" without newline: "echo -n"
		# or "echo ...\c"
15
		if [ X$(echo -n) = X-n ]; then
Natanael Copa's avatar
Natanael Copa committed
16 17 18 19 20 21 22 23 24 25 26
			ECHON=echo
			NNL="\c"
			# "
		else
			ECHON="echo -n"
			NNL=""
		fi
	fi
	$ECHON "$*$NNL"
}

27 28
# echo if in verbose mode
vecho() {
29 30 31
	if [ -n "$VERBOSE" ]; then
		echo "$@"
	fi
32 33
}

Natanael Copa's avatar
Natanael Copa committed
34 35
# echo unless quiet mode
qecho() {
36 37 38
	if [ -z "$QUIET" ]; then
		echo "$@"
	fi
Natanael Copa's avatar
Natanael Copa committed
39 40 41 42 43 44 45
}

# echo to stderr
eecho() {
	echo "$@" >&2
}

Natanael Copa's avatar
Natanael Copa committed
46 47 48 49 50 51
# echo to stderr and exit with error
die() {
	eecho "$@"
	exit 1
}

Natanael Copa's avatar
Natanael Copa committed
52
init_tmpdir() {
53 54
	local omask=$(umask)
	local __tmpd="/tmp/$PROGRAM-${$}-$(date +%s)-$RANDOM"
Natanael Copa's avatar
Natanael Copa committed
55
	umask 077 || die "umask"
Natanael Copa's avatar
Natanael Copa committed
56
	mkdir -p "$__tmpd" || exit 1
57
	trap "rc=\$?; rm -fr \"$__tmpd\"; exit \$rc" 0
Natanael Copa's avatar
Natanael Copa committed
58 59 60 61 62
	umask $omask
	eval "$1=\"$__tmpd\""
}

pkg_inst() {
Dubiousjim's avatar
Dubiousjim committed
63
	eecho "WARNING: pkg_inst is deprecated. Use 'apk add --quiet' in script"
64
	[ -z "$NOCOMMIT" ] && apk add --quiet $*
Natanael Copa's avatar
Natanael Copa committed
65 66
}

67
pkg_deinst() {
Dubiousjim's avatar
Dubiousjim committed
68
	eecho "WARNING: pkg_deinst is deprecated. Use 'apk del --quiet' in script"
69
	[ -z "$NOCOMMIT" ] && apk del --quiet $*
70 71
}

Natanael Copa's avatar
Natanael Copa committed
72 73 74 75 76 77 78 79 80 81 82
default_read() {
	local n
	read n
	[ -z "$n" ] && n="$2"
	eval "$1=\"$n\""
}


cfg_add() {
	[ -z "$NOCOMMIT" ] && lbu_add "$@"
}
83

Natanael Copa's avatar
Natanael Copa committed
84 85 86 87 88 89 90 91
# return true if given value is 1, Y, Yes, yes YES etc
yesno() {
	case $1 in
	[Yy]|[Yy][Ee][Ss]|1|[Tt][Rr][Uu][Ee]) return 0;;
	esac
	return 1
}

92 93 94 95 96 97 98 99
# Detect if we are running Xen
is_xen() {
	test -d /proc/xen
}

# Detect if we are running Xen Dom0
is_xen_dom0() {
	is_xen && \
Eivind Uggedal's avatar
Eivind Uggedal committed
100
	grep -q "control_d" /proc/xen/capabilities 2>/dev/null
101
}
102

Dubiousjim's avatar
Dubiousjim committed
103
# list of available network interfaces that aren't part of any bridge or bond
104
available_ifaces() {
105
	local iflist= ifpath= iface= i=
106 107 108 109
	if ! [ -d /sys/class/net ]; then
		ip link | awk -F: '$1 ~ /^[0-9]+$/ {printf "%s",$2}'
		return
	fi
110 111 112 113 114 115 116 117 118 119 120 121 122 123 124
	sorted_ifindexes=$(
		for i in /sys/class/net/*/ifindex; do
			[ -e "$i" ] || continue
			echo -e "$(cat $i)\t$i";
		done | sort -n | awk '{print $2}')
	for i in $sorted_ifindexes; do
		ifpath=${i%/*}
		iface=${ifpath##*/}
		# skip interfaces that are part of a bond or bridge
		if [ -d "$ifpath"/master/bonding ] || [ -d "$ifpath"/brport ]; then
			continue
		fi
		iflist="${iflist}${iflist:+ }$iface"
	done
	echo $iflist
125 126
}

127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161
# from OpenBSD installer

# Ask for a password, saving the input in $resp.
#    Display $1 as the prompt.
#    *Don't* allow the '!' options that ask does.
#    *Don't* echo input.
#    *Don't* interpret "\" as escape character.
askpass() {
	echo -n "$1 "
	set -o noglob
	stty -echo
	read -r resp
	stty echo
	set +o noglob
	echo
}

# Ask for a password twice, saving the input in $_password
askpassword() {
	local _oifs=$IFS
	IFS=
	while :; do
		askpass "Password for $1 account? (will not echo)"
		_password=$resp

		askpass "Password for $1 account? (again)"
		# N.B.: Need quotes around $resp and $_password to preserve leading
		#       or trailing spaces.
		[ "$resp" = "$_password" ] && break

		echo "Passwords do not match, try again."
	done
	IFS=$_oifs
}

162 163 164 165 166 167 168 169
# test the first argument against the remaining ones, return success on a match
isin() {
	local _a=$1 _b
	shift
	for _b; do
		[ "$_a" = "$_b" ] && return 0
	done
	return 1
170
}
171

172 173 174
# remove all occurrences of first argument from list formed by
# the remaining arguments
rmel() {
175
	local _a=$1 _b
176 177 178 179 180 181 182

	shift
	for _b; do
		[ "$_a" != "$_b" ] && echo -n "$_b "
	done
}

183
# Issue a read into the global variable $resp.
184 185 186 187 188
_ask() {
	local _redo=0

	read resp
	case "$resp" in
189
	!)	echo "Type 'exit' to return to setup."
190 191 192
		sh
		_redo=1
		;;
193
	!*)	eval "${resp#?}"
194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216
		_redo=1
		;;
	esac
	return $_redo
}

# Ask for user input.
#
#    $1    = the question to ask the user
#    $2    = the default answer
#
# Save the user input (or the default) in $resp.
#
# Allow the user to escape to shells ('!') or execute commands
# ('!foo') before entering the input.
ask() {
	local _question=$1 _default=$2

	while :; do
		echo -n "$_question "
		[ -z "$_default" ] || echo -n "[$_default] "
		_ask && : ${resp:=$_default} && break
	done
217
}
218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272

# Ask for user input until a non-empty reply is entered.
#
#    $1    = the question to ask the user
#    $2    = the default answer
#
# Save the user input (or the default) in $resp.
ask_until() {
	resp=
	while [ -z "$resp" ] ; do
		ask "$1" "$2"
	done
}

# Ask for the user to select one value from a list, or 'done'.
#
# $1 = name of the list items (disk, cd, etc.)
# $2 = question to ask
# $3 = list of valid choices
# $4 = default choice, if it is not specified use the first item in $3
#
# N.B.! $3 and $4 will be "expanded" using eval, so be sure to escape them
#       if they contain spooky stuff
#
# At exit $resp holds selected item, or 'done'
ask_which() {
	local _name=$1 _query=$2 _list=$3 _def=$4 _dynlist _dyndef

	while :; do
		# Put both lines in ask prompt, rather than use a
		# separate 'echo' to ensure the entire question is
		# re-ask'ed after a '!' or '!foo' shell escape.
		eval "_dynlist=\"$_list\""
		eval "_dyndef=\"$_def\""

		# Clean away whitespace and determine the default
		set -o noglob
		set -- $_dyndef; _dyndef="$1"
		set -- $_dynlist; _dynlist="$*"
		set +o noglob
		[ $# -lt 1 ] && resp=done && return

		: ${_dyndef:=$1}
		echo "Available ${_name}s are: $_dynlist."
		echo -n "Which one $_query? (or 'done') "
		[ -n "$_dyndef" ] && echo -n "[$_dyndef] "
		_ask || continue
		[ -z "$resp" ] && resp="$_dyndef"

		# Quote $resp to prevent user from confusing isin() by
		# entering something like 'a a'.
		isin "$resp" $_dynlist done && break
		echo "'$resp' is not a valid choice."
	done
}