Language: EN FR PT ES NL DE SV RU
#!/bin/bash
 
#-----------------------------------------------------------------------------
#    This program is free software: you can redistribute it and/or modify
#    it under the terms of the GNU General Public License as published by
#    the Free Software Foundation, either version 3 of the License, or
#    (at your option) any later version.
#
#    This program is distributed in the hope that it will be useful,
#    but WITHOUT ANY WARRANTY; without even the implied warranty of
#    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
#    GNU General Public License for more details.
#
#    You should have received a copy of the GNU General Public License
#    along with this program.  If not, see <http://www.gnu.org/licenses/>.
#-----------------------------------------------------------------------------
 
#-----------------------------------------------------------------------------
# slackin's UrT dedicated server install script!       Version: 0.1
# Justin Clark -- slackin35@yahoo.com
# slackin35 on AIM
#
# See the bottom of the script for more information.
#-----------------------------------------------------------------------------
 
### PLEASE EDIT! #############################################################
# source_dir should be the directory where the folder UrbanTerror
# is located. ie, if UrbanTerror is /usr/local/games/UrbanTerror
# then you would put: source_dir="/usr/local/games/"
#
 
source_dir="/home/real_template/"
 
#
# That concludes the inscript config!
#
### END EDIT! ################################################################
 
 
 
# Read the username from stdin
function get_user {
 
	# Ask user what user account to install to, and read reply into varible user
	color_echo "yellow" "Please enter username:"
	read user
}
 
# Read the port from stdin
function get_port {
 
	# Ask user what port to use for this server, and read reply into varible port
	color_echo "yellow" "Please enter port:"
	read port
}
 
# Color echo function, for using echo with colors in bash, syntax: color_echo "color_name" "message to send"
function color_echo {
 
	# Case statement to set ascii color code from named color
	case "$1" in
		"blue" )
			color="\e[32m"
			;;
		"red" )
			color="\e[31m"
			;;
		"yellow" )
			color="\e[33m"
			;;
		"green" )
			color="\e[34m"
			;;
	esac
 
	# Do the actual echo, note the -e to use the escape codes
	# and notice the color reset at the end, always use that.
	echo -e "${color}${2}\e[0m"
}
 
# Install all the maps listed in the maplist file without prompting.
function map_install_all {
	color_echo "green" "Installing all maps"
 
	# Link each map listed in map filelist
	for filename in $map_filelist
	do
		if ln -s ${source_dir}${filename} ${user_dir}/UrbanTerror/q3ut4 ; then
			color_echo "green" "File linked: ${user_dir}/${filename}" ; else
 
			# if link fails, produce error and exit program
			color_echo "red" "File link FAILED: ${user_dir}/${filename}"
			exit
		fi
	done
}
 
# Install all the maps listen in maplist file and prompt for each map.
function map_install_yes {
	color_echo "green" "Selecting maps one by one"
 
	# Link each map listed, prompting first.
	for filename in $map_filelist
	do
 
		# Ask user if he want to copy this map, then read his reply.
		color_echo "green" "Link map: ${filename}  (y)es / (n)o"
		read yeno
 
		# if replied "y" then link the map.
		if [ $yeno == "y" ] ; then
			if ln -s ${source_dir}${filename} ${user_dir}/UrbanTerror/q3ut4 ; then
				color_echo "green" "File linked: ${user_dir}/${filename}" ; else
 
				# if link fails, produce error and exit program
				color_echo "red" "File link FAILED: ${user_dir}/${filename}"
				exit
			fi
		fi
	done
}
 
