libalpine.sh.in 1.8 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 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22

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

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

Natanael Copa's avatar
Natanael Copa committed
28 29 30 31 32 33 34 35 36 37
# echo unless quiet mode
qecho() {
	[ -z "$QUIET" ] && echo "$@"
}

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

Natanael Copa's avatar
Natanael Copa committed
38 39 40 41 42 43
# echo to stderr and exit with error
die() {
	eecho "$@"
	exit 1
}

Natanael Copa's avatar
Natanael Copa committed
44 45
init_tmpdir() {
	local omask=`umask`
Natanael Copa's avatar
Natanael Copa committed
46
	local __tmpd="/tmp/$PROGRAM-${$}-`date +%s`-$RANDOM"
Natanael Copa's avatar
Natanael Copa committed
47
	umask 077 || die "umask"
Natanael Copa's avatar
Natanael Copa committed
48
	mkdir -p "$__tmpd" || exit 1
Natanael Copa's avatar
Natanael Copa committed
49 50 51 52 53 54
	trap "rm -fr \"$__tmpd\"; exit" 0
	umask $omask
	eval "$1=\"$__tmpd\""
}

pkg_inst() {
Natanael Copa's avatar
Natanael Copa committed
55
	[ -z "$NOCOMMIT" ] && apk add -q $*
Natanael Copa's avatar
Natanael Copa committed
56 57
}

58
pkg_deinst() {
Natanael Copa's avatar
Natanael Copa committed
59
	[ -z "$NOCOMMIT" ] && apk del -q $*
60 61
}

Natanael Copa's avatar
Natanael Copa committed
62 63 64 65 66 67 68 69 70 71
default_read() {
	local n
	read n
	[ -z "$n" ] && n="$2"
	eval "$1=\"$n\""
}


invalid_ip() {
	[ "$1" ] || return 0
72
	! ipcalc -s -m $1/0 > /dev/null
Natanael Copa's avatar
Natanael Copa committed
73 74 75 76 77 78
}


cfg_add() {
	[ -z "$NOCOMMIT" ] && lbu_add "$@"
}
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

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