# Copyright (c) 2007-2010 Nokia Corporation and/or its subsidiary(-ies).
# All rights reserved.
# This component and the accompanying materials are made available
# under the terms of the License "Eclipse Public License v1.0"
# which accompanies this distribution, and is available
# at the URL "http://www.eclipse.org/legal/epl-v10.html".
#
# Initial Contributors:
# Nokia Corporation - initial contribution.
#
# Contributors:
#
# Description:
# Header file exporting Function Like Makefile (FLM)
# The file destinations  relative to EPOCROOT
# 
#

## Parameters that are expected:
# BMCONV
# EPOCROOT
# HEADER
# OUTPUTPATH
# SOURCE
# BMTARGET
# TARGETPATH
# BINCOPYDIRS

ifneq ($(TARGETPATH),)
DATAPATH:=$(subst //,/,$(EPOCROOT)/epoc32/data/z/$(TARGETPATH))
else
DATAPATH:=$(subst //,/,$(EPOCROOT)/epoc32/data)
endif
CREATABLEPATHS:=$(DATAPATH) $(OUTPUTPATH)

BITMAPFILE:=$(subst //,/,$(DATAPATH)/$(BMTARGET))

# additional binary resource copies performed based on BINCOPYDIRS
MBMCOPYDIRS:=
MBMCOPYFILES:=
ifneq ($(BINCOPYDIRS),)
  MBMCOPYDIRS:=$(subst //,/,$(patsubst %,%/$(if $(TARGETPATH),/z/$(TARGETPATH),),$(BINCOPYDIRS)))
  CREATABLEPATHS:=$(CREATABLEPATHS) $(MBMCOPYDIRS)
  MBMCOPYFILES:=$(subst //,/,$(patsubst %, %/$(notdir $(BITMAPFILE)),$(MBMCOPYDIRS)))
endif


ifneq ($(HEADER),)
BITMAPHEADER:=$(subst //,/,$(EPOCROOT)/epoc32/include/$(basename $(BMTARGET)).mbg)
else
BITMAPHEADER:=
endif


# SOURCE is something like "DEPTH=2 FILE=/var/local/fred.bmp"
# Extract the file parts for the dependencies.
# Generate -2/var/local/fred.bmp for the command-line argument.
SOURCEDEPS:=$(subst //,/,$(subst FILE=,,$(filter FILE=%,$(SOURCE))))
SOURCEARGS:=$(subst //,/,$(subst DEPTH=,-,$(subst $(CHAR_SPACE)FILE=,,$(SOURCE))))

BMCONVCMDFILE:=$(OUTPUTPATH)/$(BMTARGET)_bmconvcommands
RELEASEABLES:=$(BITMAPHEADER) $(BITMAPFILE)
CLEANTARGETS:=$(BMCONVCMDFILE)

# The groupbmpin10 macro allows us to construct a command file, 10 
# bitmap objects at a time to avoid limits on argument lengths and 
# sizes on Windows.
define groupbmpin10
	$(if $1,$(shell echo -e $(foreach L,$(wordlist 1,10,$1),"$(L)\\n") >>$(BMCONVCMDFILE)),)
	$(if $1,$(call groupbmpin10,$(wordlist 11,$(words $1),$1)))
endef

GUARD:=$(call sanitise,TARGET_$(BITMAPFILE))
define generatebitmap
$(GUARD):=1

BITMAP:: $(RELEASEABLES)

ifneq ($(BITMAPHEADER),)
$(BITMAPHEADER): $(BITMAPFILE)
endif

# Create commandfile during parse so that the log is not littered with 
# these commands in parallel builds.
$(info <bmconvcmdfile>)
$(info $(BMCONVCMDFILE))
$(shell $(GNUMKDIR) -p "$(dir $(BMCONVCMDFILE))")
$(if $(BITMAPHEADER),$(shell echo -e "-h$(BITMAPHEADER)\n$(BITMAPFILE)" >$(BMCONVCMDFILE)),$(shell echo "$(BITMAPFILE)" >$(BMCONVCMDFILE)) )
$(call groupbmpin10, $(SOURCEARGS))
$(info </bmconvcmdfile>)

$(BITMAPFILE): $(SOURCEDEPS) $(BMCONV) 
	$(call startrule,bitmapcompile,FORCESUCCESS) \
	$(BMCONV) $(BMCONVCMDFILE) \
	$(call endrule,bitmapcompile)

endef


# Some builds require further copies of the generated mbm binaries
define copybitmap

BITMAP:: $(MBMCOPYFILES)

$(MBMCOPYFILES): $(BITMAPFILE) 
	$(call startrule,bitmapcopy,FORCESUCCESS) \
	$(GNUCP) $$< $$@ \
	$(call endrule,bitmapcopy)

endef


ifeq ($($(GUARD)),)
$(eval $(call generatebitmap))
endif

ifneq ($(MBMCOPYFILES),)
$(eval $(call copybitmap))
endif


## Clean up
$(call raptor_clean,$(CLEANTARGETS))
$(call makepath,$(CREATABLEPATHS))
$(call makepathfor,$(BITMAPHEADER))

# for the abld -what target
BMPRELEASEABLES:=$(RELEASEABLES) $(MBMCOPYFILES)
$(call raptor_release,$(BMPRELEASEABLES),BITMAP)

