#
# Copyright 2008 Sun Microsystems, Inc.  All rights reserved.
# Use is subject to license terms.
#
# ident	"@(#)i.EtcDefLu	1.8	08/06/25 SMI"
#

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"

	# Determine whether the zfs file system type has been added
	# to either the LU_UNSUPPORTED_FILE_SYSTEM_TYPES variable or
	# the LU_SUPPORTED_FILE_SYSTEM_TYPES variable.  If it's
	# already been added to either one, leave it where it is.
	# If it hasn't been added to either one, we will add it
	# later to LU_UNSUPPORTED_FILE_SYSTEM_TYPES.

	zfs_found_in_unsupported=false
	zfs_found_in_supported=false

	# first check for zfs in LU_UNSUPPORTED_FILE_SYSTEM_TYPES
	for i in `/bin/sed \
	     '/^LU_UNSUPPORTED_FILE_SYSTEM_TYPES/!d;s/.*\"\(.*\)\"/\1/' \
	     < ${dest}.old` ; do
		[ $i = zfs ] && zfs_found_in_unsupported=true
	done 

	# now check for zfs in LU_SUPPORTED_FILE_SYSTEM_TYPES
	for i in `/bin/sed \
	     '/^LU_SUPPORTED_FILE_SYSTEM_TYPES/!d;s/.*\"\(.*\)\"/\1/' \
	     < ${dest}.old` ; do
		[ $i = zfs ] && zfs_found_in_supported=true
	done 

	while read line ; do
		# if we're at the end of the modifiable section, then just
		# copy the rest; loop will terminate due to end of file.
		echo "$line" | /bin/grep '^# CAUTION: DO NOT EDIT' >/dev/null
		if [ $? -eq 0 ]; then
			echo "$line" >> $dest
			cat >> $dest
			continue
		fi

		# 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 we're processing the LU_UNSUPPORTED_SYSTEM_TYPES
		# variable, we need to add the "zfs" file system type to
		# the list of unsupported file systems, (i.e., the file
		# systems transferred by *reference* to the new BE,
		# not copied to the new BE.), for now.  If "zfs"
		# doesn't appear in either the LU_UNSUPPORTED_FILE_SYSTEM_TYPES
		# or the LU_SUPPORTED_FILE_SYSTEM_TYPES, add it to
		# LU_UNSUPPORTED_FILE_SYSTEM_TYPES.  If "zfs" has already
		# been added to either list, just leave things as they are
		# (i.e., don't override the administrator's explicit
		# configuration of zfs handling.
		if [ "$varname" = "LU_UNSUPPORTED_FILE_SYSTEM_TYPES" -a \
		     "$zfs_found_in_supported" = "false" -a \
		     "$zfs_found_in_unsupported" = "false" ] ; then
			oldval=`echo $oldsetting | sed 's/.*\"\(.*\)\"/\1/'`
			oldval="$oldval zfs"
			oldsetting="${varname}=\"${oldval}\""
		fi

		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 < $src

	# 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

	# Make sure the new file has the private section in it.  It's required.
	/bin/grep '^# CAUTION: DO NOT EDIT' $dest >/dev/null
	if [ $? -ne 0 ]; then
		echo "################################################################################" >> $dest
		echo "#" >> $dest
		/bin/sed -n -e '/^# CAUTION: DO NOT EDIT/,$p' $src >> $dest
	fi

	/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
