#!/bin/sh
#
# postinstall:	Integrate Wabi with the classing engine, install the file
# locking/sharing driver, and update the Volume Manager if required.
# Also check for the existence/permissions of the UNIX serial devices,
# advising the user if action is required.
#   
#	@(#)postinstall	2.11	25 Oct 1994
#
# SUBROUTINES 

validate_filename()
{
	# $1 is desired filename or symlink
	# returns real filename on stdout

	if [ $# -ne 1 ]; then
		echo "";
		return 1;
	fi;

	if [ "$1" = "" ]; then
		# echo no filename provided
		return 1
	fi;

	if [ ! -f $1 -a ! -d $1 -a ! -c $1 -a ! -b $1 -a ! -p $1 ]; then
		# echo $1 does not exist
		return 1
	fi;

	real_filename=$1
	while [ -h $real_filename ];  do
		listing="`/bin/ls -ld $real_filename`"
		set -- $listing
		first_part=`expr $real_filename : '\(.*\)/`
		shift 10 # make position be less than 10
		last_part=$1
		if [ `expr "$last_part" : '\(.\)'` = "/" ]; then
			real_filename=$last_part;
		else
			real_filename=${first_part}/${last_part};
		fi;
	done

	echo $real_filename
	return 0
}

get_file_perms()
{
	# $1 is name of file to check

	if [ $# -ne 1 ]; then
		echo "";
		return 1;
	fi;

	listing="`/bin/ls -ld $1`"
	if [ $? -ne 0 ]; then
		echo "";
		return 1;
	fi;
	set -- $listing

	echo $1
	return 0
}

has_permission()
{
	# $1 is desired access ('-' means don't care)
	# $2 is permissions string as displayed by ls -l
	# returns 0 (true in shell scripts) if access okay,
	#  1 (false in shell scripts) if no access

	# this only understands simple permissions
	# it treats a byte position simply as either '-' or !'-'
	# as a result it's not currently useful for testing
	#  permissions bits that don't have their own position
	#  (setuid, setgid, sticky, etc.)
	# not only does it currently not understand these, it doesn't
	#  even realize that it doesn't understand

	if [ $# -ne 2 ]; then
		return 1;
	fi;

	if [ "$1" = "" ]; then
		return 0;
	fi;

	desired="$1";
	actual="$2";
	while [ -n "$desired" ]; do
		desired_one=`expr "X$desired" : 'X\(.\)'`;
		actual_one=`expr "X$actual" : 'X\(.\)'`;

		if [ "$desired_one" != "-" -a "$actual_one" = "-" ]; then
			return 1;
		fi;

		# get ready for next iteration of loop
		desired=`expr "X$desired" : 'X.\(.*\)'`
		actual=`expr "X$actual" : 'X.\(.*\)'`
	done;

	return 0;
}

#
# MAIN
#

#
# Define system BASEDIR
#
SYSBASEDIR=`echo ${BASEDIR} | awk -F// '{ print $2 }'`
if [ "$SYSBASEDIR" = "a" ]
then
     SYSBASEDIR="/a"
else
     SYSBASEDIR=""
fi

#
# Copy the action_wabi.so.1 file.
#
if [ ! -f $SYSBASEDIR/usr/lib/rmmount/action_wabi.so.1 ]
then
    if [ -d $SYSBASEDIR/usr/lib/rmmount ]
    then
    	echo "Installing file $SYSBASEDIR/usr/lib/rmmount/action_wabi.so.1."
    	cp $BASEDIR/$PKG/drvr/libaction_wabi.so.1	$SYSBASEDIR/usr/lib/rmmount/action_wabi.so.1
    fi
fi

$INST_DATADIR/$PKG/install/cleanup $BASEDIR/$PKG/bin/wabiprog /tmp/wabiprog
if [ $? -ne 0 ]
then
         echo "Install of $PKG failed"
         rm -f $BASEDIR/$PKG/bin/wabiprog
         exit 1
fi
rm -f $BASEDIR/$PKG/bin/wabiprog
mv /tmp/wabiprog $BASEDIR/$PKG/bin/wabiprog

#
# Load the Wabi file locking/sharing driver.
#
echo "Installing file sharing/locking driver."
$BASEDIR/$PKG/drvr/wabiload

#
# Setup the classing engine to integrate Wabi with File Manager.
#
echo
echo "Installing Deskset File Manager icons."
if [ ! -d $SYSBASEDIR/etc/cetables ]
then
    mkdir -p $SYSBASEDIR/etc/cetables
fi

if [ ! -d $SYSBASEDIR/etc/cetables/icons ]
then
    mkdir -p $SYSBASEDIR/etc/cetables/icons
fi

cp $BASEDIR/$PKG/icons/appman.xview		$SYSBASEDIR/etc/cetables/icons
cp $BASEDIR/$PKG/icons/appman.mask.xview	$SYSBASEDIR/etc/cetables/icons
cp $BASEDIR/$PKG/icons/config.xview		$SYSBASEDIR/etc/cetables/icons
cp $BASEDIR/$PKG/icons/config.mask.xview	$SYSBASEDIR/etc/cetables/icons
cp $BASEDIR/$PKG/icons/default.xview		$SYSBASEDIR/etc/cetables/icons
cp $BASEDIR/$PKG/icons/default.mask.xview	$SYSBASEDIR/etc/cetables/icons
cp $BASEDIR/$PKG/icons/dos.xview		$SYSBASEDIR/etc/cetables/icons
cp $BASEDIR/$PKG/icons/dos.mask.xview		$SYSBASEDIR/etc/cetables/icons
cp $BASEDIR/$PKG/icons/filev.xview		$SYSBASEDIR/etc/cetables/icons
cp $BASEDIR/$PKG/icons/filev.mask.xview 	$SYSBASEDIR/etc/cetables/icons
cp $BASEDIR/$PKG/icons/wininst.xview		$SYSBASEDIR/etc/cetables/icons
cp $BASEDIR/$PKG/icons/wininst.mask.xview	$SYSBASEDIR/etc/cetables/icons

if [ -z "$OPENWINHOME" ]; then
    OPENWINHOME="/usr/openwin"
fi

if [ ! -d "$OPENWINHOME" ]; then
    echo
    echo "WARNING: Unable to install DeskSet File Manager types. "
    echo "OpenWindows was not found in /usr/openwin, "
    echo "and \$OPENWINHOME was not set."
    echo
    echo "To use Wabi icons from the DeskSet File Manager, you must "
    echo "run the following as super-user:"
    echo
    echo "  ce_db_build system -from_ascii $BASEDIR/$PKG/icons/classing-engine.txt"
    echo
    echo "The ce_db_build program is located in your OpenWindows bin directory."
    echo "See your System Administrator if you do not know how to locate this."
    echo
    echo "You must also add $BASEDIR/$PKG/bin to your PATH and login again"
    exit 0;
else
    echo "Installing File Manager types."
    if [ ! -f /etc/cetables/cetables ]
    then
	$OPENWINHOME/bin/ce_db_build system -from_ascii $BASEDIR/$PKG/icons/classing-engine.txt > /dev/null 2>&1
    else
	# Remove old icons from the wrong place, if they are there
	rm -f /etc/cetables/appman.xview
	rm -f /etc/cetables/appman.mask.xview

	oldsysdb=/tmp/old-ce-db.$$
	newsysdb=/tmp/new-ce-db.$$

	# Get their current database
	$OPENWINHOME/bin/ce_db_build system -to_ascii $oldsysdb

	# Replace it with ours
	$OPENWINHOME/bin/ce_db_build system -from_ascii $BASEDIR/$PKG/icons/classing-engine.txt > /dev/null 2>&1

	# Remove entries from old versions of Wabi
	awk -f $BASEDIR/$PKG/icons/delete-old-ce.awk $oldsysdb > $newsysdb 2>&1

	# Merge the remainder of their old database AFTER ours
	$OPENWINHOME/bin/ce_db_merge system -from_ascii $newsysdb > /dev/null 2>&1

	rm -f $oldsysdb $newsysdb
    fi
    echo
    echo "NOTICE: You must add $BASEDIR/$PKG/bin to your PATH and"
    echo "login again in order to use these icons."
fi

# On systems with Volume Manager update rmmount.conf to let Wabi
# work with it.

if [ `/usr/bin/uname -r` != "5.1" ]
then
    if [ -f "$SYSBASEDIR/etc/rmmount.conf" ]
    then
	ACTIONWABI="`grep action_wabi.so $SYSBASEDIR/etc/rmmount.conf`"
	if [ -z "$ACTIONWABI" ]
    	then
    	    echo " "
    	    echo "Updating Volume Management for Wabi."
    	    sed  '/Actions/ a\
action -premount floppy action_wabi.so' $SYSBASEDIR/etc/rmmount.conf > $SYSBASEDIR/etc/rmmount.conf.tmp
    	    mv $SYSBASEDIR/etc/rmmount.conf $SYSBASEDIR/etc/rmmount.conf-
    	    mv $SYSBASEDIR/etc/rmmount.conf.tmp $SYSBASEDIR/etc/rmmount.conf
	fi
    else
    	echo "Can not find file: $SYSBASEDIR/etc/rmmount.conf"
    fi
fi


# Until libvolmgt.so.1 becomes permanent in Solaris 2.3 we have to issue
# a warning to install the Volume Manager patch on Solaris 2.2 - if it's
# not already installed.

if [ `/usr/bin/uname -r` = "5.2" ]
then
    if [ ! -f /usr/lib/libvolmgt.so.1 ]
    then
   	echo " "
	echo "The Volume Manager patch should be installed on Solaris 2.2, otherwise"
	echo "Wabi may not be able to use the diskette drive."
	echo " "
    fi
fi

# Check to see if the UNIX serial devices are writable by mortal users.
# Unfortunately, a simple if [ -w ] test won't work, since the devices 
# are almost certainly going to be writeable by root, the uid running 
# this script.  Have to go to some lengths to check the "other" fields 
# of the permissions of the phyisical /devices files.

device="/dev/cua/a"
filename=`validate_filename $device`
actual_perms=`get_file_perms $filename`

for desired_perms in "-------rw-" ; do
	if has_permission "$desired_perms" "$actual_perms"
	then
		echo " "
	else
		echo " "
		echo "If you would like to use the UNIX serial ports in Wabi, "
		echo "please run the following command as root:"
		echo " "
		echo "		chmod 666 /dev/cua/*"
		echo " "
		echo "This gives ordinary users read/write permissions to the"
		echo "serial devices, allowing them to use these ports from Wabi."
	fi
done

# Intel Solaris by default does not enable COM2:.  If the user has the mouse 
# hooked up to COM1:, it will appear that there are no avaliable serial ports
# in Wabi unless COM2: is configured by following the directions in 
# /kernel/drv/asy.conf

if [ `/usr/bin/uname -p` = "i386"  -a `/usr/bin/uname` = "SunOS" -a ! -h /dev/cua/b ]
then
	echo " "
	echo "This Intel Solaris platform does not appear to have COM2: configured."
	echo "If you have a second serial port and would like to use it in Wabi,"
	echo "please follow the directions in /kernel/drv/asy.conf to configure"
	echo "this second port."
fi

if [ "$FIX_FLOPPY" = "TRUE" ]
then
	export SYSBASEDIR
	/sbin/sh $SYSBASEDIR/opt/SUNWwabi/bin/vold.switch -disable
fi



