#! /bin/sh
#
#	fmusersetup 
#
#   Copyright (c) 1986-1989 by Frame Technology Corp.
#   Steve Kirsch

#
#-------------------------------------------------------------------------------
#-----  Frame Technology	If you encounter any difficulties with this
#-----  1010 Rincon Circle	script, contact Frame Tech Support.
#Frame  San Jose, CA 95131	internet: comments@frame.com
#=====  (408) 433-3311		uucp: {riacs,ames,vsi1}!frame!comments
#-------------------------------------------------------------------------------
# This shell script completes the FrameMaker installation process.
# See more comments below (not placed here to reduce startup time).
# Modification History:
# v 1.02 (2/27/87): 
#	Added check for cp -r (which would have created
#	.makerinit as directory, rather than as a link)
#	Added more explanation re: LaserWriter Plus vs. LaserWriter.
#	Better message if .makerinit doesn't exist.
# v 1.03 (3/8/87): Added warning if .makerinit doesn't have read access
# v 1.04 (3/9/87): Loops if type in name of password file wrong. Clarified merge
#		question for passwords.
# v 1.05 (4/17/87): Adds FMHOME lines at the end of .profile
# v 1.06 (7/6/87): Added check for $FMHOME not being a valid directory
#		Added rm -rf $tmpfile in case we had run as root and aborted.
#		Fixed directory comparison to use $PWD program.
#		Fixed -w file test so we do administrative stuff on Release 2.
# v.1.07 (8/26/87): Asks for confirmation before blowing away lwplus fonts
# v.1.08 (11/10/87): Added `arch` in added .login line so your path is set
#		to the right set of binaries depending on which /bin you have
#		mounted (which /pub you mount)
# v.1.09 (2/1/88): Make it work even if upgrade from pre-1.12 Maker
#		Remove call to /bin/pwd for SunOS 4.0 automounting
# v.1.3 (6/9/88): Now knows about FrameMaker, `arch` removed from path.
#		Looks for arch-specific files in bin/bin.`arch`.
#		tmpfile uses process-id.  bin/maker should be sym link.
#		doesn't remove unnecessary files
#		adds . as well as $FMHOME/bin to $path
#		remove check for existing .makerinit directories on path
#
# make sure that we have good stuff on our path
PATH=/bin:/usr/bin:/usr/ucb

# anyone can read temporary files
umask 022

if [ "$PWD" = "" ]; then
	CURRENTDIR=`pwd`
else
	CURRENTDIR=$PWD
fi

echo -n "fmusersetup 2.0: The FrameMaker user installation program.
This script is used to reconfigure your path setting.

When you are asked a yes or no question below, type the letter 'y' or 'n' 
and then press Return. Note that the default answer (shown in brackets next 
to the question) is usually correct. To use the default answer, press 
Return in response to the question.

Would you like a description of what this script does? [y]: "
read answer
if [ "$answer" != n ]; then
	echo "

If you are a FrameMaker user, use this script to:
-	Reconfigure your path setting, allowing you to start FrameMaker
	from any directory. You need to do this when FrameMaker is first
	installed and when FrameMaker is reinstalled to a new directory.
-	Define a handy shell environment variable (FMHOME) that points to
	the directory in which FrameMaker was installed (this directory).
-	Give 'execute' permission to your home directory so that temporary
	print files sent to a network printer (a printer not attached to
	your workstation) will be removed automatically.
Note that you MUST log out completely after the installation in order for
these changes to your login to take effect!

You can run fmusersetup any time, and as often as you want.
You will be prompted for confirmation before each action is executed.

NOTICE: While running FrameMaker, please ignore console messages like:
	Window data lock broken after time limit exceeded by pid 6779
	The offending process was sent SIGXCPU

"
	echo -n "OK to continue? [y]: "
	read answer
	if [ "$answer" = n ]; then
		echo "OK, installation not done."
		exit 0
	fi
fi

# If FrameMaker is not yet installed on your path, this routine will add a 
# line to your .login or .cshrc, and/or .profile file which adds FrameMaker
# to your path and defines the shell environment variable FMHOME. A comment
# line will also be added.
# If Maker is already installed, you will be asked if you want to uninstall.

