1# Sample dhcpcd hook for ypbind
2# This script is only suitable for the Linux version.
3
4: ${ypbind_restart_cmd:=service_command ypbind restart}
5: ${ypbind_stop_cmd:=service_condcommand ypbind stop}
6ypbind_dir="$state_dir/ypbind"
7
8best_domain()
9{
10	local i=
11
12	for i in $interfaces; do
13		if [ -e "$ypbind_dir/$i" ]; then
14			cat "$ypbind_dir/$i"
15		fi
16	done
17	return 1
18}
19
20make_yp_binding()
21{
22	[ -d "$ypbind_dir" ] || mkdir -p "$ypbind_dir"
23	echo "$new_nis_domain" >"$ypbind_dir/$interface"
24	local nd="$(best_domain)"
25
26	local cf=/var/yp/binding/"$new_nis_domain".ypservers
27	if [ -n "$new_nis_servers" ]; then
28		local ncf="$cf.$interface" x=
29		rm -f "$ncf"
30		for x in $new_nis_servers; do
31			echo "$x" >>"$ncf"
32		done
33		change_file "$cf" "$ncf"
34	else
35		# Because this is not an if .. fi then we can use $? below
36		[ -e "$cf" ] && rm "$cf"
37	fi
38
39	if [ $? = 0 -o "$nd" != "$(domainname)" ]; then
40		domainname "$nd"
41		if [ -n "$ypbind_restart_cmd" ]; then
42			eval $ypbind_restart_cmd
43		fi
44	fi
45}
46
47restore_yp_binding()
48{
49	rm -f "$ypbind_dir/$interface"
50	local nd="$(best_domain)"
51	# We need to stop ypbind if there is no best domain
52	# otherwise it will just stall as we cannot set domainname
53	# to blank :/
54	if [ -z "$nd" ]; then
55		if [ -n "$ypbind_stop_cmd" ]; then
56			eval $ypbind_stop_cmd
57		fi
58	elif [ "$nd" != "$(domainname)" ]; then
59		domainname "$nd"
60		if [ -n "$ypbind_restart_cmd" ]; then
61			eval $ypbind_restart_cmd
62		fi
63	fi
64}
65
66if [ "$reason" = PREINIT ]; then
67	rm -f "$ypbind_dir/$interface"
68elif $if_up || $if_down; then
69	if [ -n "$new_nis_domain" ]; then
70		make_yp_binding
71	elif [ -n "$old_nis_domain" ]; then
72		restore_yp_binding
73	fi
74fi
75