#!/bin/bash
#
# by Emiliano Gabrielli <AlberT@SuperAlberT.it>
#
# dedicated to ziabice, he doesn't like setting permissions :-P
#
# $Id$


# Put here a list of writeble files or dirs, space separeted !!!
WF="plugins/counter/counter.dat AES/AES_tmp_dir AES/AES_tmp_dir/cache"

# A list of readable directories, space separeted !!!
RD="plugins themes AES/AES_tmp_dir/cache packages/*/plugins"

if !( [[ -d $1 ]] ) ; then
	echo -n Usage: $0 ROOT_SITE_DIR
	echo
	exit 1
fi

DIR=$1 ;

GROUP=`ls -dl $DIR | awk '{print $4}'`;

echo -n "changing permissions on '$DIR' ....";

if ([[ $GROUP = `ps auxw | grep 'httpd\|apache'  | head -2 | tail -1 | awk '{print $1}'` ]] ); then
	chgrp $GROUP -R $DIR
	chmod -R o-rwx $DIR
	chmod -R g=X $DIR
	chmod g=r `find $DIR -type f`
	chmod g+w `find $DIR -type f -name config.inc.php` ;
	for i in $WF; do
		chmod g+w $DIR/$i ;
	done;
	for i in $RD; do
		chmod g+r $DIR/$i ;
	done;
else
	chmod -R o-rwx $DIR
	chmod -R o=X $DIR
	chmod -R o=r `find $DIR -type f`
	chmod o+w `find $DIR -type f -name config.inc.php` ;
	for i in $WF; do
		chmod o+w $DIR/$i ;
	done;
	for i in $RD; do
		chmod o+r $DIR/$i ;
	done;
fi

echo -n "DONE !"
echo

