xref: /netbsd/sbin/newbtconf/newbtconf.sh (revision 857d186a)
1#!/bin/sh
2#
3# Setup a new config directory
4#
5if [ $# -lt 1 ] ; then
6	echo "usage: $0 <newconfig> [<baseconfig>]"
7	echo "usage: $0 init"
8	echo "usage: $0 revert"
9	exit 1;
10fi
11dir=$1
12
13FILES="defaultdomain fstab ifconfig.* inetd.conf mrouted.conf \
14	mygate myname netstart nsswitch.conf ntp.conf \
15	rc.conf rc.conf.d resolv.conf"
16
17if [ $dir = init ] ; then
18	if [ -d /etc/etc.network ] || [ -e /etc/etc.current ] ; then
19		echo "Error: multi-configuration already initialized"
20		exit 1
21	fi
22	dir=etc.network
23	cd /etc
24	mkdir -m 755 $dir
25	ln -s $dir etc.current
26	ln -s $dir etc.default
27	for i in ${FILES}; do
28		if [ -f $i ] || [ -d $i ] ; then
29			mv $i $dir
30			ln -s etc.current/$i .
31		fi
32	done
33	echo "/etc/$dir has now been created and populated."
34	exit 0
35fi
36
37if [ $dir = revert ] ; then
38	if [ !  -d /etc/etc.current ] ; then
39		echo "Error: multi-configuration not initialized"
40		exit 1
41	fi
42	cd /etc
43	for i in ${FILES}; do
44		if [ -f $i ] || [ -d $i ] ; then
45			stat="`ls -ld $i`"
46			case x"$stat" in
47				xl*) :;;
48				x*)
49				echo "$i: not a symlink, skipping"
50				continue ;;
51			esac
52			linkto="${stat##*-> }"
53			case x"$linkto" in
54				xetc.current/*) :;;
55				x*)
56				echo "$i: does not symlink to etc.current, skipping"
57				continue ;;
58			esac
59			if [ -f $i ] ; then
60				rm $i
61				cp -p $linkto $i
62			else
63				rm $i
64				( cd etc.current && pax -rw -pe $i /etc )
65			fi
66		fi
67	done
68	rm etc.current
69	rm etc.default
70	exit 0
71fi
72
73if [ "`expr $dir : 'etc\.\(.*\)'`" != $dir ] ; then
74	dir=etc.$dir
75fi
76if [ -e /etc/$dir ] ; then
77	echo "Error: $dir already exists"
78	exit 1;
79fi
80newname=`expr $dir : 'etc.\(.*\)'`
81if [ $# -lt 2 ] ; then
82	orig=etc.current
83	echo "Using current config as base for $newname"
84else
85	orig=$2
86fi
87
88if [ -z "`expr $orig : 'etc.\(.*\)'`" ] ; then
89	orig=etc.$orig
90fi
91
92if [ ! -d /etc/$orig ] ; then
93	echo "Original directory /etc/$orig does not exist."
94	exit 1;
95fi
96mkdir -m 755 /etc/$dir
97cd /etc/$orig
98pax -rw -pe . /etc/$dir
99echo "/etc/$dir has now been created and populated."
100exit 0
101