#################
# Configuration #
#################

NAME = 'PrBoom'
APPVERSION = /^#define VERSION "([^"]*)"$/.match(IO.read('MAC/config.h'))[1]

require 'MAC/macsupport'

DOOMWADDIR = File::expand_path('~/Library/Application Support/PrBoom')
@deutex = 'deutex'
@systemFrameworks += %w(Cocoa Carbon QuickTime OpenGL)
@frameworks += %w(SDL SDL_net)
@includes += ' -I. -IMAC'
@defines += ' -DHAVE_CONFIG_H'
@objcflags += ' -fobjc-exceptions'

if ENV['DEBUG']
	@commonflags += ' -g -DDEBUG -Wmissing-noreturn -Wredundant-decls -Wnested-externs -Wold-style-definition -Wshadow'
else
	@commonflags += ' -Os -Wno-pointer-sign -Wno-packed'
end

####################
# Bundle structure #
####################

file("#{CONTENTSDIR}/Info.plist" => ['MAC/Info.plist', CONTENTSDIR, 'MAC/Rakefile']) do |t|
	plist = []
	File.open(t.prerequisites[0], 'r') do |file|
		plist = file.readlines
	end
	File.open(t.name, 'w') do |f|
		for line in plist
			f.write(line.gsub(/%VERSION%/, APPVERSION))
		end
	end
end
task(:setup => "#{CONTENTSDIR}/Info.plist")
task(:bundle => :setup)

installFrameworks(:frameworks)
task(:bundle => :frameworks)

##############
# App binary #
##############

sources = %w(
SDL/i_main.c
SDL/i_network.c
SDL/i_joy.c
SDL/i_sound.c
SDL/i_sshot.c
SDL/i_system.c
SDL/i_video.c
MAC/i_system.m
MAC/i_sound.c
MAC/SDLMain.m
am_map.c
d_client.c
d_deh.c
d_items.c
d_main.c
doomdef.c
doomstat.c
dstrings.c
f_finale.c
f_wipe.c
g_game.c
gl_main.c
gl_texture.c
hu_lib.c
hu_stuff.c
info.c
lprintf.c
m_argv.c
m_bbox.c
m_cheat.c
m_menu.c
m_misc.c
m_random.c
md5.c
mmus2mid.c
p_ceilng.c
p_checksum.c
p_doors.c
p_enemy.c
p_floor.c
p_genlin.c
p_inter.c
p_lights.c
p_map.c
p_maputl.c
p_mobj.c
p_plats.c
p_pspr.c
p_saveg.c
p_setup.c
p_sight.c
p_spec.c
p_switch.c
p_telept.c
p_tick.c
p_user.c
r_bsp.c
r_data.c
r_demo.c
r_draw.c
r_filter.c
r_fps.c
r_main.c
r_patch.c
r_plane.c
r_segs.c
r_sky.c
r_things.c
s_sound.c
sounds.c
st_lib.c
st_stuff.c
tables.c
v_video.c
version.c
w_wad.c
w_mmap.c
wi_stuff.c
z_bmalloc.c
z_zone.c
)

launcherSources = %w(
MAC/ANSIString.m
MAC/ConsoleController.m
MAC/DrawerButton.m
MAC/FileButtonController.m
MAC/LauncherApp.m
MAC/LauncherMain.m
MAC/ResolutionDataSource.m
MAC/UKKQueue.m
MAC/UKMainThreadProxy.m
MAC/WadViewController.m
)

serverSources = %w(
POSIX/i_system.c
MAC/SDLMain.m
SDL/i_network.c
d_server.c
)

buildBinary(:binary, BINDIR, 'PrBoom', sources)
buildBinary(:binary, BINDIR, 'Launcher', launcherSources)
buildBinary(:binary, BINDIR, 'Server', serverSources)
task(:bundle => :binary)

############
# App data #
############

directory(ENGLISHDIR = "#{RESOURCEDIR}/English.lproj")
directory(MAINMENUDIR = "#{ENGLISHDIR}/MainMenu.nib")
installTask(:data, MAINMENUDIR, %w(MAC/English.lproj/MainMenu.nib/classes.nib MAC/English.lproj/MainMenu.nib/info.nib MAC/English.lproj/MainMenu.nib/keyedobjects.nib))
bundleDir(ENGLISHDIR)
bundleDir(MAINMENUDIR)

wadfiles = [Dir::glob('../data/graphics/*.ppm'),
            Dir::glob('../data/sprites/*.ppm'),
            Dir::glob('../data/lumps/*.lmp'),
            Dir::glob('../data/sounds/*.wav')].flatten

if wadfiles.empty?
	# Special case for release tarballs that only contain premade wad
	file("#{RESOURCEDIR}/prboom.wad" => [RESOURCEDIR, '../data/prboom.wad']) do |task|
		cp(task.prerequisites[1], task.name)
	end
else
	# Make the wad
	file("#{RESOURCEDIR}/prboom.wad" => [RESOURCEDIR, '../data/prboom.txt', *wadfiles]) do |task|
		sh("cd ../data && DOOMWADDIR='#{DOOMWADDIR}' #{@deutex} -make prboom.txt ../src/#{task.name}")
	end

end

task(:data => "#{RESOURCEDIR}/prboom.wad")
installTask(:data, RESOURCEDIR, %w(MAC/PrBoom.icns MAC/Launcher.icns MAC/PrBoom.sdef))
task(:bundle => :data)


#######################
# Disk Image Creation #
#######################

task(:image => :bundle) do |t|
	imageName = "#{NAME}-#{APPVERSION}"
	imagePath = "/Volumes/#{imageName}"

	if File::directory?(imagePath)
		sh("diskutil eject #{imagePath}")
	end

	sh("hdiutil create -fs HFS+ -volname #{imageName} -ov -type SPARSE -attach #{imageName}")
	cp_r([BUNDLEDIR, '../AUTHORS', '../COPYING', '../NEWS', '../README', '../doc'], imagePath)
	cp('MAC/Image-DS_Store', "#{imagePath}/.DS_Store")
	sh("diskutil eject #{imagePath}")

	rm_f("#{imageName}.dmg")
	sh("hdiutil convert #{imageName}.sparseimage -format UDZO -o #{imageName}")
	rm("#{imageName}.sparseimage")
end

################
# Main targets #
################

task(:default => :bundle)
