#!/usr/bin/make -f
# -*- makefile -*-
# debian/rules for op-panel.
# Based on example file by Joey Hess.

# Uncomment this to turn on verbose mode.
#export DH_VERBOSE=1

DEBVERSION:=$(shell head -n 1 debian/changelog \
                    | sed -e 's/^[^(]*(\([^)]*\)).*/\1/')
UPVERSION:=$(shell echo $(DEBVERSION) | sed -e 's/^.*://' -e 's/-[0-9.]*$$//' -e 's/~dfsg$$//')

FILENAME := op-panel_$(UPVERSION)~dfsg.orig.tar.gz
UPFILENAME := op-panel_$(UPVERSION).orig.tar.gz
URL := http://asternic.org/files/op_panel-$(UPVERSION).tar.gz

CFLAGS = -Wall -g

ifneq (,$(findstring noopt,$(DEB_BUILD_OPTIONS)))
	CFLAGS += -O0
else
	CFLAGS += -O2
endif
ifeq (,$(findstring nostrip,$(DEB_BUILD_OPTIONS)))
	INSTALL_PROGRAM += -s
endif

build: build-stamp

build-stamp:
	dh_testdir
	
	# Add here commands to compile the package.
	/usr/bin/perl ming-source/operator_panel.pl
	/usr/bin/perl ming-source/comunicator.pl
	if [ ! -r op_server ]; then mv op_server.pl op_server; fi
	/usr/bin/pod2man --section 8 op_server debian/op_server.8
	touch build-stamp

clean:
	dh_testdir
	dh_testroot
	rm -f build-stamp 
	
	# Add here commands to clean up after the build process.
	rm -f debian/op_server.8
	if [ -r op_server ]; then mv op_server op_server.pl; fi
	rm -f *.swf
	
	dh_clean 

install: build
	dh_testdir
	dh_testroot
	dh_clean -k 
	dh_installdirs

# Build architecture-dependent files here.
binary-arch: build install

# Build architecture-independent files here.
binary-indep: build install
	dh_testdir
	dh_testroot
	dh_installchangelogs CHANGES
	dh_installdocs
	dh_installexamples op_buttons.cfg op_server.cfg op_astdb.cfg op_style.cfg extensions.conf.sample
	dh_install
	dh_link
	dh_installinit
	dh_installman debian/op_server.8
	dh_compress
	dh_fixperms
	dh_installdeb
	dh_gencontrol
	dh_md5sums
	dh_builddeb

get-orig-source:
	@@dh_testdir
	@@[ -d ../tarballs/. ]||mkdir -p ../tarballs
	@@echo Downloading $(UPFILENAME) from $(URL) ...
	@@wget -nv -T10 -t3 -O ../tarballs/$(UPFILENAME) $(URL)
	@@echo Repacking as DFSG-free...
	@@mkdir -p ../tarballs/op_panel-$(UPVERSION).tmp/
	@@cd ../tarballs/op_panel-$(UPVERSION).tmp ; \
	tar xfz ../$(UPFILENAME)
	@@find ../tarballs/op_panel-$(UPVERSION).tmp -type f -name '*.swf'|xargs -r rm
	@@find ../tarballs/op_panel-$(UPVERSION).tmp -type f -name 'prototype.js'|xargs -r rm
	@@find ../tarballs/op_panel-$(UPVERSION).tmp -type f -name 'scriptaculous.js'|xargs -r rm
	@@cd ../tarballs/op_panel-$(UPVERSION).tmp ; \
	tar cfz ../$(FILENAME) *
	@@echo Cleaning up...
	@@$(RM) -rf ../tarballs/op_panel-$(UPVERSION).tmp/

binary: binary-indep binary-arch
.PHONY: build clean binary-indep binary-arch binary install patch unpatch
