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

# Description: Runs a server validation.

local modulename="Validate"

fn_validation(){
fn_printwarn "Validating may overwrite some customised files."
sleep 1
echo -en "\n"
echo -en "https://developer.valvesoftware.com/wiki/SteamCMD#Validate"
sleep 5
echo -en "\n"
fn_printdots "Checking server files"
sleep 1
fn_printok "Checking server files"
fn_scriptlog "Checking server files"
sleep 1
cd "${rootdir}"
cd "steamcmd"
fn_check_steamcmd
fn_check_steamuser

if [ $(command -v unbuffer) ]; then
	unbuffer=unbuffer
fi

if [ "${engine}" == "goldsource" ]; then
	${unbuffer} ./steamcmd.sh +login "${steamuser}" "${steampass}" +force_install_dir "${filesdir}" +app_set_config 90 mod ${appidmod} +app_update "${appid}" +app_update "${appid}" validate +quit|tee -a "${scriptlog}"
else
	${unbuffer} ./steamcmd.sh +login "${steamuser}" "${steampass}" +force_install_dir "${filesdir}" +app_update "${appid}" validate +quit|tee -a "${scriptlog}"
fi

if [ "${gamename}" == "Counter Strike: Global Offensive" ]; then
	echo -e '\n'
	fn_csgofix
fi
fn_scriptlog "Checking complete"
}

fn_check_root
fn_check_systemdir
tmuxwc=$(tmux list-sessions 2>&1|awk '{print $1}'|grep -v failed|grep -Ec "^${servicename}:")
if [ "${tmuxwc}" -eq 1 ]; then
    fn_stop
    fn_validation
    fn_start
else
    fn_validation
fi
