#!/usr/bin/env ruby

=begin

    ALIEN ARENA CONFIG CHECK (for server admins) V1.0
    Copyright (C) 2009 Tony Jackson

    This library is free software; you can redistribute it and/or
    modify it under the terms of the GNU Lesser General Public
    License as published by the Free Software Foundation; either
    version 2.1 of the License, or (at your option) any later version.

    This library 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
    Lesser General Public License for more details.

    You should have received a copy of the GNU Lesser General Public
    License along with this library; if not, write to the Free Software
    Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA

    Tony Jackson can be contacted at tonyj@cooldark.com
	
=end

# Program starts here

# $* is array of args
if $*.include?('--help') then
	puts ''
	puts 'Server config check utility for Alien Arena'
	puts ''
	puts '  Checks map files listed in each .cfg sv_maplist are present in data1/maps'
	puts ''
	puts '  Usage:'
	puts '  check-configs'
	puts '  Optional arguments:'
	puts ''
	puts '  --help  This message'
	puts '  -all    Also check against 3rd party maps in arena/maps directory'
	puts ''
	exit
end

mappath1st = "../../data1/maps/"
mappath3rd = "../../arena/maps/"

if File.exists?(mappath1st) and File.directory?(mappath1st) then
	maps1stparty = Dir.entries(mappath1st)
else
	maps1stparty = Array.new
end

if File.exists?(mappath3rd) and File.directory?(mappath3rd) then
	maps3rdparty = Dir.entries(mappath3rd)
else
	maps3rdparty = Array.new
end

Dir.foreach("../../arena/") do |entry|
	if entry.split('.')[1] == 'cfg' then

		cfgFile = File.open('../../arena/'+entry, 'r')

		# read file and get sv_maplist line
		while (line = cfgFile.gets)

			if line.split(' ')[1] == 'sv_maplist' then
				maplist = line.split(' ')[2..-1].map! {|map| map.gsub(/\"/, '') }
			end
		end

		if maplist == nil then
			puts "#{entry}: warning - sv_maplist not specified"
		else
			maplist.each { |map|
				if $*.include?('-all') then
					if !maps1stparty.include?(map+'.bsp') and !maps3rdparty.include?(map+'.bsp') then
						puts "#{entry}: sv_maplist error - #{map}.bsp is not present in data1/maps or arena/maps"
					end
				else
					if !maps1stparty.include?(map+'.bsp') then
						puts "#{entry}: sv_maplist error - #{map}.bsp is not present in data1/maps"
					end
				end
			}
		end

		cfgFile.close
	end
end



