#!/usr/bin/make -f

# This Makefile is used to build a debian/control file
# for a Debian Pure Blend.
#
# 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 ';')
BLEND := $(shell /usr/share/blends-dev/blend-get-names blendname)
GENCONTROL := /usr/share/blends-dev/blend-gen-control
GENCONTROL_OPTS := 
TASKSELOPTS := $(shell grep TASKSELOPTS Makefile | cut -d '=' -f2)

# 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: $(BLEND)-tasks.desc debian/control

debian/control: debian/control.stub debian/changelog tasks/* $(CONFIGCONTROL)
	$(GENCONTROL) $(GENCONTROL_OPTS) -r $(TARGET_DIST) -S -c -m

tasksel: $(BLEND)-tasks.desc
$(BLEND)-tasks.desc: tasks/* debian/changelog
	$(GENCONTROL) $(TASKSELOPTS) -r $(TARGET_DIST) -S -t

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

avoidpackages.txt: tasks/* sources.list.$(TARGET_DIST)
	$(GENCONTROL) -r $(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
	rm -f debian/*-config.postinst debian/*-config.preinst debian/*-config.postrm debian/*-config.prerm

distclean: clean

proper: distclean
	rm -f debian/control
	rm -f $(BLEND)-tasks.desc

dist:
	rm -f $(BLEND)-tasks.desc debian/control
	make -f debian/rules get-orig-source
