summaryrefslogtreecommitdiff
path: root/setup-dns.in
blob: 25a2ce5360e2d31fa69448756bed3aa79facea53 (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
#!/bin/sh

PREFIX=@PREFIX@
: ${LIBDIR=$PREFIX/lib}
. "$LIBDIR/libalpine.sh"

usage() {
	cat <<-__EOF__
		usage: setup-dns [-h] [-d DOMAINNAME] [IPADDR...]

		Setup ${ROOT}etc/resolv.conf DNS settings

		options:
		 -h  Show this help
		 -d  specify search domain name

		The optional IPADDR are a list of DNS servers to use.
	__EOF__
	exit $1
}

while getopts "d:n:h" opt; do
	case $opt in
		d) DOMAINNAME="$OPTARG";;
		h) usage 0;;
		n) NAMESERVERS="$OPTARG";;
		'?') usage "1" >&2;;
	esac
done
shift $(($OPTIND - 1))


conf="${ROOT}etc/resolv.conf"

if [ -f "$conf" ] ; then
	domain=$(awk '/^domain/ {print $2}' $conf)
	dns=$(awk '/^nameserver/ {printf "%s ",$2}' $conf)
elif fqdn="$(get_fqdn)" && [ -n "$fqdn" ]; then
	domain="$fqdn"
fi

if [ -n "$DOMAINNAME" ]; then
	domain="$DOMAINNAME"
elif [ $# -eq 0 ]; then
	ask "DNS domain name? (e.g 'bar.com')" "$domain"
	domain="$resp"
fi

if [ -n "$NAMESERVERS" ] || [ $# -gt 0 ];then
	dns="$NAMESERVERS"
else
	ask "DNS nameserver(s)?" "${dns% }"
	dns="$(echo $resp | tr ',' ' ')"
fi

if [ -n "$domain" ]; then
	mkdir -p "${conf%/*}"
	echo "search $domain" > $conf
fi

if [ -n "$dns" ] || [ $# -gt 0 ] && [ -f "$conf" ]; then
	sed -i -e '/^nameserver/d' $conf
fi
for i in $dns $@; do
	mkdir -p "${conf%/*}"
	echo "nameserver $i" >> $conf
done