echo "Checking installation..."

# If you're wondering about the screwy indents, it's because I use maker to edit this!
# **Each** user must run this script.
# The installation routines add two lines to the END of the .cshrc (if path statement
# is already there) or start of .login and/or START of .profile.:
#         a comment line, and 
#	a line that both sets the path and sets an environment variable.

# Potential problems:
#	1. The user must log out and back in again to win.
#	2. Adding our configuration lines to the start of the .login 
#	   will NOT always work because
#	   a user could over-ride our path changes by setting an absolute path.

# NOTES:	1. You can't add config lines to the end of the .login because users
#	   often may never get there (e.g., he starts suntools automatically)
#	2. You can't add after the set path statement since parsing the end of this
#	   is too hard (it could be multi-line, or have a partial next statment on
#	   the same line.
#	3. If the .cshrc contains a path, we add to the end of it instead of .login
#	   since this will generally be an absolute path setting so recursive
#	   shells don't get large path settings, but rsh will get a path.
#	4. It was not easy to figure out the above rules. 

# This script is used by each individual user to:
#	install
#	uninstall
#	or re-install (if FrameMaker is moved to a new place)
# Things to note: 
# if the directory name we install in is too big, the user can edit it to an alias
# This script is designed to ask as few questions as possible since naive users
# are always afraid of giving the wrong answer.

# If fm is not installed, 
#  if .fminit2.0 is not in the current directory, 
#	tell him this must be run from the main FrameMaker directory.
#  print the instructional message above. Type y or n in response to yes or no
#  questions.If you hit return, you will get the default in brackets.
#  chmod a+x ~ to allow lpr -s -r for files > 1M since these are written to home
#  	directory.
#  if .login exists, adds:
#	setenv FMHOME xxx; set path=( . $FMHOME/bin $path )
#  if .profile exists, adds:
#	FMHOME=xxxx;PATH=:.:$FMHOME/bin:$PATH;export FMHOME PATH
# where xxx is taken from the name of the current directory
# Tells the user that .login modified and to
# to logout and in again. Start suntools, then type maker in a SunView window.

# Otherwise, tell him where he is installed. If the filenames match,
# ask him if he wants to uninstall [no]. If yes, grep out the line.
# If the filenames don't match, tell him so and ask if he wants to re-install. 
# If yes, uninstall, then re-install.

[ -d .fminit2.0 ] || {
	echo "Sorry. The .fminit2.0 directory is not in the current directory."
	echo "This may be the result of: 
-	Running this script from a directory other than the main 
	FrameMaker directory. You must cd to the main FrameMaker 
	directory before starting this script. 
-	Installing an improperly duplicated copy of the FrameMaker 
	files. Some files may be missing from the copy. The easiest way 
	to avoid this problem is to reinstall FrameMaker from a
	FrameMaker installation tape.
-	Accidentally deleting .fminit2.0 from this directory. If this 
	happened, you should reinstall FrameMaker from a FrameMaker
	installation tape.
-	A tape read error."
	echo "Please try installing again after fixing the problem."
	exit 1
}

[ -r .fminit2.0 ] || {
	echo "Sorry. You don't have read access to .fminit2.0."
	echo "This may be the result of accidentally changing the
	access permissions on files in the main FrameMaker directory."
	echo "Please try installing again after fixing the problem."
	exit 1
}
#########################################################
# Check for the poor guy who used cp -r to get his own copy
# of FrameMaker (or who needed to transfer from another system because
# he didn't have his own tape drive)
##########################################################

[ -h bin/maker ] || {
	echo "Error: bin/maker is not a symbolic link."
	echo "This is due to one of the following: 
-	Incorrectly transferring the FrameMaker files from another 
	system. You cannot use the 'cp -r' command to copy the 
	FrameMaker system, because this command turns symbolic links 
	into files. If you did use the 'cp -r' command to copy the 
	FrameMaker system, you need to do the following: 
	1.  Remove the FrameMaker directory you are using now. 
	2.  Go to the machine where FrameMaker is installed and use 
	    the fmcopy command to copy from the original files. 
	    For example, the command 
	    'fmcopy /usr/local/frame /usr/rhost/local/frame' 
	    will copy the contents of directory /usr/local/frame into 
	    the directory /usr/rhost/local/frame. 
-	Installing an improperly duplicated copy of the FrameMaker 
	files. Some files may be missing from the copy. The easiest way 
	to avoid this problem is to reinstall FrameMaker from a
	FrameMaker distribution tape."
	echo "Please try installing again after fixing the problem."
	exit 100
}

