#!/bin/sh
#
# clientinstall: If user mounts /opt/SUNWwabi from the server which has
# Wabi Package installed onto the desktop machine that do not has Wabi
# Package installed, integrate Wabi with the classing engine, install the 
# file locking/sharing driver, and update the Volume Manager if required.
#
#  @(#)clientinstall	1.12	26 Oct 1994

# SUBROUTINES 

FILE_CHECK=/etc/.vold.wabi


NAWK=/usr/bin/nawk
GREP=/usr/bin/grep
CAT=/bin/cat
ECHO=/bin/echo
UNAME=/usr/bin/uname
PS=/usr/bin/ps
CKYORN=/usr/bin/ckyorn
SH=/usr/bin/sh

change_volmgt()
{
	#
	# change_volmgt() -
	#	check to see if /etc/vold.conf should change.
	#	return FALSE if there is no reason to change (e.g., no volmgt 
	#	    running)
	#	return TRUE if we should ask user to change
	#

	#
	# if this is already fixed, don't even continue
	#
		
	if [ -f $FILE_CHECK ]
	then
		$ECHO FALSE
		return
	fi	

	#
	# only change 5.4 systems (should we do this?)
	#

	if [ "`$UNAME -r`" != "5.4" ]
	then
		$ECHO FALSE
		return
	fi

	#
	# only do this is they have a floppy
	#

	if [ ! -c /dev/rdiskette ]
	then
		$ECHO FALSE
		return
	fi

	#
	# are they even running vold?
	#

	PID=`$PS -ef | $GREP /usr/sbin/vold | $GREP -v $GREP | $NAWK '{ print $2 }'`

	if [ -z "$PID" ]
	then
		$ECHO FALSE
		return
	fi

	#
	# look to see if line exists in /etc/vold.conf
	#

	$CAT /etc/vold.conf | $NAWK '
	    BEGIN { FOUND = 0 } 
	    {
	    	if(($1 == "use") && ($2 == "floppy") && ($3 == "drive")) 
		    FOUND = 1
	    }
	    END { 
		exit (FOUND == 1)
	    } 
	'
	RET="$?"

	if [ "$RET" = "1" ]
	then
		$ECHO TRUE	# if we have found the line to change
	else
		$ECHO FALSE
	fi
}

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
#

# Record the starting point so we can return after determining WABIHOME.
INITIAL_DIR=`pwd`

# Find out where this script lives to determine WABIHOME.
FROM_PATH=`echo "$0" | grep "/"`
 
if [ -n "$FROM_PATH" ]
then
	#in this case $0 is a directory component.
	FIRSTARG=`echo ${FROM_PATH} | awk -F/ '{ print $1 }'`
 
	if [ -z "$FIRSTARG" ]
	then
		SOURCE=`echo ${0} | awk -F/ '{ for (i = 1; i < NF; i++)  { printf "%s", $i; printf "/" } }'`
	else
		SOURCE=`echo ${0} | awk -F/ '{ if ($1 == "") printf "/"; for (i = 1; i < NF; i++)  { printf "%s", $i; printf "/" } }'`
	fi
else
	#in this case $0 is not a directory component.
	SOURCE="./"
fi

cd "$SOURCE"
cd ..
WABIHOME=`pwd`
export WABIHOME

#
# Check if user is in the root.
#
ID=`id`
UID=`echo ${ID} | awk '{ print $1 }'`
if [ $UID != "uid=0(root)" ]
then
	echo "Warning: $WABIHOME/bin/clientinstall has to be ran as root."
	echo "clientinstall failed."
	exit 0
fi

