#!/bin/sh
#
# Copyright (c) 1995 Sun Microsystems, Inc.  All rights reserved
#
# wabiclientinstall: 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.
#
#  @(#)wabiclientinstall	1.16	18 Jul 1995

# 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

#
# Define internationalized "echo" replacement routine
#

msgprint(){
	eval echo "\"`TEXTDOMAINDIR=$WABIHOME/lib/locale TEXTDOMAIN=wabiclientinstall gettext \"$1\"`\""
}

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
	msgprint 'Warning: $WABIHOME/bin/wabiclientinstall has to be run as root.
wabiclientinstall failed.' 1>&2
	exit 0
fi

RET="`change_volmgt`"
if [ "$RET" =  "TRUE" ]
then
	no_resp=`msgprint 'no'`
	yes_resp=`msgprint 'yes'`
	quit_resp=`msgprint 'quit'`

	msgprint '
To use your diskette drive under Wabi, we recommend that you
disable control of the diskette drive by Volume Manager.

Would you like this done now?' 1>&2
	msgprint 'change /etc/vold.conf ? [$yes_resp|$no_resp|$quit_resp]\\c' 1>&2
	read ANS
	case $ANS in 
	    $quit_resp )
		exit 3;;
	    $yes_resp )
		$WABIHOME/lib/vold.switch -disable
		break;;
	    *)
# TRANSLATION-NOTE
#	do not translate "-disable"
		msgprint '
If you need to use your diskette drive with Wabi at a later time,
you can always disable control of the drive by Volume Manager.
To do this, enter the following command as superuser:
$WABIHOME/lib/vold.switch -disable

See the Wabi 2.1 Release Notes for more detailed information.' 1>&2

		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
	msgprint 'Installing file /usr/lib/rmmount/action_wabi.so.1.' 1>&2
    	cp $WABIHOME/drvr/libaction_wabi.so.1	/usr/lib/rmmount/action_wabi.so.1
    else
	msgprint 'Warning: Can not read file $WABIHOME/drvr/libaction_wabi.so.1.
wabiclientinstall failed.' 1>&2
	exit 0
    fi
fi



#
# Load the Wabi file locking/sharing driver.
#
msgprint 'Installing file sharing/locking driver.' 1>&2
if [ -x $WABIHOME/drvr/wabiload ]
then
    $WABIHOME/drvr/wabiload
else
    msgprint 'Warning: Can not run file $WABIHOME/drvr/wabiload.
wabiclientinstall failed.' 1>&2
    exit 0
fi
				
#
# Setup the classing engine to integrate Wabi with File Manager.
#
msgprint '
Installing Deskset File Manager icons.' 1>&2
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
    msgprint 'Warning: Can not read files $WABIHOME/icons/*xview.
Installing Deskset File Manager icons failed.' 1>&2
    exit 0
fi


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

if [ ! -d "$OPENWINHOME" ]; then
# TRANSLATION-NOTE
#	The following 2 message are displayed sequentially.
    msgprint '
WARNING: Unable to install DeskSet File Manager types.
OpenWindows was not found in /usr/openwin,
and \\$OPENWINHOME was not set.

To use Wabi icons from the DeskSet File Manager, you must
run the following as super-user:

  ce_db_build system -from_ascii $WABIHOME/icons/classing-engine.txt' 1>&2
    msgprint '
The ce_db_build program is located in your OpenWindows bin directory.
See your System Administrator if you do not know how to locate this.

You must also add $WABIHOME/bin to your PATH and login again.' 1>&2
    exit 0;
else
    msgprint 'Installing File Manager types.' 1>&2
    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
    msgprint '
NOTICE: You must add $WABIHOME/bin to your PATH and
login again in order to use these icons.' 1>&2
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
    	msgprint '
Updating Volume Management for Wabi.' 1>&2
    	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
   	msgprint '
The Volume Manager patch should be installed on Solaris 2.2, otherwise
Wabi may not be able to use the diskette drive.
' 1>&2
    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
		msgprint '
If you would like to use the UNIX serial ports in Wabi,
please run the following command as root:

	chmod 666 /dev/cua/*

This gives ordinary users read/write permissions to the
serial devices, allowing them to use these ports from Wabi.' 1>&2
	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
	msgprint '
This Intel Solaris platform does not appear to have COM2: configured.
If you have a second serial port and would like to use it in Wabi,
please follow the directions in /kernel/drv/asy.conf to configure
this second port.' 1>&2
fi





