xref: /freebsd/tools/tools/nanobsd/Files/root/save_cfg (revision 61e21613)
1#!/bin/sh
2#
3# Copyright (c) 2006 Mathieu Arnold
4# Copyright (c) 2010 Alex Bakhtin
5# All rights reserved.
6#
7# Redistribution and use in source and binary forms, with or without
8# modification, are permitted provided that the following conditions
9# are met:
10# 1. Redistributions of source code must retain the above copyright
11#    notice, this list of conditions and the following disclaimer.
12# 2. Redistributions in binary form must reproduce the above copyright
13#    notice, this list of conditions and the following disclaimer in the
14#    documentation and/or other materials provided with the distribution.
15#
16# THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
17# ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
18# IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
19# ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
20# FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
21# DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
22# OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
23# HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
24# LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
25# OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
26# SUCH DAMAGE.
27#
28#
29
30set -e
31
32trap "umount /cfg" 1 2 15 EXIT
33mount /cfg
34(
35cd /etc
36for filename in "$@" `find * -type f`
37do
38	if [ ! -f /cfg/$filename -a ! -f /cfg/.ignore/$filename ]
39	then
40
41		#
42		# If file doesn't exist in /cfg and file is not in the 'ignore' list
43		# then check if this file is exactly the same as original file
44		# in nanobsd image
45		#
46		if ! cmp -s /etc/$filename /conf/base/etc/$filename
47		then
48			file_path=`echo "$filename" | sed 's/\/[^/]*$//'`
49			if [ $file_path != $filename ]
50			then
51				if [ ! -d /etc/$file_path ]
52				then
53					# should never go here unless we have some errors in
54					# sed script extracting file path
55					echo "Error: Path /etc/$file_path is not directory."
56					exit 1;
57				fi
58			fi
59
60			#
61			# Ask user - how should we handle this file.
62			# Add to cfg (y/n/i)?
63			#	y) -> save this file in /cfg
64			#	n) -> do not save this file in /cfg for current script invocation ONLY
65			#	i) -> add file to ignore list (/cfg/.ignore hiereachy) and never save
66			#	      try to add this file to /cfg.
67			#
68			# touch is used to add files to /cfg to keep the script flow straight and easy
69			#
70			read -p "New file /etc/$filename found. Add to /cfg (y/n/i)? " key
71			case "$key" in
72			[yY])
73				if [ $file_path != $filename ]
74				then
75					mkdir -vp /cfg/$file_path
76				fi
77				touch /cfg/$filename && echo "File /etc/$filename added to /cfg."
78				;;
79			[iI])
80				mkdir -vp /cfg/.ignore
81				if [ $file_path != $filename ]
82				then
83					mkdir -vp /cfg/.ignore/$file_path
84				fi
85				touch /cfg/.ignore/$filename && echo "File /etc/$filename added to ignore list."
86				;;
87			esac
88		fi
89	fi
90done
91
92#
93# Actually check all files in /cfg and save if necessary
94#
95cd /cfg
96for filename in "$@" `find * -type f`
97do
98	if [ -f /etc/$filename ]
99	then
100        	cmp -s /etc/$filename /cfg/$filename || cp -pfv /etc/$filename /cfg/$filename
101	else
102
103		#
104		# Give user an option to remove file from /cfg if this file is removed from /etc
105		#
106		read -p "File /cfg/$filename not found in /etc. Remove from /cfg (y/n)? " key
107		case "$key" in
108		[yY])
109			rm /cfg/$filename && echo "File /cfg/$filename removed"
110			;;
111		esac
112	fi
113done
114
115)
116umount /cfg
117trap 1 2 15 EXIT
118