# Start of install script
function run_install {
	color_echo "green" "slackin's Urban Terror server install script!"
 
	# Load the used ports list, to make sure we don't use the same port twice!
	. used_ports
 
	# Check to see if the username was given at run time.
	if [ -z $user ] ; then
		get_user
	fi
 
	# Run loop until a valid username is given.
	while [ -z $user_valid ]
	do
		color_echo "green" "Checking user directory."
 
		# Check to see if the user name given has a home directory.
		if [ -e "/home/${user}" ] ; then
 
			# if user directory exists, set variable so while loop stops.
			color_echo "blue" "User directory valid!"
			user_valid="TRUE"
			user_dir="/home/${user}" ; else
 
			# if invalid, return error.
			color_echo "red" "User directory invalid!"
 
			# Run user prompt
			get_user
		fi
	done
 
	# Run loop until a valid port is given
	while [ -z $port_valid ]
	do
		color_echo "green" "Checking port."
 
		# Check to make sure port is within sane range, 1024 < port < 65535
		# if port is not within that range, return port invalid and return to port prompt
		if [[ $port -gt "1024" && $port -lt 65535 ]] ; then
 
			# Case, if $port is in $port_list return taken and return to port prompt
			# if not found, default, Tell them port is good.
			case $port_list in
				*"${port}"* )
					color_echo "red" "Port taken! Try another."
 
					# Run port prompt
					get_port
					;;
				* )
					port_valid="TRUE"
					color_echo "blue" "Port valid!"
					;;
			esac ; else
			color_echo "red" "Port invalid!"
 
			# Run port prompt
			get_port
		fi
	done
 
	# Update used port list with new port
	port_list="${port_list} ${port}"
 
	# Write new used port list to file
	echo "port_list=\"$port_list"\" 1> used_ports
 
	# Create directory UrbanTerror in user's home folder
	if mkdir ${user_dir}/UrbanTerror ; then
		color_echo "blue" "Directory created: ${user_dir}/UrbanTerror" ; else
 
		# if failed to create, exit program
		color_echo "red" "Directory failed to create: ${user_dir}/UrbanTerror"
		exit
	fi
 
	# Create q3ut4 directory in UrbanTerror
	if mkdir ${user_dir}/UrbanTerror/q3ut4 ; then
		color_echo "blue" "Directory created: ${user_dir}/UrbanTerror/q3ut4" ; else
 
		# if failed to create, exit program
		color_echo "red" "Directory failed to create: ${user_dir}/UrbanTerror/q3ut4"
		exit
	fi
 
	# Load the list of files to be linked and copied
	. filelist
 
	# Create link for each file in /UrbanTerror/
	for filename in $urt_filelist
	do
		if ln -s ${source_dir}${filename} ${user_dir}/UrbanTerror/ ; then
			color_echo "blue" "File linked: ${user_dir}/${filename}" ; else
 
			# if link fails, produce error and exit program
			color_echo "red" "File link FAILED: ${user_dir}/${filename}"
			exit
		fi
	done
 
	# Create link for each file in /UrbanTerror/q3ut4 except configs and maplist
	for filename in $q3ut4_filelist
	do
		if ln -s ${source_dir}${filename} ${user_dir}/UrbanTerror/q3ut4/ ; then
			color_echo "blue" "File linked: ${user_dir}/${filename}" ; else
 
			# if link fails, produce error and exit program
			color_echo "red" "File link FAILED: ${user_dir}/${filename}"
			exit
		fi
	done
 
	# Copy baseline server configs and mapcycle.txt to server
	for filename in $cp_filelist
	do
		if cp ${source_dir}${filename} ${user_dir}/UrbanTerror/q3ut4/ ; then
			color_echo "blue" "File copied: ${user_dir}/${filename}" ; else
 
			# if copy fails, produce error and exit.
			color_echo "red" "File copy FAILED: ${user_dir}/${filename}"
			exit
		fi
	done
 
	# Create the server startup script with port specified earlier
	# in user's home dir, then make it executable.
	echo "#!/bin/bash" 1> ${user_dir}/start.sh
	echo "while true" 1>> ${user_dir}/start.sh
	echo "do" 1>> ${user_dir}/start.sh
	echo "${user_dir}/UrbanTerror/ioUrTded.x86_64 +set fs_game q3ut4 +set dedicated 2 +set net_port ${port} +set com_hunkmegs 256 +exec server.cfg" 1>> ${user_dir}/start.sh
	echo 'echo "server crashed on `date`" > last_crash.txt' 1>> ${user_dir}/start.sh
	echo "done" 1>> ${user_dir}/start.sh
 
	# Changing to executable
	if chmod +x ${user_dir}/start.sh ; then
		color_echo "blue" "Start script created successfully: ${user_dir}/start.sh" ; else
 
		# If chmod fails, produce error and exit.
		color_echo "red" "Start script failed to create: ${user_dir}/start.sh"
		exit
	fi
 
	# Changing owner ship of entire /UrbanTerror folder to username
	if chown -Rf ${user}:${user} ${user_dir}/UrbanTerror ; then
		color_echo "blue" "Ownership changed to ${user}:${user}" ; else
 
		# If failed, produce error and exit.
		color_echo "red" "Problem changing ownership to ${user}:${user} on directory: ${user_dir}/UrbanTerror"
		exit
	fi
 
	# Ask user if he would like more maps, and read his reply.
	color_echo "yellow" "Would you like to install additional maps? (a)ll / (y)es / (n)o"
	read inst_maps
 
	# Case: a - install all maps without prompting
	# Case: y - install all maps prompting before each map.
	case "$inst_maps" in
		"a" )
 
			# Run function to install w/o prompts
			map_install_all $map_filelist $user_dir
			;;
		"y" )
 
			# Run function to install w/ prompts
			map_install_yes $map_filelist $user_dir
			;;
	esac
 
	# Ask user if he would like additional league configs, read reply
	color_echo "yellow" "Install additional configs? (y)es / (n)o"
	read cfg_inst
 
	# If reply "y" then selectively ask for each league config
	if [ $cfg_inst == "y" ] ; then
		for filename in $cfg_filelist
		do
 
			# Ask user if they would like named config
			color_echo "yellow" "Copy config: ${filename}  (y)es / (n)o"
			read yeno
 
			# If they replied "y", copy the config to users /UrbanTerror/q3ut4
			if [ $yeno == "y" ] ; then
				if cp ${source_dir}${filename} ${user_dir}/UrbanTerror/q3ut4 ; then
					color_echo "blue" "File copied: ${user_dir}/${filename}" ; else
 
					# if copy fails, produce error and exit.
					color_echo "red" "File copy FAILED: ${user_dir}/${filename}"
					exit
				fi
			fi
		done
 
		# Ask if installing as a pugbot server.
		color_echo "yellow" "Install pugbot configs?  (y)es / (n)o"
		read pug_inst
 
		# If replied "y" copy all pugbot configs listed.
		if [ $pug_inst == "y" ] ; then
			for filename in $pugcfg_filelist
			do
				if cp ${source_dir}${filename} ${user_dir}/UrbanTerror/q3ut4 ; then
					color_echo "blue" "File copied: ${user_dir}/${filename}" ; else
 
					# if copy fails, produce error and exit.
					color_echo "red" "File copy FAILED: ${user_dir}/${filename}"
					exit
				fi
			done
		fi
	fi
}
 
# Set command line parms to user and port, syntax: install-urtded.sh username port
user=$1
port=$2
 
# Run main install function, passing on user and port parms
run_install $user $port
 
#--------------------------------------------------------------------------------------------
# About the script:
#
# This script was created so that a server admin of a fairly larger server
# could easily install new urt servers without wasting excessive space
# or wasting even more valuable time.
#
# You can see by the sample files provided the layout of the files pretty easy.
# You can add as many maps or configs to the lists as you want.
# I would not advise messing with the base line install file lists,
# they are picked pretty selectively as to make sure min space usage
# but copy any files that server operator may want to change later.
# The file lists are loaded from the same dir as the script.
#
# Syntax for running: install-urtded.sh username port
# username should be a valid user on the system with a directory in /home/
# port should be between 1024 and 65535 and not already be taken by another server
#
#
# Just another one in a collection of tools for UrbanTerror by slackin.
# Check out the Pick-Up game system, PUGBOT @ http://pugbot.com/
# To talk about this script or any other script/tool by slackin,
# try using the pugbot forums, link to the forums: http://forums.pugbot.com/
#
# Hope one of you guys finds this useful!
#--------------------------------------------------------------------------------------------