1178369Sphk#!/bin/sh
2178369Sphk#
3178369Sphk# Copyright (c) 2006 Mathieu Arnold
4263189Simp# Copyright (c) 2010 Alex Bakhtin
5178369Sphk# All rights reserved.
6178369Sphk#
7178369Sphk# Redistribution and use in source and binary forms, with or without
8178369Sphk# modification, are permitted provided that the following conditions
9178369Sphk# are met:
10178369Sphk# 1. Redistributions of source code must retain the above copyright
11178369Sphk#    notice, this list of conditions and the following disclaimer.
12178369Sphk# 2. Redistributions in binary form must reproduce the above copyright
13178369Sphk#    notice, this list of conditions and the following disclaimer in the
14178369Sphk#    documentation and/or other materials provided with the distribution.
15178369Sphk#
16178369Sphk# THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
17178369Sphk# ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
18178369Sphk# IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
19178369Sphk# ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
20178369Sphk# FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
21178369Sphk# DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
22178369Sphk# OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
23178369Sphk# HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
24178369Sphk# LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
25178369Sphk# OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
26178369Sphk# SUCH DAMAGE.
27178369Sphk#
28178369Sphk# $FreeBSD: stable/11/tools/tools/nanobsd/Files/root/save_cfg 309884 2016-12-12 11:02:55Z brueffer $
29178369Sphk#
30178369Sphk
31178369Sphkset -e
32178369Sphk
33178369Sphktrap "umount /cfg" 1 2 15 EXIT
34178369Sphkmount /cfg
35178369Sphk(
36263189Simpcd /etc
37263189Simpfor filename in "$@" `find * -type f`
38263189Simpdo
39263189Simp	if [ ! -f /cfg/$filename -a ! -f /cfg/.ignore/$filename ]
40263189Simp	then
41263189Simp
42263189Simp		#
43263189Simp		# If file doesn't exist in /cfg and file is not in the 'ignore' list
44263189Simp		# then check if this file is exactly the same as original file
45263189Simp		# in nanobsd image
46263189Simp		#
47263189Simp		if ! cmp -s /etc/$filename /conf/base/etc/$filename 
48263189Simp		then
49263189Simp			file_path=`echo "$filename" | sed 's/\/[^/]*$//'`
50263189Simp			if [ $file_path != $filename ]
51263189Simp			then
52263189Simp				if [ ! -d /etc/$file_path ]
53263189Simp				then
54263189Simp					# should never go here unless we have some errors in
55263189Simp					# sed script extracting file path
56263189Simp					echo "Error: Path /etc/$file_path is not directory."
57263189Simp					exit 1;
58263189Simp				fi
59263189Simp			fi
60263189Simp
61263189Simp			#
62263189Simp			# Ask user - how should we handle this file.
63263189Simp			# Add to cfg (y/n/i)?
64263189Simp			#	y) -> save this file in /cfg
65263189Simp			#	n) -> do not save this file in /cfg for current script invocation ONLY
66263189Simp			#	i) -> add file to ignore list (/cfg/.ignore hiereachy) and never save
67263189Simp			#	      try to add this file to /cfg.
68263189Simp			#
69309884Sbrueffer			# touch is used to add files to /cfg to keep the script flow straight and easy
70263189Simp			#
71263189Simp			read -p "New file /etc/$filename found. Add to /cfg (y/n/i)? " key
72263189Simp			case "$key" in
73263189Simp			[yY])
74263189Simp				if [ $file_path != $filename ]
75263189Simp				then
76263189Simp					mkdir -vp /cfg/$file_path
77263189Simp				fi
78263189Simp				touch /cfg/$filename && echo "File /etc/$filename added to /cfg."
79263189Simp				;;
80263189Simp			[iI])
81263189Simp				mkdir -vp /cfg/.ignore
82263189Simp				if [ $file_path != $filename ]
83263189Simp				then
84263189Simp					mkdir -vp /cfg/.ignore/$file_path
85263189Simp				fi
86263189Simp				touch /cfg/.ignore/$filename && echo "File /etc/$filename added to ignore list."
87263189Simp				;;
88263189Simp			esac
89263189Simp		fi
90263189Simp	fi
91263189Simpdone
92263189Simp
93263189Simp#
94263189Simp# Actually check all files in /cfg and save if necessary
95263189Simp#
96178369Sphkcd /cfg
97263189Simpfor filename in "$@" `find * -type f`
98178369Sphkdo
99263189Simp	if [ -f /etc/$filename ]
100263189Simp	then
101263189Simp        	cmp -s /etc/$filename /cfg/$filename || cp -pfv /etc/$filename /cfg/$filename
102263189Simp	else
103263189Simp
104263189Simp		#
105263189Simp		# Give user an option to remove file from /cfg if this file is removed from /etc
106263189Simp		#
107263189Simp		read -p "File /cfg/$filename not found in /etc. Remove from /cfg (y/n)? " key
108263189Simp		case "$key" in
109263189Simp		[yY])
110263189Simp			rm /cfg/$filename && echo "File /cfg/$filename removed"
111263189Simp			;;
112263189Simp		esac
113263189Simp	fi
114178369Sphkdone
115263189Simp
116178369Sphk)
117178369Sphkumount /cfg
118178369Sphktrap 1 2 15 EXIT
119