#! /bin/sh
## debian/rules for SHOOP
## by Gergely Nagy <algernon@debian.org>
##
## Trapped in time
## A miracle of hope and change
## A swirling mass, no mercy now
## If the truth hurts prepare for pain
## ...Do you think we're forever ?
##
##	- Anathema: Eternity Part I

# exports
export SHOOPPATH=${PWD}:${PWD}/modules
export SHOOPMOD=${PWD}/modules
export DH_COMPAT=2

# #include <shoop>
. ${PWD}/shoop.sh

# basic setup
OBJECT . new DEBIAN

DEBIAN . MAKE_LEVEL =q 0
DEBIAN . dry_run =q no
DEBIAN . verbose =q yes
DEBIAN . keep_going =q no
DEBIAN . DPKG_BUILDPACKAGE =q ${DPKG_BUILDPACKAGE:-dpkg-buildpackage}
DEBIAN . INSTALL_OPTIONS =q ""

# core methods
DEBIAN . do_header : 'echo "==> [$($THIS . MAKE_LEVEL)] Making $@" 1>&2
	$THIS . MAKE_LEVEL =q $(expr $($THIS . MAKE_LEVEL) + 1)'
DEBIAN . do_done : '$THIS . MAKE_LEVEL =q $(expr $($THIS . MAKE_LEVEL) - 1)
	echo "<== [$($THIS . MAKE_LEVEL)] Finished $@" 1>&2'
DEBIAN . do : 'if test "x$($THIS . verbose)" != "xno"; then echo $@; fi
	if test "x$($THIS . dry_run)" = "xno"; then eval $@; fi'
DEBIAN . header : 'if test "x$($THIS . verbose)" != "xno"; then
		$THIS . do_header $@
	fi'
DEBIAN . done : 'if test "x$($THIS . verbose)" != "xno"; then
		$THIS . do_done $@
	fi'

# These do the Real Job
DEBIAN . do_build : 'test -f build-stamp || $THIS . build_stamp'
DEBIAN . do_build_stamp : '$THIS . do dh_testdir
	$THIS . do "make docs/modules.txt 2>/dev/null"
	$THIS . do touch build-stamp'
DEBIAN . do_clean : '$THIS . do dh_testdir
	$THIS . do dh_testroot
	$THIS . do rm -f build-stamp docs/modules.txt docs/modules.pod
# Back up the ChangeLog
	mv ChangeLog ChangeLog.backup
# Clean up!
	$THIS . do "make clean || true"
# Copy the ChangeLog back
	mv ChangeLog.backup ChangeLog
	$THIS . do dh_clean'
DEBIAN . do_install : '$THIS . build
	$THIS . do dh_testdir
	$THIS . do dh_testroot
	$THIS . do dh_clean -k
	$THIS . do dh_installdirs
	$THIS . do make install $($THIS . INSTALL_OPTIONS)
	$THIS . do rm -f debian/shoop/usr/share/doc/shoop/COPYING \
			 debian/shoop/usr/share/doc/shoop/COPYING.gz
	$THIS . do mv debian/shoop/usr/share/shoop/modules \
		      debian/shoop-modules/usr/share/shoop/modules
	$THIS . do mv debian/shoop/usr/share/doc/shoop/modules.txt \
		      debian/shoop-dev/usr/share/doc/shoop/
	$THIS . do mv debian/shoop/usr/share/doc/shoop/MODULES \
		      debian/shoop-dev/usr/share/doc/shoop/
	$THIS . do rm -rf debian/shoop/usr/share/doc/shoop/examples
	$THIS . do ln -sf shoop debian/shoop-dev/usr/share/doc/shoop-dev
	$THIS . do ln -sf shoop debian/shoop-modules/usr/share/doc/shoop-modules
# I have to use ugly IFS magic here, otherwise things would fail miserably
	SAVE_IFS="$IFS"
	GOOD_IFS=" 	
"
	IFS="$GOOD_IFS"
	for f in $(cat debian/shoop-dev.examples); do
		IFS="$SAVE_IFS"
		$THIS . do cp $f debian/shoop-dev/usr/share/doc/shoop/examples/$f
		IFS="$GOOD_IFS"
	done
	IFS="$SAVE_IFS"'
DEBIAN . do_packages : '$THIS . build
	$THIS . install
	$THIS . do dh_testdir
	$THIS . do dh_testroot
