#!/usr/bin/make -f

PACKAGE:=$(shell sed -nre 's/^Source: (.*)$$/\1/p' debian/control)
DEBVERSION:=$(shell head -n 1 debian/changelog \
		    | sed -e 's/^[^(]*(\([^)]*\)).*/\1/')
DEB_BASE_VERSION:=$(shell echo $(DEBVERSION) | sed -e 's/^.*://' -e 's/-[0-9~.a-z]*$$//')
UPVERSION:=$(shell echo $(DEB_BASE_VERSION) | sed -e 's/[~+]dfsg\(~\|$$\)/\1/' -e 's/~\(rc\|beta\)/-\1/')
SVN_REV:=$(shell echo $(UPVERSION) | sed -e 's/^.*svn\.//')
SVN_BRANCH:=branches/2.0
SVN_URL:=http://svn.asterisk.org/svn/asterisk-gui/$(SVN_BRANCH)
DIR_NAME:=$(PACKAGE)-$(UPVERSION)
TARBALL:=$(PACKAGE)_$(UPVERSION).orig.tar.gz

FILENAME := $(PACKAGE)_$(DEB_BASE_VERSION).orig.tar.gz
URL := http://downloads.digium.com/pub/telephony/$(PACKAGE)/releases/$(PACKAGE)-$(UPVERSION).tar.gz


-include /usr/share/cdbs/1/class/autotools.mk
-include /usr/share/cdbs/1/rules/debhelper.mk

# The makefile tries to guess this from the currently-installed files under
# /etc/asterisk . Even if we have asterisk installed, those files may be 
# non-readable to the current user.
#
# Furthermore, the makefile fails to separate the datadir from the /var/lib
# dir. In Debian the data resides under /usr/share/asterisk .
DEB_MAKE_INVOKE += \
  ASTETCDIR='$$(DESTDIR)'/etc/asterisk \
  ASTVARLIBDIR='$$(DESTDIR)'/usr/share/asterisk \
  ASTSBINDIR='$$(DESTDIR)'/usr/sbin \
  HTTPHOST=localhost \
  HTTPBINDPORT=8088 \
  HTTPPREFIX=asterisk \

build/asterisk-gui::
	mkdir -p debian/images-dec/setup debian/images-dec/images
	for file in debian/images/*/*.uu; do \
	  new=`echo $$file | sed -e 's/images/images-dec/' -e 's/\.uu$$//'`; \
	  uudecode -o $$new $$file ; \
	done

clean::
	rm -rf debiam/images-dec

install/asterisk-gui::
	chmod +x $(CURDIR)/debian/asterisk-gui/usr/share/asterisk/scripts/*
	#rm -f $(CURDIR)/debian/asterisk-gui/usr/share/asterisk/static-http/config/js/jquery.js

print-version:
	@echo "Debian version:      $(DEBVERSION)"
	@echo "Upstream version:    $(UPVERSION)"

TARBALL_DIR=../tarballs/$(PACKAGE)-$(UPVERSION).tmp
get-orig-source:
	@@dh_testdir
	@@[ -d ../tarballs/. ]||mkdir -p ../tarballs
	@@echo Downloading $(FILENAME) from $(URL) ...
	@@wget -nv -T10 -t3 -O ../tarballs/$(FILENAME) $(URL)

.PHONY: get-orig-source
