#
# usage:        perm.sh
#
# abstract:     This Bourne Shell script checks permissions on files
#		and sets the umask under user control.
#
# note(s):      1. This routine must be called using a . (period)
#
# Copyright (c) 1994-95 by The MathWorks, Inc.
# $Revision: 1.9 $  $Date: 1995/04/17 13:34:27 $
#----------------------------------------------------------------------------
#
# The type of install determines what we check.
#
# Type 1,2,3 check: (matlab should be there)
#
#	./.scripts/matlab.bin		  - world readable
#	./install/arch.sh                 - world executable
#       ./toolbox/matlab/general/diary.m  - world readable
#
# Type 4 check: (license manager only)
#
#       ./etc/README			  - world readable
#	./etc/lmdebug                     - world executable
#
# NOTE: If the file doesn't exit it is simply skipped. This is not
#       an error.
#
    case "$whichi" in
	1|2|3)
    	    File="$MATLAB/.scripts/matlab.bin"
    	    . $dir/$actualp_sh
    	    permfile1="r"
    	    if [ "$Filepath" != "" ]; then
        	permfile1=`ls -l $Filepath | awk '{ print substr($1,length($1) - 2, 1) }'`
    	        if [ "$permfile1" != "r" ]; then
		    file1='./.scripts/matlab.bin'
        	    allperm1=`ls -l $Filepath | awk '{ print $1 }'`
		fi
    	    fi
#
    	    File="$MATLAB/install/arch.sh"
    	    . $dir/$actualp_sh
    	    permfile2="x"
    	    if [ "$Filepath" != "" ]; then
        	permfile2=`ls -l $Filepath | awk '{ print substr($1,length($1)) }'`
    	        if [ "$permfile2" != "x" ]; then
		    file2='./install/arch.sh'
        	    allperm2=`ls -l $Filepath | awk '{ print $1 }'`
		fi
    	    fi
#
	    File="$MATLAB/toolbox/matlab/general/diary.m"
	    . $dir/$actualp_sh
    	    permfile3="r"
    	    if [ "$Filepath" != "" ]; then
        	permfile3=`ls -l $Filepath | awk '{ print substr($1,length($1) - 2, 1) }'`
    	        if [ "$permfile3" != "r" ]; then
	    	    file3='./toolbox/matlab/general/diary.m'
        	    allperm3=`ls -l $Filepath | awk '{ print $1 }'`
		fi
    	    fi
	    ;;
	4)
    	    File="$MATLAB/etc/README"
    	    . $dir/$actualp_sh
    	    permfile1="r"
    	    if [ "$Filepath" != "" ]; then
        	permfile1=`ls -l $Filepath | awk '{ print substr($1,length($1) - 2, 1) }'`
    	        if [ "$permfile1" != "r" ]; then
    	            file1='./etc/README'
        	    allperm1=`ls -l $Filepath | awk '{ print $1 }'`
		fi
    	    fi
#
    	    File="$MATLAB/etc/lmdebug"
    	    . $dir/$actualp_sh
    	    permfile2="x"
    	    if [ "$Filepath" != "" ]; then
        	permfile2=`ls -l $Filepath | awk '{ print substr($1,length($1)) }'`
    	        if [ "$permfile2" != "x" ]; then
    	    	    file2='./etc/lmdebug'
        	    allperm2=`ls -l $Filepath | awk '{ print $1 }'`
		fi
    	    fi
#
    	    permfile3="r"
	    ;;
    esac
#
    if [ "$permfile1" != "r" -o "$permfile2" != "x"  -o "$permfile3" != "r" ]; then
        . $dir/$clearsc_sh
#++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
echo '               Warning: Permission Problem With MATLAB Files!'
echo '   ------------------------------------------------------------------------'
echo '   | Checked some files in your MATLAB root directory. These are bad:     |'
echo ' '
#++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
	if [ "$permfile1" != "r" ]; then
#++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
    echo "     $allperm1 (not world readable)  - $file1"
#++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
	fi
	if [ "$permfile2" != "x" ]; then
#++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
    echo "      $allperm2 (not world executable) - $file2"
#++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
	fi
	if [ "$permfile3" != "r" ]; then
#++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
    echo "      $allperm3 (not world readable)   - $file3"
#++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
	fi
echo ' '
echo '   | If you continue with this installation parts of MATLAB may be        |'
echo '   | inaccessible to the general user after installation. If this is      |'
echo '   | acceptable for security reasons then continue. Otherwise, you        |'
echo '   | SHOULD STOP and start all over and extract the files again being     |'
echo '   | sure that you CORRECTLY set your user file-creation mask with the    |'
echo "   | 'umask' command BEFORE you extract the files. The argument to the    |"
echo "   | 'umask' command SHOULD end in 2. For example,                        |"
echo '   |                                                                      |'
echo '   |                 # umask 2                                            |'
echo '   |                                                                      |'
echo '   | will preserve the read and execute permissions for world. You        |'
echo '   | should extract the files again using the same userid (usually root)  |'
echo '   | as was orginally done so that deleting the existing files is         |'
echo '   | optional.                                                            |'
echo '   ------------------------------------------------------------------------'
#++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
        if [ "$batch" = "0" ]; then
            mesg="Continue? (y/[n]) "
            . $dir/$echon_sh
            read ans
            if [ `expr "//$ans" : '//[Yy].*'` -ne 0 ]; then
		:
	    else
		. $dir/$abort_sh
	    fi
	else
            echo 'Continue? (y/[n]) y'
	fi
    fi

#	
# Check umask - must end in a 2.
#
#++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
    worldmask=`(umask) 2>&1 | awk  'NF != 0 { print substr($1, length($1)) }'`
    if [ "$worldmask" != "2" ]; then
        . $dir/$clearsc_sh
	maskstr=`(umask) 2>&1 | awk '{ printf "%4s", $1 }'`
#++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
    echo '            Warning: User File-creation Mask Problem!'
    echo ''
echo '   ------------------------------------------------------------------------'
echo "   | Your user file-creation mode mask (umask) = $maskstr does not end        |"
echo "   | in '2'! If you do not change its value NOW to end in '2' parts of    |"
echo '   | MATLAB may be inaccessible to the general user after installation.   |'
echo '   | If this is acceptable for security reasons then enter the current    |'
echo '   | value again. Otherwise, enter a value (usually up to 4 octal digits) |'
echo "   | which ends in '2' or press <return> for the default.                 |"
echo '   ------------------------------------------------------------------------'
echo ''
#++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
        if [ "$batch" = "0" ]; then
	    mesg="User file-creation mask string [2] "
            . $dir/$echon_sh
            read maskstr
            if [ "$maskstr" ]; then
	        umask $maskstr
            else
                umask 2
	    fi
	else
	    mesg="User file-creation mask string [2] "
            echo "$mesg" `umask` 
	fi
    fi
