#
# ident "@(#)i.EtcDefLu 1.5     05/10/24 SMI"
#
# Copyright (c) 2004 by Sun Microsystems, Inc.
# All rights reserved.
#

trap '' 1 2 3 15

#
# The etcdeflu class is used used to update the /etc/default/lu file.
# The original file is renamed and the new file is created, updating
# the contents of the new file with settings from the old file.
#
while read src dest ; do
	# If the destination file does not exist, then a simple copy will suffice.
	if [ ! -f "${dest}" ] ; then
		echo "Installing ${dest}"
		/bin/cp -p "${src}" "${dest}"
		continue
	fi

	# The destination file exists: must update new with contents from old.
	echo "Updating ${dest}"
	/bin/rm -f "${dest}.old" 2>/dev/null
	/bin/mv -f "${dest}" "${dest}.old"
	/bin/rm -f "${dest}" 2>/dev/null
	/bin/rm -f "${dest}.new" 2>/dev/null
	/bin/cp -p "${src}" "${dest}.new"

	/bin/cat "${src}" | while read line ; do
		# comment lines copied without modification
		echo "${line}" | /bin/grep '^#' 2>/dev/null 1>&2
		if [ "$?" -eq "0" ] ; then
			# Line is a comment line (begins with #).
			echo "${line}" >> "${dest}"
			continue
		fi

		# non-variable assignment lines pass without modification.
		echo "${line}" | /bin/grep '..*=.*$' 2>/dev/null 1>&2
		if [ "$?" -ne "0" ] ; then
			# Line is not an assignment statement.
			echo "${line}" >> "${dest}"
			continue
		fi

		# Line is an assignment statement - see if value should always be set.
		echo "${line}" | /bin/grep '^LU_PLUGIN' 2>/dev/null 1>&2
		if [ "$?" -eq "0" ] ; then
			# plugins are always reset.
			echo "${line}" >> "${dest}"
			continue
		fi

		# Line is an assignment statement - see if value set in old file.
		varname="`echo \"${line}\" | /bin/cut -f 1 -d'='`"
		newsetting="${line}"
		oldsetting="`/bin/grep \"^${varname}=..*$\" ${dest}.old | /bin/tail -1`"
		if [ -n "${oldsetting}" -a "${newsetting}" != "${oldsetting}" ] ; then
			# Old file has a setting - write old setting to the new file.
			echo "${oldsetting}" >> "${dest}"
			# Notify user that old setting is overriding original setting.
			echo "${dest}: Old setting <${oldsetting}> preserved over new setting <${line}>"
		else
			# Old file does NOT have a setting - write new setting to the new file.
			echo "${line}" >> "${dest}"
		fi
	done

	# Make sure that all plugins listed in the original file are
	# carried forward to the new file since they are added by
	# other packages besides SUNWluu and SUNWlur - scan the original
	# configuration file and make sure that all LU_PLUGIN entries
	# are represented in the new configuration file.

	/bin/grep '^LU_PLUGIN' "${dest}.old" | while read line ; do
		# get LU_PLUGIN variable from original configuration file
		varname="`echo \"${line}\" | /bin/cut -f 1 -d'='`"
		# determine if variable is set in new configuration file
		currsetting="`/bin/grep \"^${varname}=..*$\" ${dest} | /bin/tail -1`"
		if [ -z "$currsetting" ] ; then
			echo "${line}" >> "${dest}"
		fi
	done

	/bin/chown root:bin "${dest}"
	/bin/chmod 0444 "${dest}"
	/bin/rm -f "${dest}.old" 2>/dev/null
	/bin/rm -f "${dest}.new" 2>/dev/null
done

exit 0
