#! /bin/sh

# Change file permissions
# used to prepare a new version of wims from svn repository
# and at the end of installation of a new version.

cd `dirname $0`/..
wimshome=`pwd`


cd $wimshome
dir=`pwd`
echo "Change file permissions in " $wimshome

noread="bin sessions s2 src log log/classes log/forums log/.developers\
 backup tmp/log\
 public_html/bin public_html/msg\
 public_html/bases/dic public_html/bases/sheet\
 public_html/bases/site public_html/bases/sys\
 public_html/bases/doc\
 public_html/scripts/adm\
 public_html/modules/home public_html/modules/adm"

nowrite="public_html README README_rpm HEADER.html download other\
 compile localwims chroot/bin chroot/usr chroot/etc chroot/sbin chroot/lib"

nowrite2="tmp tmp/sessions chroot chroot/tmp/sessions"

noexec="public_html/modules"

users="bin public_html chroot/*"

wexec="other other/lib other/share tmp tmp/forall tmp/sessions -maxdepth 0"

wexec2="other/bin"

# noread : 
echo "****************************  noread ****************************"

for f in $noread
do
# permission multiple de 4x16=64 ? (i.e. aucune perm pour group et other)
 test=`stat -Lt $f | cut -d' ' -f4 | grep '..[048c]0'`
 if [ -e $f ] && [ -z "$test" ]; then
 test1=`stat -Lt $f | cut -d' ' -f4`
  echo "bad permission " $test1 "for " $dir/$f 
 chmod og-rwx $f
 fi
done




# nowrite : 
echo "


****************************  nowrite nowrite2 ****************************"
cd $wimshome

test=`find $nowrite -perm +22 \! -type l 2>/dev/null | head -20000`
# Attention, j'ai remplacé w_nowrite2 par nowrite2
test2=`find $nowrite2 -maxdepth 0 -perm +22 \! -type l 2>/dev/null | head -20000`
if [ ! -z "$test$test2" ]; then 
 chmod og-w $test $test2 
echo "$test
$test2" | head -10
echo "........... "
echo "$test
$test2" | tail -10
fi
NF=`echo "$test$test2" | wc -l`
echo "
Number of nowrite files changed : " $NF

echo "


****************************  noexec noexec2 ****************************"
cd $wimshome
test=`find $noexec -perm +11 -type f 2>/dev/null | head -20000`
if [ ! -z "$test" ]; then 
# vrai chmod
chmod og-x $test 
echo "$test" | head -10
echo "........... "
echo "$test" | tail -10
fi
NF=`echo "$test" | wc -l`
echo "
Number of noexec files changed : " $NF

echo "


**************************** users ****************************"
cd $wimshome
i=`id -un`
echo "File which don't belong to wims : "
find $users \! -user $i | head -60 | grep -v '^chroot/usr/bin/\[$'


echo "


**************************** wexec wexec2 ****************************"
cd $wimshome
testd=`find $wexec -follow -type d \! -perm -11 2>/dev/null | head -20000`
testf=`find $wexec2 -follow -type f \! -perm -11 2>/dev/null | head -20000`
test="$testd $testf"
if [ ! -z "$testd$testf" ]; then 
chmod a+rx $test; 
echo "$test" | head -10
echo "........... "
echo "$test" | tail -10
fi
NF=`echo "$test" | wc -l`
echo "
Number of wexec files changed : " $NF


echo "end of changing file permissions in " $wimshome
