# Copyright (C) 2002  Britton Leo Kerin, see copyright.

# Where to install soundgrab script and its man page.
EXE_DIR = /usr/local/bin
MAN_DIR = /usr/local/man/man1

# Locations in this distribution package of the odd things soundgrab
# depends on.
CPAN_MODS_PATH = odd_things/cpan_modules
HIRES_MOD = Time-HiRes-1.42
READLINE_GNU_MOD = Term-ReadLine-Gnu-1.13
RAWREC_PATH = odd_things/rawrec-0.9.98

# I guess sometime you want to use 'dmake' on 'nmake' with perl
# modules.  Depending on what 'perl -V:make' tells you.  Huh.
PERLS_MAKE = `perl -V:make | perl -p -e "s/make='(.*)';/\\$$1/"`
# Perl 5.8.0 and later include Time::HiRes in the distribution.
PERL_HAS_TIME_HIRES = `perl --version | perl -e 'while ( <> ) { if \
                       ( m/v((5\.8\.\d)+|(5\.10\.\d+))/ ) \
                       { print "true"; exit(0) } else { ; }} \
                       print "defined but false"'`

# Delete out of date targets whose production commands return nonzero.
.DELETE_ON_ERROR:

usage: 
	@echo 'All this makefile does is install things, and you need to give' 
	@echo 'it a specific target, see INSTALL.'

install: build_docs
	install -d $(EXE_DIR)
	install src/soundgrab $(EXE_DIR)
	install -d $(MAN_DIR)
	install --mode='u=rw,go=r' src/soundgrab.1 $(MAN_DIR)

build_docs:
	cd src ; pod2man soundgrab soundgrab.1

install_odd_things: install_cpan_modules install_rawrec

install_cpan_modules: install_hires install_readline_gnu

install_hires:
	if [ "$(PERL_HAS_TIME_HIRES)" != "true" ]; then \
	    cd $(CPAN_MODS_PATH) ; tar xzvf $(HIRES_MOD).tar.gz ; \
	                           cd $(HIRES_MOD) ; perl Makefile.PL ; \
		                                     $(PERLS_MAKE) ; \
	                                             $(PERLS_MAKE) test ; \
	                                    	     $(PERLS_MAKE) install ; \
	fi

install_readline_gnu:
	cd $(CPAN_MODS_PATH) ; tar xzvf $(READLINE_GNU_MOD).tar.gz
	cd $(CPAN_MODS_PATH)/$(READLINE_GNU_MOD) ; perl Makefile.PL ; \
	                                           $(PERLS_MAKE) ; \
	                                           $(PERLS_MAKE) test ; \
	                                           $(PERLS_MAKE) install

install_rawrec:
	$(MAKE) -C $(RAWREC_PATH)/src install

# This target leaves the tarred gzipped perl modules alone.
totally_clean:
	find . -name "*~" -exec rm -f \{\} \;
	rm -rf src/soundgrab.1 src/TAGS
	cd $(CPAN_MODS_PATH) ; rm -rf $(HIRES_MOD) $(READLINE_GNU_MOD)
	$(MAKE) -C $(RAWREC_PATH)/src distclean

# Doesn't uninstall anything from the odd_things directory.
uninstall: 
	@echo "Note: things from the odd_things directory don't actually get"
	@echo "uninstalled by this target, since they may have come from"
	@echo "somewhere else."
	rm -f $(EXE_DIR)/soundgrab
	rm -r $(MAN_DIR)/soundgrab.1

# Non-file targets.
.PHONY: info install install_odd_things install_cpan_modules install_hires \
                install_readline_gnu install_rawrec uninstall
