#! /bin/sh
#
#	fmusersetup 
#
#   Copyright (c) 1986-1993 by Frame Technology Corp.
#
#-------------------------------------------------------------------------------
# Frame Technology	If you encounter any difficulties with this
# 1010 Rincon Circle	script, contact Frame Tech Support.
# San Jose, CA 95131	internet: comments@frame.com
# (408) 433-3311	
#-------------------------------------------------------------------------------
#
# make sure that we have good stuff on our path
PATH=/bin:/usr/bin:/usr/ucb
arg0=$0

# anyone can read temporary files
umask 022

CURRENTDIR=`pwd`
cc="\c"; nn=""
test "`echo -n X`" = X && {
   cc=""
   nn="-n"
}

echo $nn "$arg0: 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]:$cc "
read answer
[ "$answer" != n ] && {
	echo "

If you are a Frame 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 $arg0 any time, and as often as you want.
You will be prompted for confirmation before each action is executed.

"
	echo $nn "OK to continue? [y]:$cc "
	read answer
	[ "$answer" = n ] && {
		echo "OK, installation not done."
		exit 0
	}
}

# 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 X
#		windows 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 fminit 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 X windows, then type maker in a shell 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 fminit ] || {
	echo "Sorry. The fminit directory is not in the current directory."
	echo "This may be the result of: 
-	Running this script from a directory other than the main 
	Frame directory. You must cd to the main Frame
	directory before starting this script. 
-	Installing an improperly duplicated copy of the Frame
	files. Some files may be missing from the copy. The easiest way 
	to avoid this problem is to reinstall Frame from a Frame 
	installation tape.
-	Accidentally deleting fminit from this directory. If this 
	happened, you should reinstall Frame from a Frame
	installation tape.
-	A tape read error."
	echo "Please try installing again after fixing the problem."
	exit 1
}

[ -r fminit ] || {
	echo "Sorry. You don't have read access to fminit."
	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
}

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, .cshrc, or a .profile 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 file 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. 
   [ $file = .login -a -f $HOME/.cshrc ] && {
      grep "$greppath" $HOME/.cshrc > /dev/null 2>&1
         [ $? = 0 -o ! -f $HOME/.login ] && {
            file=.cshrc
         }
   }

   saved=0

   # Don't do anything unless the file exists
   [ -f $HOME/$file ] && {
      # 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
      instfcn=install
      grep "FMHOME" $HOME/$file >$tmpfile && {
	 grep "$CURRENTDIR;" $tmpfile > /dev/null 2>&1
	 if [ $? -eq 0 ]; then
	    instfcn=none
	    grep "/bin..arch." $tmpfile > /dev/null 2>&1 && instfcn=reinstall
	 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 "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
	       echo "different."
	       echo "Frame is already installed in your $file file."
	       echo "Do not re-install the Frame entry unless you want to"
	       echo "install Frame in the new location."
	       echo $nn "Do you wish to re-install the FrameMaker entry? [y]:$cc "
	       read answer
	       if [ "$answer" = "n" ]; then
	         echo ""
	         echo "Do not remove the FrameMaker entry unless you want to"
	         echo "uninstall FrameMaker."
	         echo $nn "Do you wish to remove the FrameMaker entry? [n]:$cc "
	         read answer
             if [ "$answer" = "y" ]; then
	             instfcn=uninstall
             else
		         echo "No changes made to your $file file."
                 instfcn=reallynone
             fi
	       else
		  instfcn=reinstall
	       fi
	    fi
	 fi
      }
      cd $CURRENTDIR
      [ $instfcn = none ] && {
	 echo "FrameMaker is already installed in your $file file."
	 echo "Do not remove the FrameMaker entry unless you want to"
	 echo "uninstall FrameMaker."
	 echo $nn "Do you wish to remove the FrameMaker entry? [n]:$cc "
	 read answer
	 if [ "$answer" = "y" ]; then
	    instfcn=uninstall
	 else
	    echo "No changes made to your $file file."
	 fi
      }
      # if we need to remove FMHOME from login (re-install or uninstall)
      [ $instfcn = uninstall -o $instfcn = reinstall ] && {
	 echo "Removing all lines containing 'FMHOME' from $HOME/$file."
	 grep -v "FMHOME" $HOME/$file >$tmpfile
	 [ $saved = 0 ] && {
	    mv $HOME/$file $HOME/${file}.fmsave
	    echo "Original version of $file saved in $HOME/${file}.fmsave."
	    saved=1
	 }
	 cp $tmpfile $HOME/$file
	 echo "Done."
      }

      [ $instfcn = install -o $instfcn = reinstall ] && {
	 # 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 user's setup program
setenv FMHOME $CURRENTDIR; set path=( \$FMHOME/$bindir \$path )
EOF
	 else
	    cat <<EOF >$tmpfile
# FMHOME line added by the FrameMaker user's setup program
FMHOME=$CURRENTDIR; PATH=:\$FMHOME/$bindir:\$PATH; export FMHOME PATH 
EOF
	 fi
      }

      [ $instfcn = install ] && {
	 echo "FrameMaker is not already installed in your $file file."
	 echo "OK to add these two lines:"
	 echo "----"
	 cat $tmpfile
	 echo "----"
	 echo $nn "to $HOME/$file? [y]:$cc ";
	 read answer
	 echo "------------------------------------------------------------"
	 [ "$answer" = "n" ] && {
	    echo "OK. $file will not be modified. Continuing..."
	    continue
	 }
      }

      [ $instfcn = install -o $instfcn = reinstall ] && {
	 if [ $file != .login ]; then
	    echo "OK. Adding the lines to the end of your $file file"
	    [ $saved = 0 ] && {
	       cp $HOME/$file $HOME/${file}.fmsave
	       echo "Original version of $file saved in $HOME/${file}.fmsave."
	       saved=1
	    }
	    cat $tmpfile >>$HOME/$file
	 else
	    echo "OK. Adding the lines to the beginning of your $file file."
	    cat $HOME/$file >>$tmpfile
	    # cp preserves permissions
	    [ $saved = 0 ] && {
	       mv $HOME/$file $HOME/${file}.fmsave
	       echo "Original version of $file saved in $HOME/${file}.fmsave."
	       saved=1
	    }
	    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:"

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

[ $installed = 1 ] && {
   # Now add allow lpr -s over a network to delete files
   ls -ld $HOME >$tmpfile
   grep \^.........x $tmpfile > /dev/null 2>&1
   if [ $? != 0 ]; then
      echo
      echo "Your home directory must have 'execute' permission so that 
Frame'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 $nn "OK to add 'execute' access to your home dir (chmod a+x ~)? [y]:$cc "
      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
X windows and log out *completely* (until you see the UNIX 'login:' prompt).
Then log in and start X windows.  From then on, you can start FrameMaker
from any X windows window by typing: maker
-----------------------------------------------------
Please exit X windows and log out and back in again."
}

# message for installers of FrameMaker software:

[ ! -w bin ] && {
   echo "$arg0 done."
   exit 0
}

echo "------------------------"
echo "Please be sure that each person who wishes to use FrameMaker
runs this installation script ($arg0).
  If any files were changed by this script there will a corresponding .fmsave
  file produced that contains the original version of the file.  To be sure
  that this script changed things appropiately, you should diff the files.
"

[ $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

# Local Variables:
# tab-width: 3
# End:
