#! /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/savedebz

builddir:
	mkdir builddir

builddir/savedebz: builddir
	cp savedebz 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/savedebz $(PREFIX)/usr/bin

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

Makefile:
	ln -s $(MKFILE) Makefile	

maemodeb: launchsh.Makefile builddir/debian
	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
	:  :savedebz (0.8) unstable; urgency=low
	:  :
	:  :  * Initial version
	:  : 
	:  : -- Linux Eventually <oshazard@gmail.com>  Sun, 01 Aug 2010 23:21:02 -0700
	:  :
	EOF
	cut -d: -f3- > builddir/debian/control <<- EOF
        :  :Source: savedebz
        :  :Section: user/utilities
        :  :Priority: optional
        :  :Maintainer: Linux Eventually <oshazard@gmail.com>
        :  :Uploaders: Linux Eventually <oshazard@gmail.com> 
        :  :Build-Depends: debhelper (>= 4.0.0)
        :  :Standards-Version: 3.6.1
        :  :
        :  :Package: savedebz
        :  :Architecture: all
        :  :Provides: savedebz
        :  :Description: savedebz
        :  : A script to backup and restore .deb packages to/from SD card
	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/savedebz
	:  :
	:  :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`;
	chmod +x savedebz
	mv savedebz /usr/bin/

#binclean:
#	rm builddir/* || true

clean distclean:
	rm -rf builddir build-stamp config.guess config.sub
