#!/usr/bin/make -f
#export DH_VERBOSE = 1
export OEMLIB  	  = ./lib
SHELL   := bash
PWD     := $(shell pwd)

PACKAGE := $(shell dpkg-parsechangelog | sed -n -e 's/^Source: //p')
VERSION := $(shell dpkg-parsechangelog | sed -n -e 's/^Version: //p')

PROJECT := $(shell awk -F= '($$1 == "PROJECT") { print $$2 }' <debian/$(PACKAGE).env)
SERIES  := $(shell awk -F= '($$1 == "SERIES") { print $$2 }' <debian/$(PACKAGE).env)


VOLATILEDIR 	:= debian/${PACKAGE}/usr/share/volatile
UBUNTUDIR       := debian/${PACKAGE}/usr/share/ubuntu

#DIR=$(shell lib/create-tasks -x -p "${PROJECT}" -s "${SERIES}" -T . ${dir##*/} -o $(VOLATILEDIR));

%:
	dh $@ --with python2 --with systemd

override_dh_install:
	dh_install
	cp -fr templates debian/$(PACKAGE)/
	# copy libraries
	find lib -maxdepth 1 -type f -printf '%P\n' | xargs -I '{}' cp -fp lib/'{}' $(VOLATILEDIR)/
	for dir in $(shell find lib/* -maxdepth 1 -type d); do \
	    if [ "$(PROJECT)" = "" ]; then \
		find $$dir -maxdepth 1 -type f | xargs -I '{}' cp -fp '{}' $(VOLATILEDIR); \
	    elif [ "$(PROJECT)" = "$${dir##*/}" ]; then \
		find $$dir -maxdepth 1 -type f | xargs -I '{}' cp -fp '{}' $(VOLATILEDIR); \
	    fi \
	done
	for dir in $(shell find -maxdepth 1 -name "*task" -type d -printf '%P\n'); do \
	    if [ "$(PROJECT)" != "" ]; then \
		utils/create-tasks -p "$(PROJECT)" -s "$(SERIES)" -T . -o $(VOLATILEDIR) $$dir; \
		sed "s,##__$${dir}__##,/usr/share/volatile/$${dir},g" -i debian/$(PACKAGE)/templates/*.in; \
	    else \
		cp -frp $$dir $(VOLATILEDIR)/; \
		sed "s,##__$${dir}__##,\$$\(/usr/share/volatile/create-task $$dir\),g" -i debian/$(PACKAGE)/templates/*.in; \
	    fi; \
	done
	if [ "$(PROJECT)" = "" ]; then \
	    install -m755 utils/create-tasks $(VOLATILEDIR)/create-tasks; \
	    install -m755 utils/generate-dpkg-list.create-tasks $(VOLATILEDIR)/generate-dpkg-list; \
	else \
	    install -m755 utils/generate-dpkg-list.simple $(VOLATILEDIR)/generate-dpkg-list; \
	fi
	# install the systemd services
	install -m644 -D debian/$(PACKAGE)/templates/volatile-task-casper.service.in \
	    debian/volatile-task-casper.service
	install -m644 -D debian/$(PACKAGE)/templates/volatile-task.service.in \
	    debian/volatile-task.service
	install -m644 -D debian/$(PACKAGE)/templates/volatile-task-end.service.in \
	    debian/volatile-task-end.service
	# install the systemd helper scripts
	install -m755 -D debian/$(PACKAGE)/templates/volatile-task-casper.sh.in \
	    $(VOLATILEDIR)/bin/volatile-task-casper.sh
	install -m755 -D debian/$(PACKAGE)/templates/volatile-task.sh.in \
	    $(VOLATILEDIR)/bin/volatile-task.sh
	install -m755 -D debian/$(PACKAGE)/templates/volatile-task-end.sh.in \
	    $(VOLATILEDIR)/bin/volatile-task-end.sh
	# deploy oem-config hooks
	install -m755 -D debian/$(PACKAGE)/templates/oem-config-general.sh.in \
	    $(UBUNTUDIR)/scripts/oem-config.d/early/75_stella-oem-config-wrapper
	install -m755 -D debian/$(PACKAGE)/templates/oem-config-general.sh.in \
	    $(UBUNTUDIR)/scripts/oem-config.d/late/75_stella-oem-config-wrapper
	# deploy ubiquity success/failure hooks
	install -m755 -D debian/$(PACKAGE)/templates/post-success-general.sh.in \
	    $(UBUNTUDIR)/scripts/ubiquity.d/success/80_stella-post-success-general
	install -m755 -D debian/$(PACKAGE)/templates/post-fail-general.sh.in \
	    $(UBUNTUDIR)/scripts/ubiquity.d/failure/80_stella-post-fail-general
	# deploy oobe/ubiquity hooks
	install -m755 -D debian/$(PACKAGE)/templates/run-ubiquity-oobe-scripts.in \
	    $(UBUNTUDIR)/scripts/oem-config.d/custom/60_stella-run-scripts
	install -m755 -D debian/$(PACKAGE)/templates/run-ubiquity-install-scripts.in \
	    $(UBUNTUDIR)/scripts/ubiquity.d/custom/60_stella-run-scripts
	rm -fr debian/$(PACKAGE)/templates

override_dh_systemd_enable:
	dh_systemd_enable --name volatile-task-casper
	dh_systemd_enable --name volatile-task
	dh_systemd_enable --name volatile-task-end
