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# $FreeBSD: stable/11/tools/tools/nanobsd/Files/root/save_cfg 309884 2016-12-12 11:02:55Z brueffer $
29#
30
31set -e
32
33trap "umount /cfg" 1 2 15 EXIT
34mount /cfg
35(
36cd /etc
37for filename in "$@" `find * -type f`
38do
39	if [ ! -f /cfg/$filename -a ! -f /cfg/.ignore/$filename ]
40	then
41
42		#
43		# If file doesn't exist in /cfg and file is not in the 'ignore' list
44		# then check if this file is exactly the same as original file
45		# in nanobsd image
46		#
47		if ! cmp -s /etc/$filename /conf/base/etc/$filename 
48		then
49			file_path=`echo "$filename" | sed 's/\/[^/]*$//'`
50			if [ $file_path != $filename ]
51			then
52				if [ ! -d /etc/$file_path ]
53				then
54					# should never go here unless we have some errors in
55					# sed script extracting file path
56					echo "Error: Path /etc/$file_path is not directory."
57					exit 1;
58				fi
59			fi
60
61			#
62			# Ask user - how should we handle this file.
63			# Add to cfg (y/n/i)?
64			#	y) -> save this file in /cfg
65			#	n) -> do not save this file in /cfg for current script invocation ONLY
66			#	i) -> add file to ignore list (/cfg/.ignore hiereachy) and never save
67			#	      try to add this file to /cfg.
68			#
69			# touch is used to add files to /cfg to keep the script flow straight and easy
70			#
71			read -p "New file /etc/$filename found. Add to /cfg (y/n/i)? " key
72			case "$key" in
73			[yY])
74				if [ $file_path != $filename ]
75				then
76					mkdir -vp /cfg/$file_path
77				fi
78				touch /cfg/$filename && echo "File /etc/$filename added to /cfg."
79				;;
80			[iI])
81				mkdir -vp /cfg/.ignore
82				if [ $file_path != $filename ]
83				then
84					mkdir -vp /cfg/.ignore/$file_path
85				fi
86				touch /cfg/.ignore/$filename && echo "File /etc/$filename added to ignore list."
87				;;
88			esac
89		fi
90	fi
91done
92
93#
94# Actually check all files in /cfg and save if necessary
95#
96cd /cfg
97for filename in "$@" `find * -type f`
98do
99	if [ -f /etc/$filename ]
100	then
101        	cmp -s /etc/$filename /cfg/$filename || cp -pfv /etc/$filename /cfg/$filename
102	else
103
104		#
105		# Give user an option to remove file from /cfg if this file is removed from /etc
106		#
107		read -p "File /cfg/$filename not found in /etc. Remove from /cfg (y/n)? " key
108		case "$key" in
109		[yY])
110			rm /cfg/$filename && echo "File /cfg/$filename removed"
111			;;
112		esac
113	fi
114done
115
116)
117umount /cfg
118trap 1 2 15 EXIT
119