RET="`change_volmgt`"
if [ "$RET" =  "TRUE" ]
then
	$ECHO
	$ECHO
	$ECHO "To use your diskette drive under Wabi, we recommend that you"
	$ECHO "disable Volume Manager's control of the diskette drive."
	$ECHO
	$ECHO "Would you like this done now?\c"
	ANS=`$CKYORN -d y -d "change /etc/vold.conf"`||exit 3
	case $ANS in 
	    q* | Q* )
		exit 3;;
	    y* | Y* )
		$WABIHOME/bin/vold.switch -disable
		break;;
	    *)
		$ECHO
		$ECHO "If you need to use your diskette drive with Wabi, you can always"
		$ECHO "disable Volume Manager's control of the drive later. To do this,"
		$ECHO "enter the following command as superuser:"
		$ECHO "	$WABIHOME/bin/vold.switch -disable"
		$ECHO
		$ECHO "See the Wabi 2.0 Release Notes for more detailed information."
		$ECHO
		break;;
 	esac
	$ECHO
	$ECHO
fi


#
# Copy action_wabi.so.1 to user desktop machine.
#
if [ ! -f /usr/lib/rmmount/action_wabi.so.1 ]
then
    if [ -r $WABIHOME/drvr/libaction_wabi.so.1 ]
    then
	echo "Installing file /usr/lib/rmmount/action_wabi.so.1."
    	cp $WABIHOME/drvr/libaction_wabi.so.1	/usr/lib/rmmount/action_wabi.so.1
    else
	echo "Warning: Can not read file $WABIHOME/drvr/libaction_wabi.so.1."
	echo "clientinstall failed."
	exit 0
    fi
fi



#
# Load the Wabi file locking/sharing driver.
#
echo "Installing file sharing/locking driver."
if [ -x $WABIHOME/drvr/wabiload ]
then
    $WABIHOME/drvr/wabiload
else
    echo "Warning: Can not run file $WABIHOME/drvr/wabiload."
    echo "clientinstall failed"
    exit 0
fi
				
#
# Setup the classing engine to integrate Wabi with File Manager.
#
echo
echo "Installing Deskset File Manager icons."
if [ ! -d /etc/cetables ]
then
    mkdir -p /etc/cetables
fi

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


#
# Copy icon files
#
if [ -r $WABIHOME/icons/*xview ]
then
    cp $WABIHOME/icons/appman.xview 		/etc/cetables/icons
    cp $WABIHOME/icons/appman.mask.xview 	/etc/cetables/icons
    cp $WABIHOME/icons/config.xview 		/etc/cetables/icons
    cp $WABIHOME/icons/config.mask.xview 	/etc/cetables/icons
    cp $WABIHOME/icons/default.xview 		/etc/cetables/icons
    cp $WABIHOME/icons/default.mask.xview 	/etc/cetables/icons
    cp $WABIHOME/icons/dos.xview 		/etc/cetables/icons
    cp $WABIHOME/icons/dos.mask.xview 		/etc/cetables/icons
    cp $WABIHOME/icons/filev.xview		/etc/cetables/icons
    cp $WABIHOME/icons/filev.mask.xview		/etc/cetables/icons
    cp $WABIHOME/icons/wininst.xview 		/etc/cetables/icons
    cp $WABIHOME/icons/wininst.mask.xview 	/etc/cetables/icons
else
    echo "Warning: Can not read files $WABIHOME/icons/*xview."
    echo "Installing Deskset File Manager icons failed."
    exit 0
fi


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 $WABIHOME/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 $WABIHOME/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 $WABIHOME/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 $WABIHOME/icons/classing-engine.txt > /dev/null 2>&1

	# Remove entries from old versions of Wabi
	awk -f $WABIHOME/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 $WABIHOME/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
    ACTIONWABI="`grep action_wabi.so /etc/rmmount.conf`"
    if [ -z "$ACTIONWABI" ]
    then
    	echo " "
    	echo "Updating Volume Management for Wabi."
    	sed  '/Actions/ a\
action -premount floppy action_wabi.so' /etc/rmmount.conf > /etc/rmmount.conf.tmp
    	mv /etc/rmmount.conf /etc/rmmount.conf-
    	mv /etc/rmmount.conf.tmp /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





