#!/bin/sh
#
#ident	"@(#)i.rmmconf	1.13	00/07/17 SMI"
#
# Copyright (c) 1997 by Sun Microsystems, Inc.
# All rights reserved.
#

PATH="/usr/bin:/usr/sbin:${PATH}"
export PATH

change_from_to()
{
	d="$1"
	old="$2"
	new="$3"

	sed -e "s/$2/$3/" $d > /tmp/tmp.$$
	cp /tmp/tmp.$$ $dest

	# clean up
	rm -f /tmp/*.$$
}

#
# update_action_line:
#
# takes three params:
#	1. dest file name;
#	2. new line to after last 'action ...' line;
#	3. old line to delete;
#
# algorithm:
#	if the "$new" line is already in the file "$d", then return, else
#	find the line number of the last "action ..." line in the "$d" file
#	add the supplied "$new" line after the current last "action ..." line
#	if the "$old" line is in the file, remove it
#
update_action_line()
{

	d="$1"
	new="$2"
	old="$3"

	# check for the "$new line"
	if fgrep "$new" $d >/dev/null 2>&1 ; then
		return		# no work needs to be done
	fi

	# find last '^action ...' line
	line_no="`grep -n '^action' $d|tail -1|awk -F: '{print $1}'`"

	# add new line after last '^action ...' line
	sed -e "$line_no a\\
$new" $d > /tmp/tmp.$$

	# copy temp file back to dest file, removing old line (if present)
	grep -v "^$old" /tmp/tmp.$$ > $d
}

update_ufs_ident_line()
{
        d="$1"

        # get the line and check if up to date
        if grep '^ident ufs .* rmdisk' $d >/dev/null 2>&1 ; then
                :
        else
                #edit ident ufs line
                sed -e '/^ident ufs ident_ufs/ s/$/ rmdisk/' \
                        $dest >/tmp/tmp.$$
                cp /tmp/tmp.$$ $dest

                # clean up
                rm -f /tmp/*.$$
        fi
}

update_pcfs_ident_line()
{
        d="$1"

        # check for the "$new line"
        if grep '^ident pcfs .* rmdisk' $d >/dev/null 2>&1 ; then
                :
        else
                #edit ident pcfs line
                sed -e '/^ident pcfs ident_pcfs/ s/$/ rmdisk/' \
                        $dest >/tmp/tmp.$$
                cp /tmp/tmp.$$ $dest

                # clean up
                rm -f /tmp/*.$$
        fi
}

add_udfs_ident_line()
{
	d="$1"
	new="$2"

	if grep '^ident udfs .* rmdisk' $d >/dev/null 2>&1 ; then
		:
	elif grep '^ident udfs' $d >/dev/null 2>&1 ; then
		# edit ident udfs line
                sed -e '/^ident udfs ident_udfs/ s/$/ rmdisk/' \
                       	$dest >/tmp/tmp.$$
		cp /tmp/tmp.$$ $dest
	else
		# find last '^ident ...' line
		line_no="`grep -n '^ident' $d|tail -1|awk -F: '{print $1}'`"

		# add new line after last '^ident ...' line
		sed -e "$line_no a\\
$new" $d > /tmp/tmp.$$
		cp /tmp/tmp.$$ $dest
	fi

	# clean up
	rm -f /tmp/*.$$
}

update_mount_line()
{

	d="$1"
	new="mount * hsfs udfs ufs -o nosuid"
	file="/tmp/tmp.$$"
	flag=0

	# check for the "new" line
	if fgrep "$new" $d >/dev/null 2>&1 ; then
		flag=1
	fi

nawk '

/^#/ {

	# Comment Line
	print;
	next
}

/^mount/ {

	# Mount Line

	pcfs_flag=0

	if ( match($0, /pcfs/) > 0 ) {
		if ( match($0, /[       ](ufs|udfs|hsfs)/) <= 0 ) {
			print;
			next
		}
		else {
			pcfs_flag=1;
		}
	}
	if ( match($0, /(nosuid|suid)/) > 0 ){
		print;
		next
	}
	else {
		if ( match($0, /-o/) > 0) {
			pos = index($0, "-o");
			string = substr($0, pos);
			if (pcfs_flag == 1) {
				print $1" " $2" " "pcfs " string;
				sub(/pcfs[      ]*/, "");
			}
			sub(/-o[        ]/, "-o nosuid,");
			print;
			next
		}
	}
}

{
	#Lines other than comment lines and those for mount options.
	print;
	next
}' $d > $file

if [ $flag -eq 0 ]
then
	echo "" >> $file
	echo "$new" >> $file
	cat $file > $d
else
	cat $file > $d
fi

# Clean-up
rm -f $file

}


while read src dest
do
	if [ ! -f $dest ] ; then
		cp $src $dest
	else
		# save the 'ident' line from the 'new' file
		grep '@(#)rmmount.conf' $src \
		    > /tmp/newident.$$ 2>/dev/null

		# save all but the 'ident' line from the old (orig) file
		grep -v '@(#)rmmount.conf' $dest \
		    > /tmp/rest.$$ 2>/dev/null

		# concatenate the new ident and the old data, replacing old
		cat /tmp/newident.$$ /tmp/rest.$$ > $dest

		# catch all just replace rmscsi with rmdisk everwhere
		# in action as well as any other file systems added
		change_from_to $dest rmscsi rmdisk

		# get the new 'action rmdisk ...' line
		new_action_line="`grep '^action rmdisk action_filemgr' $src`"

		# ensure that default rmdisk line is up to date
		update_action_line $dest "$new_action_line" \
		    '# there is no old action line to remove'

                # ensure that default rmdisk line is up to date
                update_ufs_ident_line $dest 
                update_pcfs_ident_line $dest
		add_udfs_ident_line $dest \
			"ident udfs ident_udfs.so cdrom floppy rmdisk"

		# ensure that safe mount line is added
		update_mount_line $dest

		# restore permissions if they changed
		chmod 0444 $dest
		chgrp bin $dest
	fi
done

exit 0
