#!/bin/bash
SUGARBASEPATH='/var/www/fusion/'
SUGARINSTANCE=$1
APACHEUSER='www-data:www-data'
SUGARPATH=$SUGARBASEPATH$SUGARINSTANCE

if [ -n "$SUGARINSTANCE" ];
then
	if [ -d "$SUGARPATH" ];
	then
		echo -e "\nSetpermission Script Started:"
		echo -e "Setting Up Permission On $SUGARINSTANCE"
		sudo chmod -R 755 "$SUGARPATH"
		echo -e "Setting Up Ownership On $SUGARINSTANCE"
		sudo chown -R $APACHEUSER $SUGARPATH

		folders=(cache custom data modules include upload)
		for folder in ${folders[@]}
		do
		   if [ -d "$SUGARPATH"/"$folder" ];
			then
				echo -e "Setting Up Permission On $folder"
				sudo chmod -R 777 "$SUGARPATH"/"$folder"
			else
				echo -e "Cannot Set Permission On $folder, Directory Not Found"
			fi
		done
		echo -e "Permission Applied Successfully\n"
	else
		echo -e "\nSugarCRM Instance $SUGARINSTANCE Not Found At Path $SUGARBASEPATH\n"
	fi
else
	echo -e "\nSpecify The SugarCRM Instance Name!\n"
fi

