#!/bin/bash

#
#  Variables: change these to match your setup.
#

RM_HOME=/var/www/redmine

APACHE_USER=apache

###########################################################
# These permissions are needed for Apache to run Redmine. #
###########################################################

if [ -z ${RM_HOME} -o ! -d ${RM_HOME} ]; then
	echo Error: The path to Redmine is not a valid path: ${RM_HOME}.
	exit 1
fi

#
# Base permissions.
#

chown -R root:${APACHE_USER} ${RM_HOME}
chmod -R u=rw,g=r,o-rwx ${RM_HOME}
chmod -R ug+X ${RM_HOME}
chcon -R -u system_u -t httpd_sys_content_t ${RM_HOME}

#
# Writable directories.
#

chown -R ${APACHE_USER}:${APACHE_USER} ${RM_HOME}/log
chcon -R -t httpd_log_t ${RM_HOME}/log

chown -R ${APACHE_USER}:${APACHE_USER} ${RM_HOME}/tmp
chcon -R -t httpd_tmpfs_t ${RM_HOME}/tmp

chown -R ${APACHE_USER}:${APACHE_USER} ${RM_HOME}/files
chcon -R -t httpd_sys_script_rw_t ${RM_HOME}/files

if [ ! -d ${RM_HOME}/public/plugin_assets ]; then
	echo Info: Creating directory ${RM_HOME}/public/plugin_assets.
fi
chown -R ${APACHE_USER}:${APACHE_USER} ${RM_HOME}/public/plugin_assets
chcon -R -t httpd_sys_script_rw_t ${RM_HOME}/public/plugin_assets
