#!/bin/ksh

#--------------------------------------------------------------------------
# Licensed Materials - Property of IBM
# 5724-C04
#
# (C) Copyright IBM Corporation 2006. All Rights Reserved.
# US Government Users Restricted Rights - Use, duplication
# or disclosure restricted by GSA ADP Schedule Contract
# with IBM Corp.
#--------------------------------------------------------------------------

permission=$1
fileFull=$2

permissionNoX=$(print $permission | sed 's/\([^,]*\)\(+[r]*[w]*\)x/\1\2,\1-x/g')
fileParentPath="$(dirname "$fileFull"|sed 's/\// /g')"


if [[ -L "$fileFull" ]]
then
   # Do not change permissions on symbolic links
   :
else
   # Change permissions for any non-links
	if [[ -d "$fileFull" ]]
	then
	   chmod $permission "$fileFull"
	fi
	if [[ -f "$fileFull" ]]
	then
	   useNoX=""
	   for pathPart in $fileParentPath
	   do
	      if [[ "$pathPart" = @(bin|classes|lib|sh) ]]
	      then
	         useNoX=yes
	      fi
	   done
	   if [[ "$useNoX" = yes ]]
	   then
	      chmod $permission "$fileFull"
	   else
	      chmod $permissionNoX "$fileFull"
	   fi
	fi
fi
