#! /usr/bin/make -f
VER=1.1

MKFILE=launchsh.Makefile

help:
	: enter "make compile" to compile binaries "(will appear in builddir/)"
	: enter "make install PREFIX=..." to install...
	: enter "make maemodeb" to make Maemo package "(in scratchbox)"

.PHONY:	ALWAYS

compile: builddir/launchsh

builddir:
	mkdir builddir

builddir/launchsh: builddir launchsh root
	cp root builddir && cp launchsh builddir

chkprefix: ALWAYS
	@[ x"$(PREFIX)" != x ] \
		|| { sed -n 's/^#prefix: \?//p;' $(MKFILE); false; }
	@[ -d "$(PREFIX)/usr/bin" ] || mkdir -m 755 -p "$(PREFIX)/usr/bin"

#prefix:
#prefix:  Can not install: PREFIX missing.
#prefix:
#prefix:  try for example: `make install PREFIX=/usr/local'
#prefix:

install: chkprefix compile
	cp builddir/launchsh $(PREFIX)/usr/bin

# [builddir/]debian/rules launches this.
maemo-install:
	rm -rf $(DESTDIR)
	mkdir -p $(DESTDIR)/usr/bin
	cp builddir/launchsh $(DESTDIR)/usr/bin/rootsh
	cp builddir/root $(DESTDIR)/usr/bin/root

Makefile:
	ln -s $(MKFILE) Makefile	

maemodeb: launchsh.Makefile builddir/debian
	cp launchsh.c builddir
	cp launchsh.Makefile builddir/Makefile
	cd builddir

builddir/debian: Makefile builddir ALWAYS
	sed -n '/^debianfiles:/,/^ *$$/ p' Makefile | tail -n +3 | sh -ve #-nv

debianfiles:
	exit 1 # this target is not to be run.
	:
	[ ! -h debian ] || rm debian
	rm -rf builddir/debian; mkdir builddir/debian
	echo 4 > builddir/debian/compat
	echo GPL > builddir/debian/copyright
	cut -d: -f3- > builddir/debian/changelog <<- EOF
	:  :rootsh (1.1) unstable; urgency=low
	:  :
	:  :  * PATH environment variable set also when shell launched
	:  : 
	:  : -- Tomi Ollila <too@iki.fi>  Sat, 15 Dec 2007 14:46:16 +0200
	:  :
	:  :rootsh (1.0) unstable; urgency=low
	:  :
	:  :  * Initial version
	:  : 
	:  : -- Tomi Ollila <too@iki.fi>  Sun, 02 Dec 2007 11:41:32 +0200
	:  :
	EOF
	cut -d: -f3- > builddir/debian/control <<- EOF
        :  :Source: rootsh
        :  :Section: user/accessories
        :  :Priority: optional
        :  :Maintainer: Tomi Ollila <too@iki.fi>
        :  :Uploaders: Tomi Ollila <too@iki.fi> 
        :  :Build-Depends: debhelper (>= 4.0.0)
        :  :Standards-Version: 3.6.1
        :  :
        :  :Package: rootsh
        :  :Architecture: any
        :  :Provides: rootsh
        :  :Description: rootsh
        :  : A tool to run given command (or shell) with root privileges.
	EOF
	cut -d: -f3- > builddir/debian/postinst <<- EOF
	:  :#!/bin/sh -e
	:  :
	:  :exit 0
	EOF
	cut -d: -f3- > builddir/debian/rules <<- 'EOF'
	:  :#!/usr/bin/make -f
	:  :
	:  :build: build-stamp
	:  :build-stamp:
	:  :	dh_testdir
	:  :	$(MAKE) compile
	:  :	touch build-stamp
	:  :
	:  :clean:
	:  :	dh_testdir
	:  :	dh_testroot
	:  :	-rm -f configure-stamp build-stamp 
	:  :	-$(MAKE) binclean
	:  :	-test -r /usr/share/misc/config.sub && \
	:  :	  cp -f /usr/share/misc/config.sub config.sub
	:  :	-test -r /usr/share/misc/config.guess && \
	:  :	  cp -f /usr/share/misc/config.guess config.guess
	:  :	dh_clean
	:  :
	:  :install: build
	:  :	dh_testdir
	:  :	dh_testroot
	:  :	$(MAKE) maemo-install DESTDIR=`pwd`/debian/rootsh
	:  :
	:  :binary-indep: build install
	:  :binary-arch: build install
	:  :	dh_testdir
	:  :	dh_testroot
	:  :	dh_link -a
	:  :	dh_strip -a
	:  :	dh_compress -a
	:  :	dh_fixperms -a
	:  :	dh_installdeb -a
	:  :	dh_shlibdeps -a
	:  :	dh_gencontrol -a
	:  :	dh_md5sums -a
	:  :	dh_builddeb -a
	:  :
	:  :binary: binary-indep binary-arch
	:  :.PHONY: build clean binary-indep binary-arch binary install 
	EOF

dist:	clean
	export VER=$(VER) ;\
	sed -n '/^dist.sh:/,/^ *$$/ p' Makefile | tail -n +3 | sh -ve #-nv

dist.sh:
	exit 1 # this target is not to be run.
	:
	pwd=`pwd`; thisdir=`basename $pwd`;
	#ver=$VER-r`head -1 SvnVersion`
	ver=$VER
	cd ..
	dl () { [ ! -h rootsh-$ver ] || rm -f rootsh-$ver; }
	trap dl 0
	ln -s rootsh rootsh-$ver
	tar --exclude .svn -zcvf rootsh-$ver.tar.gz \
		`sed "s/^/rootsh-$ver\//" rootsh/MANIFEST`
	mv rootsh-$ver.tar.gz rootsh


binclean:
	rm builddir/* || true

clean distclean:
	[ ! -h debian ] || rm debian
	rm -rf builddir build-stamp debian config.guess config.sub Makefile
