Selador /

# 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)

# The group the webserver belongs to (default is the argument given)

# 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" ]

# Try to get the group the http-server belongs to if not set
if [ -z "$serverGroup" ]
	# 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'.* ]]
		# 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
		serverArr=( $findGroup )

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

		# Set the first element to be compared

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

		# 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" ]
			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++ ))
				echo "${serverArr[$i]}"
			echo "!!! They have been found in following files:"
			echo `sudo find /etc/ -type f -name "*.conf" -exec grep -l "^Group " {} \; 2> /dev/null` 


	# 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" =~ '${'.* ]]
		# Remove brackets in front and end

		# 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'.* ]]
			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."
		# Finally set the group
		serverGroup=${varMatch#"export $groupVar="}

# 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 ./ $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 ./ $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/"
[ -f "brain/" ] || sudo cp brain/ brain/

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

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

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

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