tmpfile=/tmp/fmusersetup.$$
# Make sure it isn't there
rm -f $tmpfile

bindir=bin


# instfcn is the install function to perform
# instfcn is either: install; none; reinstall; uninstall.


# if he hasn't got a .login or a .profile or .cshrc, give error
[ -f $HOME/.login -o -f $HOME/.profile -o -f $HOME/.cshrc ] || {
	echo "No .login, .cshrc, or .profile file found in your home directory."
	echo "Create the appropriate directory manually. Then try installing again."
	exit 1
	}
# Flag which is set if we performed an install and need to type instructions
# after all the installs are done.
installed=0

# Grep expression which matches a set path = statement
greppath="set[ 	][ 	]*path[ 	]*="


###########################################################################
# Start of the main install loop
###########################################################################
for file in .login .profile 
do
# If we are installing in .login, but there is a set path entry in cshrc instead,
# (or if .login doesn't exist), use .cshrc instead. 
	if [ $file = .login -a -f $HOME/.cshrc ]; then
		grep -s "$greppath" $HOME/.cshrc
			[ $? = 0 -o ! -f $HOME/.login ] && {
				file=.cshrc
			}
	fi

# Don't do anything unless the file exists
if [ -f $HOME/$file ]; then
# Make sure we have write access.
	chmod u+w $HOME/$file
	[ -w $HOME/$file ] || {
		 echo "Sorry, you need write access to $HOME/$file."
		 echo "Make sure that you are logged into your home machine."
		 echo "Press Return to continue."
		 read answer
		 continue
	}
# Extract out the line containing FMHOME
	grep "FMHOME" $HOME/$file >$tmpfile
	if [ $? = 0 ]; then
		grep -s "$CURRENTDIR;" $tmpfile
		if [ $? = 0 ]; then
			grep -s "/bin..arch." $tmpfile
			if [ $? = 0 ]; then
				instfcn=reinstall
			else
				instfcn=none
			fi
		else
# Directory name not found, but could be a link to the same directory, so check it
# First switch to root to allow for a null FMHOME. Save the pwd, then switch back here
		   echo -n "Checking whether \$FMHOME ($FMHOME) is the same as $CURRENTDIR..."
# WARNING: This is NOT a conclusive test to see if FrameMaker is correctly installed
# since $FMHOME may not be the same as what it is set to in the shell script.

# cd to / to protect against a null or relative FMHOME
			cd /

# $FMHOME may no longer exist if this is a re-install to a new place and the
# old directory was renamed.
			[ $FMHOME ] && [ -d $FMHOME ] && cd $FMHOME
			if [ `pwd` = $CURRENTDIR ]; then
				instfcn=none
				echo "same."
			else
				instfcn=reinstall
				echo "different."
			fi
		fi
	else
		instfcn=install
	fi
	cd $CURRENTDIR
	if [ $instfcn = none ]; then
		echo "FrameMaker is already installed in your $file file."
		echo "Do not remove the FrameMaker entry unless you want to"
		echo "uninstall FrameMaker."
		echo -n "Do you wish to remove the FrameMaker entry? [n]: "
		read answer
		if [ "$answer" = "y" ]; then
			instfcn=uninstall
		else
			echo "No changes made to your $file file."
		fi
	fi
# if we need to remove FMHOME from login (re-install or uninstall)
	if [ $instfcn = uninstall -o $instfcn = reinstall ]; then
	        echo -n "Removing all lines containing 'FMHOME' from $HOME/$file ..."
		grep -v "FMHOME" $HOME/$file >$tmpfile
		cp $tmpfile $HOME/$file
		echo "Done."
	fi

	if [ $instfcn = install -o $instfcn = reinstall ]; then
