summaryrefslogtreecommitdiff
path: root/libalpine.sh.in
blob: 569264cf4619ef1e93a65ae712d0d1f7b04562c4 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
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
109
110
111
112
113
114
115
#!/bin/sh

PREFIX=@PREFIX@

PROGRAM=`basename $0`

echon () {
	if [ X"$ECHON" = X ]; then
		# Determine how to "echo" without newline: "echo -n"
		# or "echo ...\c"
		if [ X`echo -n` = X-n ]; then
			ECHON=echo
			NNL="\c"
			# "
		else
			ECHON="echo -n"
			NNL=""
		fi
	fi
	$ECHON "$*$NNL"
}

# echo if in verbose mode
vecho() {
	[ -n "$VERBOSE" ] && echo "$@"
}

# echo unless quiet mode
qecho() {
	[ -z "$QUIET" ] && echo "$@"
}

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

# echo to stderr and exit with error
die() {
	eecho "$@"
	exit 1
}

init_tmpdir() {
	local omask=`umask`
	local __tmpd="/tmp/$PROGRAM-${$}-`date +%s`-$RANDOM"
	umask 077 || die "umask"
	mkdir -p "$__tmpd" || exit 1
	trap "rm -fr \"$__tmpd\"; exit" 0
	umask $omask
	eval "$1=\"$__tmpd\""
}

pkg_inst() {
	[ -z "$NOCOMMIT" ] && apk add -q $*
}

pkg_deinst() {
	[ -z "$NOCOMMIT" ] && apk del -q $*
}

default_read() {
	local n
	read n
	[ -z "$n" ] && n="$2"
	eval "$1=\"$n\""
}


invalid_ip() {
	[ "$1" ] || return 0
	! ipcalc -s $1
}


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

# 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
}