%% -=-=-=-
%% 20020913 - Mickael Remond
%%  Platform independant version of the compilation file for 
%%  Eddieware dns_server
%% -=-=-=-
include("vsn.emk").

SRCDIR=src.
EBINDIR=ebin.
BINDIR=bin.
INCDIR=inc.
PRIVDIR=priv.
PREFIX=/usr/local.

include("standard_rules.emk").

%% Erlang source files
EBINFILES=$(EBINDIR)/dns_app.beam,
          $(EBINDIR)/dns_cache.beam,
          $(EBINDIR)/dns_catalog.beam,
	  $(EBINDIR)/dns_domain_sup.beam,
          $(EBINDIR)/dns_load.beam,
          $(EBINDIR)/dns_parse.beam,
          $(EBINDIR)/dns_query.beam,
          $(EBINDIR)/dns_recurse.beam,
          $(EBINDIR)/dns_recurse_udp_tracker.beam,
          $(EBINDIR)/dns_rr.beam,
          $(EBINDIR)/dns_server.beam,
          $(EBINDIR)/dns_sig.beam,
          $(EBINDIR)/dns_sup.beam,
          $(EBINDIR)/dns_tcp_accept.beam,
          $(EBINDIR)/dns_udp.beam,
          $(EBINDIR)/dns_xfr.beam,
          $(EBINDIR)/dns_zone.beam,
	  $(EBINDOR)/config_file.erl.

%% OTP Application files
APPFILE=dns_server.app.
APPSRC=$(SRCDIR)/$(APPFILE).src.
APPTARGET=$(EBINDIR)/$(APPFILE).
RELSRC=$(SRCDIR)/dns.rel.src.
RELFILE=$(EBINDIR)/dns.
RELTARGET=$(RELFILE).rel.

CONFIGSRC=$(SRCDIR)/sys.config.src.
CONFIGTARGET=$(EBINDIR)/dns_sys.config.

STARTSRC=$(SRCDIR)/lbdns.src.
STARTTARGET=$(BINDIR)/lbdns.

%% -=-=-

all when $(EBINFILES), appfile, script, sysconfig, lbdns.

appfile when $(APPTARGET) ->
	ermake_lib:subst("$(APPSRC)", "$(APPTARGET)", [{"%VSN%", $(VSN)}]).

%% TODO: Make start script both on Windows and Unix/Linux
%%     Use: os:type to switch to the proper build of the script.
script ->
	ermake_lib:subst("$(RELSRC)", "$(RELTARGET)", []),
	systools:make_script("$(RELFILE)", [{path, ["$(EBINDIR)"]}]).

sysconfig ->
	ermake_lib:subst("$(CONFIGSRC)", "$(CONFIGTARGET)",
			[{"%MF_BYTES%", "512000"},
			 {"%MF_FILES%", "5"}]).

%% Start script:
%% TODO: Windows version
lbdns ->
	{ok, ROOTDIR} = file:get_cwd(),
	ermake_lib:subst("$(STARTSRC)", "$(STARTTARGET)",
			[{"%ROOTDIR%", ROOTDIR},
			 {"%DNS_BOOT%", filename:absname("$(EBINDIR)/dns")},
                         {"%BINDIR%", filename:absname("$(BINDIR)")},
			 {"%SYSCONF%", filename:absname("$(EBINDIR)/dns_sys")},
			 {"%ERL_ROOT%", code:root_dir()}]).
         %%TODO: chmod of start file

test ->
	%% TODO: Test build
	system:cmd("bin/start -b test/named.boot").
	%% TODO: Call test functions.

clean ->
	%% Not yet implemented in ermake_lib.
	%%ermake_lib:clean("$(APPTARGET)").
	io:format("Removing ~s ~n", ["$(EBINFILES)"]).
%% TODO Tar generation.