# First create a temp file that has the lines to add
# If file is .login or .cshrc, create the appropriate thing.
		if [ $file != .profile ]; then
cat <<EOF >$tmpfile
# FMHOME line added by the FrameMaker setup program
setenv FMHOME $CURRENTDIR; set path=( . \$FMHOME/$bindir \$path )
EOF
		else
cat <<EOF >$tmpfile
# FMHOME line added by the FrameMaker setup program
FMHOME=$CURRENTDIR; PATH=:.:\$FMHOME/$bindir:\$PATH; export FMHOME PATH 
EOF
		fi
	fi
	if [ $instfcn = install ]; then
	echo "FrameMaker is not already installed in your $file file."
	echo "OK to add these two lines:"
	echo "----"
	cat $tmpfile
	echo "----"
	echo -n "to $HOME/$file? [y]: ";
	read answer
	echo "------------------------------------------------------------"
	[ "$answer" = "n" ] && {
		echo "OK. $file will not be modified. Continuing..."
		continue
	}
	fi

	if [ $instfcn = install -o $instfcn = reinstall ]; then

		echo -n "OK. Adding the lines "
		if [ $file != .login ]; then
			echo "to the end of your $file."
			cat $tmpfile >>$HOME/$file
		else
			echo "to the beginning of your $file."
			cat $HOME/$file >>$tmpfile
# cp preserves permissions
			cp $tmpfile $HOME/$file
echo "
WARNING: If you set your path absolutely in your $file, i.e.,
if you have a path statement which is *NOT* of the form:
	set path = ( ... \$path ... )
it will nullify the path setting which was just added at the beginning 
of your $file. You will need to use a text editor to move the top two lines 
(the ones just added) to a position after the place where you set the path. 
Note that your path statement(s) are now:"

			if [ $file = .profile ]; then
				match="PATH="
			else
				match="$greppath"
			fi
			echo "-----"
			grep "$match" $HOME/$file
			echo "-----"
		fi
		echo
		echo "FrameMaker installation in your $file is now complete."
		echo
		echo "Press Return to continue."
		read answer
		installed=1 
	fi
fi
done
rm -f $tmpfile

if [ $installed = 1 ]; then
# Now add allow lpr -s over a network to delete files
	ls -ld $HOME >$tmpfile
	grep -s \^.........x $tmpfile
	if [ $? != 0 ]; then
		echo
		echo "Your home directory must have 'execute' permission so that 
FrameMaker's temporary print files can be removed automatically when 
they are sent to a network printer (a printer which is not physically 
attached to your workstation)."
		echo -n "OK to add 'execute' access to your home dir (chmod a+x ~)? [y]: "
		read answer
		if [ "$answer" != n ]; then
			chmod a+x $HOME
			echo "Done."
		else
			echo "OK, but you will have to delete large print files manually."
		fi
	rm -f $tmpfile
	fi
	echo "
(You may want to remove old or redundant copies of FrameMaker)
IMPORTANT:-------------------------------------------
In order for the changes in your login file to take effect,
you MUST exit Suntools and log out *completely*
(until you see the UNIX 'login:' prompt).
Then log in and start suntools.
From then on, you can start FrameMaker from any suntools
window by typing: maker
-----------------------------------------------------
Please exit suntools and log out and back in again at the prompt.
"
fi

# message for installers of FrameMaker software:

if [ ! -w bin ]; then
	echo "fmusersetup done."
	exit 0
fi

echo "------------------------"
echo "Please be sure that each person who wishes to use FrameMaker
runs this installation script (fmusersetup).

If you wish to be kept informed by e-mail of the latest Frame developments,
please send an e-mail message to sun!frame!comments and ask to be added to
the user mailing list.  If there are several Frame users at your site, we
request that you ask your system administrator to set up a mailing alias, for
example: \"sun!mycompany!frame-users\" and send us the address.

fmusersetup done."
[ $installed = 1 ] && echo "
IMPORTANT:
Don't forget to log out completely now so that
the changes to your login file will take effect."

exit 0

