#!/bin/bash
# LGSM fn_start function
# Author: Daniel Gibbs
# Website: http://gameservermanagers.com
# Version: 261015

# Description: Starts the server.

local modulename="Starting"

fn_start_teamspeak3(){
fn_check_root
fn_check_systemdir
fn_check_logs
fn_check_ts3status

if [ "${ts3status}" != "Server is running" ]; then
	# Will check for updates is updateonstart is yes
	if [ "${updateonstart}" == "yes" ]||[ "${updateonstart}" == "1" ]||[ "${updateonstart}" == "on" ]; then
		fn_update_check
	fi	
fi

if [ ! -e "${servercfgfullpath}" ]; then
	fn_printwarn "${servercfgfullpath} is missing"
	fn_scriptlog "${servercfgfullpath} is missing"
	sleep 2
	echo -en "\n"
	echo  "	* Creating blank ${servercfg}"
	fn_scriptlog "Creating blank ${servercfg}"
	sleep 2
	echo  "	* ${servercfg} can remain blank by default."
	fn_scriptlog "${servercfgfullpath} can remain blank by default."
	sleep 2
	echo  "	* ${servercfg} is located in ${servercfgfullpath}."
	fn_scriptlog "${servercfg} is located in ${servercfgfullpath}."
	sleep 5
	touch "${servercfgfullpath}"
fi

fn_logs

fn_printdots "${servername}"
fn_scriptlog "${servername}"
sleep 1

if [ "${ts3status}" == "Server is running" ]; then
	fn_printinfo "${servername} is already running"
	fn_scriptlog "${servername} is already running"
	sleep 1
	echo -en "\n"
	exit
fi

mv "${scriptlog}" "${scriptlogdate}"
# Create lock file
date > "${rootdir}/${lockselfname}"
cd "${executabledir}"
./ts3server_startscript.sh start inifile="${servercfgfullpath}" > /dev/null 2>&1
sleep 1
fn_check_ts3status
if [ "${ts3status}" = "Server seems to have died" ] || [ "${ts3status}"	= "No server running (ts3server.pid is missing)" ];then
	fn_printfailnl "Unable to start ${servername}"
	fn_scriptlog "Unable to start ${servername}"
	echo -e "	Check log files: ${rootdir}/log"
else
	fn_printok "${servername}"
	fn_scriptlog "Started ${servername}"
fi
sleep 0.5
echo -en "\n"
}

fn_start_tmux(){
fn_check_root
fn_check_systemdir
fn_check_ip
fn_check_logs
if [ "${gamename}" == "Counter Strike: Global Offensive" ]; then
	startfix=1
	fn_csgofix
fi
if [ "${gamename}" == "Insurgency" ]; then
	fn_insfix
fi
fn_details_config
fn_parms
fn_logs

tmuxwc=$(tmux list-sessions 2>&1|awk '{print $1}'|grep -v failed|grep -Ec "^${servicename}:")
# Will check for updates if updateonstart is yes
if [ "${tmuxwc}" -eq 0 ]; then
	if [ "${updateonstart}" == "yes" ]||[ "${updateonstart}" == "1" ]||[ "${updateonstart}" == "on" ]; then
		fn_update_check
	fi
fi

fn_printdots "${servername}"
fn_scriptlog "${servername}"
sleep 1
fn_check_tmux

if [ "${gamename}" != "Mumble" ]; then
	fn_check_steamcmd
fi

if [ "${tmuxwc}" -eq 0 ]; then
	fn_scriptlog "Rotating log files"
	if [ "${engine}" == "unreal2" ]; then
		mv "${gamelog}" "${gamelogdate}"
	fi
	mv "${scriptlog}" "${scriptlogdate}"
	mv "${consolelog}" "${consolelogdate}"
fi

if [ "${tmuxwc}" -eq 1 ]; then
	fn_printinfo "${servername} is already running"
	fn_scriptlog "${servername} is already running"
	sleep 1
	echo -en "\n"
	exit
fi

# Create lock file
date > "${rootdir}/${lockselfname}"
cd "${executabledir}"
tmux new-session -d -s "${servicename}" "${executable} ${parms}" 2> "${scriptlogdir}/.${servicename}-tmux-error.tmp"
# tmux pipe-pane not supported in tmux versions < 1.6
if [ "$(tmux -V|sed "s/tmux //"|sed -n '1 p'|tr -cd [:digit:])" -lt "16" ]; then
	echo "Console logging disabled: Tmux => 1.6 required" >> "${consolelog}"
	echo "Currently installed: $(tmux -V)" >> "${consolelog}"
elif [ "$(tmux -V|sed "s/tmux //"|sed -n '1 p'|tr -cd [:digit:])" -eq "18" ]; then
	echo "Console logging disabled: Bug in tmux 1.8 breaks logging" >> "${consolelog}"
	echo "Currently installed: $(tmux -V)" >> "${consolelog}"
else
	touch "${consolelog}"
	tmux pipe-pane -o -t "${servicename}" "exec cat >> '${consolelog}'"
fi
sleep 1
tmuxwc=$(tmux list-sessions 2>&1|awk '{print $1}'|grep -Ec "^${servicename}:")
# If the server fails to start
if [ "${tmuxwc}" -eq 0 ]; then
	fn_printfail "Unable to start ${servername}"
	fn_scriptlog "Unable to start ${servername}"
	sleep 1
	if [ -s "${scriptlogdir}/.${servicename}-tmux-error.tmp" ]; then
		fn_printfail "Unable to start ${servername}: Tmux error:"
		fn_scriptlog "Tmux error"
		sleep 1
		echo -en "\n"
		echo ""
		echo "Command"
		echo "================================="
		echo "tmux new-session -d -s \"${servicename}\" \"${executable} ${parms}\""
		echo "tmux new-session -d -s \"${servicename}\" \"${executable} ${parms}\"" >> "${scriptlog}"
		echo ""
		echo "Error"
		echo "================================="
		cat "${scriptlogdir}/.${servicename}-tmux-error.tmp"
		cat "${scriptlogdir}/.${servicename}-tmux-error.tmp" >> "${scriptlog}"

		# Detected error http://gameservermanagers.com/issues
		if [ $(grep -c "Operation not permitted" "${scriptlogdir}/.${servicename}-tmux-error.tmp") ];then
		echo ""
		echo "Fix"
		echo "================================="
			if [ ! $(grep "tty:" /etc/group|grep "$(whoami)") ];then
				echo "$(whoami) is not part of the tty group."
				fn_scriptlog "$(whoami) is not part of the tty group."
				group=$(grep tty /etc/group)
				echo ""
				echo "	${group}"
				fn_scriptlog "${group}"
				echo ""
				echo "Run the following command with root privileges."
				echo ""
				echo "	usermod -G tty $(whoami)"
				echo ""
				echo "http://gameservermanagers.com/tmux-op-perm"
				fn_scriptlog "http://gameservermanagers.com/tmux-op-perm"
			else
				echo "No known fix currently. Please log an issue."
				fn_scriptlog "No known fix currently. Please log an issue."
				echo "http://gameservermanagers.com/issues"
				fn_scriptlog "http://gameservermanagers.com/issues"
			fi
		fi
	fi
else
	fn_printok "${servername}"
	fn_scriptlog "Started ${servername}"
fi
rm "${scriptlogdir}/.${servicename}-tmux-error.tmp"
echo -en "\n"
}

if [ "${gamename}" == "Teamspeak 3" ]; then
	fn_start_teamspeak3
else
	fn_start_tmux
fi
