/*

ACROBATICS I/O CONFIG BLOCK

What goes in, must be saved to /luafiles.

*/

if acrobatics == nil then return end

-----------------------------

local defSettings = [[#Acrobatics v0.5 Settings
#
#Change the 2nd option on each setting! SERVER OPTIONS will only run if you're hosting or an admin.
#Your options are outlined in []s.
#===============================================================
#DON'T EDIT ANYTHING WITH A "#" - it will NOT be read!
#
#PLAYER OPTIONS - 
#
# Preferred trickstyles - [burst, store, augment]
ac_trickstyle burst
#  ***Won't work on servers with locked styles, beware!
#
#Trick Reminder - [on, off]
ac_trickreminder on
#
#Meter Style - [none, minimal, simple, default, alternative]
ac_meterstyle default
#
#Text Alignment - [centered, left]
ac_textalignment centered
#
#Store Bar Snaps to Left? - [yes, no]
ac_barsnap yes
#
#Augment Percentage Display - [potential, raw]
ac_augshow potential
# ***Does NOT affect actual speed output.
#
#!!! SERVER OPTIONS - 
#
#Acrobatics Active - [on, off]
ac_active on
#
# Lock Tricks on a Server - [freestyle, burst, store, augment]
ac_forcetrick freestyle
#Text Debug - [on, off]
ac_textdebug off
# ***WARNING: Will spam unfunny memes in your log. You shouldn't need this to be on unless you're debugging something.
#
#Acrohell Mode - [forced, disabled, hellmap]
ac_acrohell hellmap
# ***WARNING: I hope you know what you're doing if you modify this option.
]]

local function loadConfig(p, arg)
	local loadthis = io.open(arg or "acrobatics.cfg", "r") 
	--Not INsertive.
	assert(loadthis ~= nil, "No config file found! \142...Did it get deleted somehow??\131 Reload the game and load the mod again to generate a fresh copy.")
	
	--I don't know why I'm particularly checking this, I guess just to make sure to confirm they're loading a different file.
	if arg ~= "acrobatics.cfg" and arg ~= nil
		print("\142*...This doesn't look like the default Acrobatics config file.")
		print("\131*Applying settings anyway!")
	else
		print("\131*Applying settings from acrobatics.cfg...")
	end
	
	--babby's first string match HAW HAW
	for l in loadthis:lines() do
		local ignoreMe = string.match(l, "#") 
		local serverCheck = string.match(l, "!!!")
		if ignoreMe then continue end
		--i'll figure it out later
		/*if serverCheck 
			--print(#p .. " is player, server is" .. #server)
			if not(IsPlayerAdmin(p)) or p != server
				print("\132*Settings applied!")
				return
			else
				print("\131*Applying server settings...")
				continue
			end
		end*/
		COM_BufInsertText(p, l)
	end
	
	print("\131*Settings applied!")
	
end
COM_AddCommand("ac_load", loadConfig) --manual loads of other

--First load/file creation.

local writeme = io.open("acrobatics.cfg", "r")
if not writeme
	print("\142*NOTICE:\128 Didn't find an acrobatics config file, let's make one real quick...!")
	writeme = io.open("acrobatics.cfg", "w")
	writeme:write(defSettings)
else
	print("\128Acrobatics config file found.")
	if not(netgame)
		print("\130Settings will be loaded when you start or join a game.")
	end
end
writeme:close()

--what do you MEAN there's no way to find a player during the title screen :V
local firstLoad = false

local function firstloadThink()
	if firstLoad then return end
	for p in players.iterate
		if displayplayers[0] == p --otherwise it loads the config file FOUR times in splitscreen :|
			print("\130Acrobatics settlings not applied yet, let's fix that...")
			loadConfig(p, "acrobatics.cfg")
			firstLoad = true
		end
	end
end

addHook("ThinkFrame", firstloadThink)
addHook("IntermissionThinker", firstloadThink)
addHook("VoteThinker", firstloadThink)

print("\131* Acrobatics " .. acrobatics .. "\131, by Angular. Load successful! Please spin responsibly. *")