Source

Selador / INSTALL.sh

#!/bin/bash
# This is a new version of the Selador installation script
# It should work more comfortable and on any unix-like system

##########
# You can set the two variables on your own
# By default they take the arguments given
##########

# Your username, i.e. the user working with selador (default is the argument given)
userName="$1"

# The group the webserver belongs to (default is the argument given)
serverGroup="$2"

##########
# Nothing need to be edited beyond this line (hopefully)
##########


# The Introtext, nothing special...
echo "The script tries to 'guess' two options:"
echo "1. Your username"
echo "2. The group the webserver belongs to"
echo "You can set both by setting the appropriate variables in this script or starting this script with the appropriate arguments."
echo "'./INSTALL <username> <serverGroupName>' for example './INSTALL johnDoe www-data'"
echo ""


# Check that user is not root
[ `whoami` == "root" ] && echo "!!! Do not run script as root but as ordinary user." && exit

# Check that sudo is installed
[ -z `which sudo 2>/dev/null` ] && echo "!!! The sudo command is not installed. Please install it or adapt this script to your needs." && exit

# Get the current user if not set
if [ -z "$userName" ]
then
	userName=`whoami`
fi

# Try to get the group the http-server belongs to if not set
if [ -z "$serverGroup" ]
then
	# sudo might be needed in case /etc/ is not readable for ordinary users
	echo "Trying to guess group. sudo is needed so please enter your password if needed."
	findGroup=`sudo find /etc/ -type f -name "*.conf" -exec grep "^Group " {} \; 2> /dev/null | awk '{ print $2 }' ;`
	
	# Debug output if something is wrong
	[ -z "$findGroup" ]  && echo "!!! Something went wrong while trying to find the webservers group. You have to set it on your own. Sorry." && exit

	# Check if there is only one possible result
	if [[ "$findGroup" =~ .*$'\n'.* ]]
	then
		# If multiple result exist, the result is treated as an array
		# Therefore IFS is set to newline to use it so transform the String into an array to work on
		OLD_IFS="$IFS"
		IFS=$'\n'
		serverArr=( $findGroup )
		IFS="$OLD_IFS"

		# A new variable is introduced to check if a found groups are equal
		equal="true"

		# Set the first element to be compared
		comp=${serverArr[0]}

		# Compare all groups found to the first one.
		for (( i = 0 ; i < ${#serverArr[@]} ; i++ ))
		do
			[ "$comp" != "${serverArr[$i]}" ] && equal="false"
		done

		# If all found groups are equal it's assumed that this is the right group.
		# Else the user has to set the group on his own
		if [ "$equal" == "false" ]
		then
			echo "!!! Found multiple groups, which are not equal."
			echo "!!! This implies that you have to set the group on your own."
			echo "!!! The possible groups having been found are:"
			for (( i = 0 ; i < ${#serverArr[@]} ; i++ ))
			do
				echo "${serverArr[$i]}"
			done
			echo "!!! They have been found in following files:"
			echo `sudo find /etc/ -type f -name "*.conf" -exec grep -l "^Group " {} \; 2> /dev/null` 
			exit
		fi
		serverGroup="$comp"

	else
		serverGroup="$findGroup"
	fi

	# It seems like ubuntu is working with enviroment variables to set the group/user of the webserver
	# We take care of this now
	if [[ "$serverGroup" =~ '${'.* ]]
	then
		# Remove brackets in front and end
		groupVar=${serverGroup#'${'}
		groupVar=${groupVar%'}'}

		# Find where the variable is initialized
		varMatch=`sudo find /etc/ -type f -exec grep " $groupVar=" {} \; 2> /dev/null ;`
		# We hope there is only one match, but to be sure we add an error message at least
		if [[ "$varMatch" =~ .*$'\n'.* ]]
		then
			echo "!!! Two places found, where $groupVar is set."
			echo "!!! This situation cannot be treated now."
			echo "!!! Please fix the export statement or adapt the sript."
			echo "!!! Files where $groupVar is set:"
			echo `sudo find /etc/ -type f -exec grep -l " $groupVar=" {} \; 2> /dev/null ;`
			echo "!!! Installation aborted."
			exit
		fi
		
		# Finally set the group
		serverGroup=${varMatch#"export $groupVar="}
	fi
fi

# Show gathered information to user and propose to go on with those information
echo "Do you want to continue installation with following information?"
echo "Name for 'Selador-User': $userName"
echo "Group the webserver belongs to: $serverGroup"
echo "(y for yes)"
read tmp
[ "$tmp" != "y" ] && echo "Installation aborted" && exit

# Set appropriate rights
echo "..Adjusting rights"
sudo ./setRights.sh $userName $serverGroup


# The user wants to go on, so create needed folders
echo "....Creating folders"
[ -d local ] || [ -h local ] || sudo mkdir local 
[ -d var ] || sudo mkdir var
[ -d var/help ] || sudo mkdir var/help
[ -d var/help/auto ] || sudo mkdir var/help/auto
[ -d var/log ] || sudo mkdir var/log
[ -d var/php ] || sudo mkdir var/php
[ -d var/precalc ] || sudo mkdir var/precalc
[ -d var/preinfo ] || sudo mkdir var/preinfo
[ -d var/premap ] || sudo mkdir var/premap
[ -d local/hof ] || [ -h local/hof ] || sudo mkdir local/hof

# Set appropriate rights
echo "......Adjusting rights"
sudo ./setRights.sh $userName $serverGroup


# Pack in default event and unit maps
echo "........Creating php/unitmap.php and php/base/eventmap.php"
[ -f "var/php/unitmap.php" ] || sudo cp php/unitmap.sample.php var/php/unitmap.php

[ -f "php/base/eventmap.php" ] || sudo cp php/base/eventmap.sample.php var/php/eventmap.php



# Copy the needed config files
echo "..........Creating brain/userconfig.inc.php"
[ -f "brain/userconfig.inc.php" ] || sudo cp brain/userconfig.inc.sample.php brain/userconfig.inc.php

echo "..........Creating php/base/config.inc.php"
[ -f "php/base/config.inc.php" ] || sudo cp php/base/config.inc.sample.php php/base/config.inc.php

echo "..........Creating php/base/forum_config.inc.php"
[ -f "php/base/forum_config.inc.php" ] || sudo cp php/base/forum_config.inc.sample.php php/base/forum_config.inc.php

# Set appropriate rights
echo "............Adjusting rights"
sudo ./setRights.sh $userName $serverGroup

echo ""
echo ""
echo "--------------------"
echo "Installation complete"
echo "Edit php/base/config.inc.php to adjust Selador to your configuration."
echo "Edit brain/userconfig.inc.php to be able to access admin console."
echo "If needed edit php/base/forum_config.inc.php as well."
echo Finally go to http://YOURDOMAIN/brain/admin.php and export buildings or research.