
SHELL = /bin/sh

# ------------------------------------------------
#   DEFINITIONS
# ------------------------------------------------

#   installation tools
SHTOOL          = ./shtool
INSTALL_SCRIPT  = $(SHTOOL) install -c -m 755
INSTALL_DATA    = $(SHTOOL) install -c -m 644
MKDIR           = $(SHTOOL) mkdir -p -f -m 755
VERSION_TOOL    = $(SHTOOL) version

prefix          = /usr/local
bindir          = $(prefix)/bin
mandir          = $(prefix)/man

# ------------------------------------------------
#   STANDARD TARGETS
# ------------------------------------------------

all: tarcust tarcust.1

tarcust: tarcust.pl version.pl
	set -e; path_perl=`$(SHTOOL) path -m perl5 perl`; \
	V=`$(VERSION_TOOL) -l perl -d long version.pl`; \
        sed -e "1s|@path_perl@|$${path_perl}|" \
            -e "s|@version@|$$V|" \
            -e '/__END__/,$$d' tarcust.pl > $@ && chmod a+x $@

tarcust.1: tarcust.pl version.pl
	set -e; V=`$(VERSION_TOOL) -l perl -d long version.pl`; \
    pod2man --section=1 \
            --center="A tar Customizer" \
            --release="tarcust $$V" tarcust.pl > $@

install: all
	$(MKDIR) $(bindir)
	$(MKDIR) $(mandir)/man1
	$(INSTALL_SCRIPT) tarcust $(bindir)/tarcust
	$(INSTALL_DATA) tarcust.1 $(mandir)/man1/tarcust.1

clean:
	-rm -f tarcust tarcust.1

distclean: clean
	@:

# ------------------------------------------------
#   THE CONFIGURATION SUPPORT
# ------------------------------------------------

shtool:
	@shtoolize echo version install fixperm mkdir path

# ------------------------------------------------
#   THE RELEASE STUFF
# ------------------------------------------------

TAR    = tar       # where to find GNU Tar
FIND   = find      # where to find a good Find tool
GZIP   = gzip      # where to find GNU Zip
NAME   = barbier   # name of maintainer who rolls the tarball

NEWVERS = \
    $(VERSION_TOOL) -l perl -n tarcust -p tarcust_ $$OPT version.pl; \
    V=`$(VERSION_TOOL) -l perl -d long version.pl`;\
    sed -e "s/version .*(.*)/version $$V/g" <README >README.n && mv README.n README

UPDATEVERS = \
    V=`$(VERSION_TOOL) -l perl -d short version.pl`; \
    $(VERSION_TOOL) -l perl -n tarcust -p tarcust_ -s $$V version.pl; \
    V=`$(VERSION_TOOL) -l perl -d long version.pl`; \
    sed -e "s/version .*(.*)/version $$V/g" <README >README.n && mv README.n README;

_GETDISTINFO = \
    _version=`$(VERSION_TOOL) -l perl -d short version.pl`; \
    _date=`date '+%Y%m%d_%H%M'`;

_BUILDDIST = \
    echo "Creating tarball..."; \
    awk '{print $$1}'  MANIFEST | xargs $(TAR) cvf - |\
    ./tarcust --user-name=$(NAME) \
              --group-name=tarcust \
              --prefix="$${_distname}:775" |\
    $(GZIP) --best - >$${_tarball}; \
    ls -l $${_tarball}; \
    echo "Done"

release: fixperm tarcust
	set -e; $(_GETDISTINFO) \
    _distname="tarcust-$${_version}"; \
    _tarball="$${_distname}.tar.gz"; \
    echo "Release Distribution: Tarcust Version $$_version"; \
    $(_BUILDDIST)

deb: release
	fakeroot ./debian/rules clean
	set -e; $(_GETDISTINFO) \
    mv tarcust-$${_version}.tar.gz ../tarcust_$${_version}.orig.tar.gz
	dpkg-buildpackage -i'.*CVS.*|.*\.cvsignore' -rfakeroot
	fakeroot ./debian/rules clean

snap: fixperm tarcust
	set -e; @$(_GETDISTINFO) \
    _distname="tarcust-$${_version}-SNAP"; \
    _tarball="$${_distname}.tar.gz"; \
    echo "Snap of whole source tree: Tarcust Version $$_version as of $$_date"; \
    $(_BUILDDIST)

new-version:
	@V="$(VERSION)"; \
    if [ ".$$V" != . ]; then \
        OPT="-s$$V"; \
    else \
        OPT="-e"; \
    fi; \
    $(NEWVERS)

update-version:
	$(UPDATEVERS)

fixperm:
	$(SHTOOL) fixperm *

##EOF##