#	$THIS . do dh_installdebconf
	$THIS . do dh_installdocs
#	$THIS . do dh_installmenu
#	$THIS . do dh_installinfo
	$THIS . do dh_installchangelogs
# This is Evil.
	$THIS . do rm -f debian/shoop-dev/usr/share/doc/shoop/copyright
	$THIS . do rm -f debian/shoop-dev/usr/share/doc/shoop/changelog{,.gz}
	$THIS . do rm -f debian/shoop-modules/usr/share/doc/shoop/copyright
	$THIS . do rm -f debian/shoop-modules/usr/share/doc/shoop/changelog{,.gz}
# Evil ends here.
	$THIS . do dh_link
	$THIS . do dh_strip
	$THIS . do dh_compress
	$THIS . do dh_fixperms
	$THIS . do dh_installdeb
	$THIS . do dh_gencontrol
	$THIS . do dh_md5sums
	$THIS . do dh_builddeb'
DEBIAN . do_binary_arch : ''
DEBIAN . do_binary_indep : '$THIS . packages'
DEBIAN . do_binary : '$THIS . binary_arch
	$THIS . binary_indep'
DEBIAN . do_cvs_build : '# Oh boy, oh boy!
	$THIS . do make ChangeLog
	$THIS . do $($THIS . DPKG_BUILDPACKAGE)'

# Wrapper function that calls header & done too
DEBIAN . do_func : '$THIS . header $LASTMETH
	$THIS . do_$LASTMETH
	$THIS . done $LASTMETH'
# Use the wrapper for all targets
DEBIAN . build : DEBIAN . do_func
DEBIAN . build_stamp : DEBIAN . do_func
DEBIAN . clean : DEBIAN . do_func
DEBIAN . install : DEBIAN . do_func
DEBIAN . packages : DEBIAN . do_func
DEBIAN . binary_arch : DEBIAN . do_func
DEBIAN . binary_indep : DEBIAN . do_func
DEBIAN . binary : DEBIAN . do_func
DEBIAN . cvs_build : DEBIAN . do_func

# void main ( whatever )
DEBIAN . debian : '# Go Johny, go! Go Johny, go go go!
	while test $# -gt 0
	do
		opt=$1
		shift

		case $opt in
			build|build-stamp|clean|install|packages|binary-arch|binary-indep|binary|cvs-build)
				method=$(echo $opt | sed -e "s,\-,_,g")
				$THIS . TODO .=q $method
				;;
			DEB_BUILD_OPTIONS=*nostrip*)
				$THIS . INSTALL_OPTIONS .=q "strip_comment_space=/bin/cat"
				;;
			DEB_*=*)
				# These are useless for us
				;;
			-*b*|-*m*|-*d*|-*e*|-*i*|-*p*|-*q*|-*r*|-*t*|-*w*|-*C|-*f|-*I|-*o|-*W|-*j*|-*k*|-*s*|-*S-|-*n*|-*l*)
				# Ignored, handled later
				;;
			-*)
				echo "*** Unsupported option: $opt"
				exit 1
				;;
			*)
				echo "*** Unsupported target: $opt"
				exit 1
				;;
		esac
		case $opt in
			-*b*|-*m*|-*d*|-*e*|-*i*|-*p*|-*q*|-*r*|-*t*|-*w*)
				# Ignored for GNU Make compatibility
				;;
		esac
		case $opt in
			-*C|-*f|-*I|-*o|-*W|-*l)
				# Ignored (with arguments) for GNU Make compatibility
				shift
				;;
		esac
		case $opt in
			-*j*)
				# This is not implemented yet
				;;
		esac
		case $opt in
			-*k*)
				$THIS . keep_going =q yes
				;;
		esac
		case $opt in
			-*S*)
				$THIS . keep_going =q no
				;;
		esac
		case $opt in
			-*n*)
				$THIS . dry_run =q yes
				;;
		esac
		case $opt in
			-*s*)
				$THIS . verbose =q no
				;;
		esac
		case $opt in
			-*v*)
				$ THIS . version
				exit 0
				;;
		esac
	done
	$THIS . header debian
	if test "x$($THIS . keep_going)" = "xno"; then
		set -e
	fi
	# FIXME : $THIS . TODO empty => print warning
	for rule in $($THIS . TODO); do
		$THIS . $rule
	done
	$THIS . done debian'

# Run forest, run !
DEBIAN . debian ${@:-build}

exit 0
