summaryrefslogtreecommitdiff
path: root/package/base-files/src/etc/functions.sh
blob: 8b2b3d66588f244bafab16772987d44013bf052d (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
# newline
N="
"

append() {
	local var="$1"
	local value="$2"
	local sep="${3:- }"

	eval "export -- \"$var=\${$var:+\${$var}\${value:+\$sep}}\$value\""
}

load_modules() {
	if [ -d /lib/modules/$(uname -r) ]; then
  		(sed "s,^[^#][^[:space:]]*,insmod /lib/modules/$(uname -r)/&.ko," $* | sh 2>&- || :)
	fi
}

user_exists() {
	grep -q "^$1:" $IPKG_INSTROOT/etc/passwd 2>&-
}

group_exists() {
	grep -q "^$1:" $IPKG_INSTROOT/etc/group 2>&-
}

service_exists() {
	grep -q "^$1[[:space:]]*$2" $IPKG_INSTROOT/etc/services 2>&-
}

rcconf_exists() {
	grep -q "^#*$1=" $IPKG_INSTROOT/etc/rc.conf 2>&-
}

add_user() {
	user_exists $1 || {
		echo "adding user $1 to /etc/passwd"
		echo "$1:x:$2:${3:-$2}:$1:${4:-/tmp}:${5:-/bin/false}" \
		    >>$IPKG_INSTROOT/etc/passwd
	}
}

add_group() {
	group_exists $1 || {
		echo "adding group $1 to /etc/group"
		echo "$1:x:$2:$3" >>$IPKG_INSTROOT/etc/group
	}
}

add_service() {
	service_exists $1 $2 || {
		echo "adding service $1 to /etc/services"
		printf '%s\t%s\n' "$1" "$2" >>$IPKG_INSTROOT/etc/services
	}
}

add_rcconf() {
	rcconf_exists $1 || {
		echo "adding service $1 to /etc/rc.conf"
		printf '%s="%s"\n' "${1}" "${2:-NO}" \
			>>$IPKG_INSTROOT/etc/rc.conf
	}
}

get_next_uid() {
	uid=1
	while grep "^[^:]*:[^:]*:$uid:" $IPKG_INSTROOT/etc/passwd \
	    >/dev/null 2>&1; do
		uid=$(($uid+1))
	done
	echo $uid
}

get_next_gid() {
	gid=1
	while grep "^[^:]*:[^:]*:$gid:" $IPKG_INSTROOT/etc/group \
	    >/dev/null 2>&1; do
		gid=$(($gid+1))
	done
	echo $gid
}