#!/usr/bin/make -f

%:
	dh $@

override_dh_auto_build:
	set -x ; \
	for x in `find debian/debhelper.in -maxdepth 1 -type f`; do \
	  y=debian/`basename $$x`; \
	  cp $$x $$y; \
	  PACKAGE=$$(basename "$$x" | sed -e 's/[.].*//'); \
	  if [ -n "$$PACKAGE" ]; then \
	    ucf_file_list="$$(find config/$$PACKAGE/etc -type f -printf "/etc/%P\n" | sort | tr "\n" " ")"; \
	  fi; \
	  sed -e "s|#PACKAGE#|$$PACKAGE|g" -i $$y; \
	  sed -e "/LIB_POSTINST/r debian/scripts.in/lib_postinst.sh" -i $$y; \
	  sed -e "/LIB_POSTRM/r debian/scripts.in/lib_postrm.sh" -i $$y; \
	  sed -e "s|#UCF_FILE_LIST#|$$ucf_file_list|" -i $$y; \
	  case $$y in \
	    *.install) \
	      sed -e "s/^#.*//" -i $$y ; \
	      ;; \
	  esac; \
	done

override_dh_auto_clean:
	dh_auto_clean
	$(RM) -v $$(find debian/debhelper.in -type f -printf "debian/%P\n")

override_dh_fixperms:
	dh_fixperms
	chmod 700 debian/vdanjean-setup-dj/usr/share/vdanjean-setup-dj/ucf/etc/sssd/sssd.conf
	chmod 700 debian/vdanjean-setup-dj/usr/share/vdanjean-setup-dj/ucf/etc/sssd/conf.d/danjean.conf
	chmod 540 debian/vdanjean-setup-dj/usr/share/vdanjean-setup-dj/ucf/etc/sudoers.d/dj

override_dh_installsystemd:
	dh_installsystemd --name cpumode

update-control: debian/control.in ../admin-docs/packages.txt
	cd ../admin-docs && ( test "$$NO_PULL" = 1 || git pull )
	CHU_PACKAGES_LIST="$$(cat ../admin-docs/packages.txt | \
		egrep -v '^[[:space:]]*(#.*)?$$' | \
		sort -u | \
		sed -e 's/^ */	/' -e 's/$$/,@/' | \
		tr -d "\n" | \
		sed -e 's/@$$//' )" ; \
	sed -e 's/ *@CHU_PACKAGES_LIST@/'"$$CHU_PACKAGES_LIST"'/g' \
		-e 's/,@/,\n/g' \
		< $< \
		> debian/control.new
	if diff -u debian/control debian/control.new | grep '^-[^-]' | grep -sq -v '@' ; then \
		diff -u debian/control debian/control.new ; \
		echo "**** debian/control has been manually modified" ; \
		echo "**** Aborting instead of 'mv -v debian/control.new debian/control'" ; \
		exit 1 ; \
	fi
	mv -v debian/control.new debian/control
	@echo "**** debian/control is up-to-date"
