######################################################################
CF_VERSION=$(shell . ../IDMETA; echo $$VERSION)
NAME=$(shell perl -ne '/<em:id>(.*)@/ && print "$$1"' install.rdf.in)
XPINAME=$(NAME)-$(CF_VERSION).xpi
BUILDID=$(shell git show --pretty=format:%H|head -1)
XULRUNNER=xulrunner


######################################################################
# the download sub directory. Maybe empty for the
# root directory
CF_HOMEPAGE=cf.indexdata.com
CF_DOWNLOAD_SUBDIR=/download
CF_USERNAME=cfrepo
CF_PASSWORD=gravenstein
CFREPO_HOMEPAGE=cfrepo.indexdata.com
CFREPO_DOWNLOAD_DIR=/var/cache/cf-repo/download

CF_DOWNLOAD_PATH=https://${CF_USERNAME}:${CF_PASSWORD}@${CF_HOMEPAGE}${CF_DOWNLOAD_SUBDIR}

# link to download the extension
CF_UPDATE_LINK=${CF_DOWNLOAD_PATH}/latest.xpi

# link to download update information
CF_UPDATE_URL=${CF_DOWNLOAD_PATH}/update.rdf

# dynamic update check per repo customer
#CF_UPDATE_URL=https://cfbuilder:hoshigarei@cfrepo-test2.indexdata.com/builder/builder-update-rdf.pl

######################################################################
# development
BUILDER_DEVEL_PROFILE=cf-devel

######################################################################

all: install.rdf update.rdf defaults

#XULAPP TARGETS

appdebug: appdist
	$(XULRUNNER) ./dist/application.ini -console -jsconsole

apprun: appdist
	$(XULRUNNER) ./dist/application.ini

appinstall: appdist
	$(XULRUNNER) --install-app ./dist/application.ini

appdist: dist application-ini.xsl
	xsltproc --stringparam buildId $(BUILDID) application-ini.xsl install.rdf > ./dist/application.ini
	cp -r ./defaults.in ./dist/defaults

#EXTENSION TARGETS

release: dist update.rdf builder-upload-cfrepo

builder-upload-cfrepo: $(XPINAME) news.xhtml
	ssh ${CFREPO_HOMEPAGE} "cd ${CFREPO_DOWNLOAD_DIR}"
	scp $(XPINAME)    ${CFREPO_HOMEPAGE}:${CFREPO_DOWNLOAD_DIR}
	ssh ${CFREPO_HOMEPAGE} "cd ${CFREPO_DOWNLOAD_DIR} && echo ${CF_VERSION} > latest.txt"
	scp -p news.xhtml ${CFREPO_HOMEPAGE}:${CFREPO_DOWNLOAD_DIR}

news.xhtml: ../NEWS
	echo "<html xmlns=\"http://www.w3.org/1999/xhtml\"><body><ul><li><b>CHANGES</b>" > news.xhtml
	sed 's/---/\<\/li\>\<li\>/g' < ../NEWS >> news.xhtml
	echo "</li></ul></body></html>" >> news.xhtml
	chmod 664 news.xhtml

dist: all $(XPINAME) news.xhtml

$(XPINAME): bundle
	cd ./dist/ && zip -rXq ../$(XPINAME) ./

bundle: install.rdf chrome/* chrome/*/* components/* modules/* templates/* scratch/* LICENCE defaults.in/preferences/defaults.js
	rm -rf ./dist
	mkdir -p ./dist
	cp -r ./chrome ./components ./modules ./templates ./scratch ./LICENCE chrome.manifest install.rdf ./dist
	mkdir -p ./dist/defaults/preferences/
	cp defaults/preferences/defaults.js ./dist/defaults/preferences/

defaults: defaults/preferences/defaults.js
defaults/preferences/defaults.js: defaults.in/preferences/* ../IDMETA ../.git
	mkdir -p defaults/preferences
	cp defaults.in/preferences/* defaults/preferences

install.rdf: install.rdf.in ../IDMETA ../.git
	sed s/@build_id@/$(BUILDID)/g < install.rdf.in | \
		sed s/@cf_version@/$(CF_VERSION)/g | \
		sed s,@cf_updateURL@,$(CF_UPDATE_URL),g > install.rdf

update.rdf: update.rdf.in ../IDMETA ../.git
	sed s/@build_id@/$(BUILDID)/g < update.rdf.in | \
  sed s/@cf_version@/$(CF_VERSION)/g | \
	sed s,@cf_updateURL@,$(CF_UPDATE_URL),g | \
	sed s,@cf_downloadPath@,$(CF_DOWNLOAD_PATH),g | \
	sed s,@cf_updateLink@,$(CF_UPDATE_LINK),g > update.rdf

builder-devel-macos:
	@PWD=$(pwd); \
	  cd "$$HOME/Library/Application Support/Firefox/Profiles"; \
	  test 1 == `ls -d *${BUILDER_DEVEL_PROFILE} | wc -l`; \
	  cd *${BUILDER_DEVEL_PROFILE}; \
	  mkdir -p extensions; \
	  cd extensions; \
	  echo ${PWD} > cfbuilder@indexdata.com; \
	  echo "Installed Add-ons \"`pwd`/cfbuilder@indexdata.com\""

clean:
	rm -f install.rdf update.rdf
	rm -rf ./dist
	rm -f ./*.xpi
	rm -f news.xhtml
	rm -rf ./defaults

distclean: clean

help:
	@echo "make [ all | clean | distclean | dist | release ]"
	@echo "make [ builder-upload-cfrepo ]"
	@echo "make [ builder-devel-macos [ BUILDER_DEVEL_PROFILE=${BUILDER_DEVEL_PROFILE} ]]"

