#!/usr/bin/make -f

# This Makefile is used to build a debian/control file
# for a Custom Debian Distribution.
#
# Copyright (C) Andreas Tille <tille@debian.org>
# License: GPL

# TARGET_DIST is one of stable, sarge, etch, unstable, or any other available
# sources.list file available
TARGET_DIST := $(shell head -1 debian/changelog |awk '{print $$3}'|tr -d ';')
CDD := $(shell /usr/share/cdd-dev/cdd-get-names cddname)
GENCONTROL := /usr/share/cdd-dev/cdd-gen-control

# Verify whether config/control exists, if yes, add it to the depends of debian/control
CONFIGCONTROL := $(shell if [ -d config -a -e config/control ] ; then echo config/control; fi)

all: $(CDD)-tasks.desc debian/control

debian/control: debian/control.stub debian/changelog tasks/* $(CONFIGCONTROL)
	(cat debian/control.stub; \
	 test -f config/control && ( cat config/control; echo ) ; \
	$(GENCONTROL) -s $(TARGET_DIST) -D -c -m -i -A) > $@.new && mv $@.new $@

tasksel: $(CDD)-tasks.desc
$(CDD)-tasks.desc: tasks/* debian/changelog
	$(GENCONTROL) -s $(TARGET_DIST) -t -A > $(CDD)-tasks.desc.new && mv $(CDD)-tasks.desc.new $(CDD)-tasks.desc

packages.txt: tasks/*
	$(GENCONTROL) -s $(TARGET_DIST) -a > packages.txt.$$$$ && mv packages.txt.$$$$ packages.txt

avoidpackages.txt: tasks/* sources.list.$(TARGET_DIST)
	$(GENCONTROL) -s $(TARGET_DIST) -e > avoidpackages.txt.$$$$ && mv avoidpackages.txt.$$$$ avoidpackages.txt

by_vote:
	rm -f by_vote
	wget http://developer.skolelinux.no/popcon/by_vote

packages-sorted.txt: packages.txt by_vote
	for pkg in `cat packages.txt` ; do \
		grep " $$pkg " by_vote ; \
	done | LANG=C sort -r -n -k 4 -k 3 > packages-sorted.txt
usage: packages-sorted.txt

clean: 
	rm -rf tmp
	rm -f tasks/*~
	rm -rf tasksel
	rm -f packages.txt by_vote packages-sorted.txt
