#! /bin/sh
#
# Copyright (c) 2006 Florent Thoumie
# All rights reserved.
#
# Redistribution and use in source and binary forms, with or without
# modification, are permitted provided that the following conditions
# are met:
# 1. Redistributions of source code must retain the above copyright
#    notice, this list of conditions and the following disclaimer.
# 2. Redistributions in binary form must reproduce the above copyright
#    notice, this list of conditions and the following disclaimer in the
#    documentation and/or other materials provided with the distribution.
#
# THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
# ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
# IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
# ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
# FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
# DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
# OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
# HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
# LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
# OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
# SUCH DAMAGE.
#
# $FreeBSD$
#
# Script to synchronize /etc with configuration partition 1 on a NanoBSD system.
#
# usage:
#	sync-etc [-hin] [-b basedir] [-d destdir]
#

exclude="--exclude=log"

base=/conf/base/etc
dest=/cfg

usage()
{
	echo "usage: `basename $0` [-hin] [-b basedir] [-d destdir]"
}

tmpfile="/var/tmp/sync-etc.XXXXXX"
tmpfile=`mktemp -q ${tmpfile}`

args=`getopt b:d:hin $*`
if [ $? -ne 0 ]; then
        usage
        exit 2
fi

set -- $args

for i; do
        case "$i" in
	-b)
		base="$2"; shift; shift;;
	-d)
		dest="$2"; shift; shift;;
	-h)
		usage; exit 0;;
        -i)
                opt_i="yes"; shift;;
        -n)
                opt_n="yes"; shift;;
	--)
		shift; break;;
        esac
done

mnt_u=`mount | grep ${dest}`
if [ x"${mnt_u}" != "x" ]; then
	mnt_u="-u"
fi

echo "Mounting ${dest} read-write..."
mount ${mnt_u} -w ${dest}
if [ $? -ne 0 ]; then
        exit 1
fi

echo "Searching modified files..."

diff ${exclude} -ruN ${base} /etc/ | grep -- '+++ /' | sed -e "s#^+++ /etc/\([^	]*\).*#\1#" > ${tmpfile}

echo "Creating missing directories..."

for i in `cat ${tmpfile}`; do
	dir=`dirname ${i}`
	if [ ! -d ${dest}/${dir} ]; then
		mkdir ${dest}/${dir}
	fi
done

echo "Synchronizing /etc with ${dest}..."

for i in `cat ${tmpfile}`; do
	if [ ! -f /etc/${i} ]; then
		continue
	fi
	md5src=`md5 -q /etc/${i}`
	md5dst=`md5 -q ${dest}/${i} 2>/dev/null`
	if [ ! -f ${dest}/${i} -o x${md5src} != x${md5dst} ]; then
		if [ ! -z ${opt_n} ]; then
			echo "/etc/${i} -> ${dest}/${i}"
		else
			if [ ! -z ${opt_i} ]; then
				echo -n "Copy /etc/${i} to ${dest}/${i} [yN] ? "
				read answer
				case "${answer}" in
				[yY])
					cp -pv /etc/${i} ${dest}/${i};;
				*)
					;;
				esac
			else
				cp -pv /etc/${i} ${dest}/${i}
			fi
		fi
	fi
done

if [ ! -z "${mnt_u}" ]; then
	echo "Remounting ${dest} read-only..."
	mount ${mnt_u} -r ${dest}
else
	echo "Umounting ${dest}..."
	umount ${dest}
fi

echo "Cleaning files..."
rm -f ${tempdir}
