COM_AddCommand("supermusic", function(player, arg)
	if player and (player.mo and player.mo.valid)
		if arg
			local file = io.openlocal("client/SuperMusic/SuperMusic-" .. player.mo.skin .. ".dat","w+")
			file:write(arg)
			file:close()
			player.supermusic = string.upper(arg)
			if arg == "_super" or arg == "default"
				CONS_Printf(player, "Super Music set to the default!")
			else
				if arg != "off" and arg != "none"
					CONS_Printf(player, "Super Music set to "..string.upper(arg).."!")
				else
					CONS_Printf(player, "Super music turned off!")
				end
			end
			S_StartSound(nil, sfx_kc48, player)
		else
			CONS_Printf(player,"Usage: supermusic <music name> | Sets super music for current character.\nSuper Music is currently set to "..player.supermusic)
			S_StartSound(nil, sfx_addfil, player)
		end
	else
		CONS_Printf(player, "You need to be in a level to use this.")
	end
end, COM_LOCAL)

local supermusicplaying = false
addHook("ThinkFrame", function()
	for player in players.iterate do
		if not(player.mo and player.mo.valid) then continue end
		if player.supermusic == nil or player.mo.skin != player.prevskin
			local file = io.openlocal("client/SuperMusic/SuperMusic-" .. player.mo.skin .. ".dat")
			if file
				player.supermusic = file:read("*a")
				file:close()
			else
				player.supermusic = "default"
			end
		end
	end
end)

addHook("MusicChange", function(oldname, newname)
	if consoleplayer
		local supermusic = consoleplayer.supermusic
		local custommusic = consoleplayer.hassupermusic or supermusic == "default"
		local nomusic = supermusic == "off" or supermusic == "none"
		if newname == "_super"
			if not custommusic
				return supermusic
			elseif nomusic
				return S_MusicName(consoleplayer)
			end
		end
	end
end)

addHook("PlayerThink", function(player)
	if not (player.mo and player.mo.valid) then return end
	player.prevskin = player.mo.skin
end)