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

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

require './MAC/macsupport'

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

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_sshot.c
SDL/i_sound.c
SDL/i_system.c
SDL/i_video.c
MAC/i_system.m
MAC/i_sound.m
MAC/SDLMain.m
PCSOUND/pcsound.c
PCSOUND/pcsound_sdl.c
TEXTSCREEN/txt_button.c
TEXTSCREEN/txt_checkbox.c
TEXTSCREEN/txt_desktop.c
TEXTSCREEN/txt_dropdown.c
TEXTSCREEN/txt_gui.c
TEXTSCREEN/txt_inputbox.c
TEXTSCREEN/txt_io.c
TEXTSCREEN/txt_label.c
TEXTSCREEN/txt_radiobutton.c
TEXTSCREEN/txt_scrollpane.c
TEXTSCREEN/txt_sdl.c
TEXTSCREEN/txt_separator.c
TEXTSCREEN/txt_spinctrl.c
TEXTSCREEN/txt_strut.c
TEXTSCREEN/txt_table.c
TEXTSCREEN/txt_widget.c
TEXTSCREEN/txt_window.c
TEXTSCREEN/txt_window_action.c
am_map.c
d_client.c
d_deh.c
d_items.c
d_main.c
doomdef.c
doomstat.c
dstrings.c
e6y.c
f_finale.c
f_wipe.c
g_game.c
g_overflow.c
gl_opengl.c
gl_detail.c
gl_drawinfo.c
gl_gamma.c
gl_main.c
gl_missingtexture.c
gl_texture.c
gl_vertex.c
gl_wipe.c
gl_hires.c
gl_clipper.c
gl_light.c
gl_hqresize.c
gl_preprocess.c
gl_shaders.c
gl_shadow.c
gl_sky.c
gl_soft2gl.c
gl_fbo.c
gl_map.c
hu_lib.c
hu_stuff.c
hu_tracers.c
i_capture.c
i_pcsound.c
i_smp.c
icon.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
memio.c
mus2mid.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_screenmultiply.c
r_segs.c
r_sky.c
r_things.c
s_advsound.c
s_sound.c
sc_man.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-Plus', sources)
buildBinary(:binary, BINDIR, 'Launcher', launcherSources)
buildBinary(:binary, BINDIR, 'Server-Plus', 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-plus.wad" => [RESOURCEDIR, '../data/prboom-plus.wad']) do |task|
		cp(task.prerequisites[1], task.name)
	end
else
	# Make the wad
	file("#{RESOURCEDIR}/prboom-plus.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-plus.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}")
	system('/usr/bin/rsync', '-a', 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)
