# ---------------------------------------------------------------------
# Author: Bill Erickson <erickson@esilibrary.com>
#
# Makefile to install prerequisites for OpenSRF and Evergreen
#
# Currently supports Debian (etch/lenny), Ubuntu (gutsy/hardy/intrepid), and Gentoo.
# Working towards support of CentOS 5 / RHEL 5.
# Installs Perl prereqs, libjs with Perl wrapper, libdbi, libdbi-drivers, and libyaz
#
# usage:
# 	make -f Makefile.install debian-etch
# 	- or -
# 	make -f Makefile.install debian-lenny
# 	- or -
# 	make -f Makefile.install ubuntu-gutsy
# 	- or -
# 	make -f Makefile.install ubuntu-hardy
# 	- or -
# 	make -f Makefile.install ubuntu-intrepid
# 	- or -
# 	make -f Makefile.install centos
# 	- or -
# 	make -f Makefile.install rhel
# 	- or -
# 	make -f Makefile.install gentoo
#
# Notes:
#
# 	This makefile has been tested much more with Debian and Ubuntu than
# 	CentOS, Gentoo, or RHEL.
#
# 	Gentoo (especially amd64) requires a good bit of masked package
# 	mangling for some packages.  These are not documented here because
# 	they will continue to evolve
#
# ---------------------------------------------------------------------
 
# Make any assumptions about the shell being used explicit
SHELL=/bin/bash 

# XXX
# Gentoo needs explicit versions on many of these packages
# to simulate a "blessed" set of packages
#
# Also, I (think) Gentoo has a javascript::spidermonkey package that does
# not require fetching the sources externally ... needs testing/updating in here
#
# Intrepid has libmozjs-dev and spidermonkey-bin - might work

LIBJS=js-1.7.0
LIBJS_PERL=JavaScript-SpiderMonkey-0.19
LIBJS_URL=ftp://ftp.mozilla.org/pub/mozilla.org/js/$(LIBJS).tar.gz
LIBJS_PERL_URL=ftp://mirror.datapipe.net/pub/CPAN/authors/id/T/TB/TBUSCH/$(LIBJS_PERL).tar.gz
# used for installing libjs lib and header files
JS_INSTALL_PREFIX=/usr/


# The libdbi sources can be fetched from sourceforge.net.  They are stored on
# the open-ils.org site now for ease of direct linking
LIBDBI=libdbi-0.8.3
LIBDBI_DRIVERS=libdbi-drivers-0.8.3
LIBDBI_HOST=http://open-ils.org/~denials/evergreen

# We need a recent version of Yaz
# Debian Lenny and Ubuntu 8.10 come with 3.0.34
YAZ=yaz-3.0.47
YAZ_HOST=http://ftp.indexdata.dk/pub/yaz

# Business::OnlinePayment is not packaged on CentOS/RHEL
PAY_ONLINE=Business-OnlinePayment-3.00_08.tar.gz
PAY_ONLINE_HOST=http://ftp.perl.org/pub/CPAN/authors/id/I/IV/IVAN/

APT_TOOL=aptitude

# Debian dependencies
DEBS =  \
        apache2-prefork-dev\
	aspell\
	aspell-en\
	libbusiness-creditcard-perl\
	libbusiness-onlinepayment-authorizenet-perl\
	libbusiness-onlinepayment-perl\
	libclass-dbi-pg-perl\
	libdatetime-format-builder-perl\
	libdatetime-format-iso8601-perl\
	libdatetime-format-mail-perl\
	libdatetime-perl\
	libdatetime-timezone-perl\
	libemail-send-perl\
	libgd-graph3d-perl\
	liblog-log4perl-perl\
	libmarc-record-perl\
	libncurses5-dev\
	libnet-server-perl\
	libole-storage-lite-perl\
	libpq-dev\
	libreadline5-dev\
	libspreadsheet-writeexcel-perl\
	libtext-aspell-perl\
	libtext-csv-perl\
	libtext-csv-perl\
	libuniversal-require-perl\
	libunix-syslog-perl\
	libuuid-perl

# Debian Lenny and Ubuntu Intrepid bundle recent versions of yaz
EXTRA_DEBS = \
	libmarc-charset-perl \
	libmarc-xml-perl \
	libnet-z3950-zoom-perl \
	libyaz-dev \
	yaz

CENTOS = \
	aspell \
	aspell-devel \
	aspell-en \
	gd-devel \
	perl-DBD-Pg

PGSQL_CENTOS = \
	postgresql \
	postgresql-contrib \
	postgresql-devel \
	postgresql-pl \
	postgresql-server

# Avoids a weak reference error that otherwise sinks DateTime:Format:ISO8601
CENTOS_PERL_FORCE = \
	Scalar::Util

# Some of these packages have stupid bugs in their test suites
# that are simply too painful to workaround
CENTOS_PERL_NOTEST = \
	Business::OnlinePayment::AuthorizeNet \
	DateTime::Format::Strptime

# This is a developer's version, but B:OP:AuthorizeNet depends on it
CENTOS_PERL_VERSION = \
	IVAN/Business-OnlinePayment-3.00_08.tar.gz

CENTOS_PERL = \
	Business::CreditCard \
	Class::DBI::Pg \
	DateTime \
	DateTime::TimeZone \
	DateTime::Format::Builder \
	GD::Graph3d \
	MARC::Record \
	Net::Z3950::ZOOM \
	Spreadsheet::WriteExcel \
	Text::Aspell \
	Text::CSV

PGSQL_CLIENT_DEBS_8.1 = \
	postgresql-client-8.1

PGSQL_SERVER_DEBS_8.1 = \
	postgresql-8.1 \
	postgresql-contrib-8.1 \
	postgresql-plperl-8.1 \
	postgresql-server-dev-8.1

PGSQL_CLIENT_DEBS_82 = \
	postgresql-client

PGSQL_SERVER_DEBS_82 = \
	postgresql \
	postgresql-contrib-8.2 \
	postgresql-plperl-8.2 \
	postgresql-server-dev-8.2

PGSQL_CLIENT_DEBS_83 = \
	postgresql-client

PGSQL_SERVER_DEBS_83 = \
	postgresql \
	postgresql-contrib-8.3 \
	postgresql-plperl-8.3 \
	postgresql-server-dev-8.3

GENTOOS = \
    yaz\
    aspell-en\
    dev-db/libpq\
    dev-db/postgresql\
    dev-perl/Email-Send\
    dev-perl/DateTime\
    dev-perl/DateTime-TimeZone\
    dev-perl/DBD-Pg\
    dev-perl/GD-Graph3d\
    dev-perl/Text-Aspell\
    dev-perl/Template-Toolkit\
    dev-perl/Text-CSV_XS\
    dev-perl/Spreadsheet-WriteExcel

GENTOO_PERL = \
    MARC::Record \
    Net::Z3950::ZOOM \
    Text::CSV

DEB_APACHE_MODS = \
    expires\
    include\
    proxy\
    proxy_http\
    rewrite

DEB_APACHE_DISMODS = \
    deflate

# Chronically unpackaged CPAN modules
CPAN_MODULES = \
    Business::CreditCard::Object \
    Net::Z3950::Simple2ZOOM \
    SRU

# Intrepid and Lenny have libmarc-charset-perl, libmarc-xml-perl, libnet-z3950-zoom-perl
CPAN_MODULES_MARC = \
    MARC::Charset \
    MARC::File::XML \
    Net::Z3950::ZOOM

# ----------------------------------------------------------------------------

all: 
	@echo "please specify an OS" && exit 0

# these should be the same for any distro
install: install_cpan install_js_sm install_libdbi 

centos: install_centos_pgsql install_centos_rpms install_yaz install_cpan_marc install install_centos_perl create_ld_local

debian-etch: etch generic_debian
debian-lenny: lenny generic_debian
etch: install_pgsql_client_debs_81 install_yaz install_cpan_marc
lenny: install_pgsql_client_debs_83 install_extra_debs
generic_debian:  install_debs install debian_sys_config

gentoo: install_gentoos install_gentoo_perl install

rhel: centos

ubuntu-gutsy: hardy generic_ubuntu 
ubuntu-hardy: hardy generic_ubuntu
ubuntu-intrepid: intrepid generic_ubuntu
hardy: install_pgsql_client_debs_82 install_yaz install_cpan_marc
intrepid: install_pgsql_client_debs_83 install_extra_debs
generic_ubuntu: install_debs install debian_sys_config

# - COMMON TARGETS ---------------------------------------------------------

# Install the CPAN modules
install_cpan: 
	for m in $(CPAN_MODULES); do perl -MCPAN -e "install \"$$m\";"; done

# Install the CPAN modules for MARC functionality
install_cpan_marc: 
	for m in $(CPAN_MODULES_MARC); do perl -MCPAN -e "install \"$$m\";"; done

# Install a known working version of YAZ
install_yaz:    
	if [ ! -d $(YAZ) ]; then wget $(YAZ_HOST)/$(YAZ).tar.gz; fi;
	tar xzf $(YAZ).tar.gz
	cd $(YAZ) && ./configure && make && make install

# Install the custom spidermonkey libs and JavaScript-SpiderMonkey Perl modules
install_js_sm: 
	if [ ! -f $(LIBJS).tar.gz ]; then wget $(LIBJS_URL); fi;
	if [ ! -f $(LIBJS_PERL).tar.gz ]; then wget $(LIBJS_PERL_URL); fi;
	tar -zxf $(LIBJS).tar.gz
	tar -zxf $(LIBJS_PERL).tar.gz
	cd js/src/ && make -f Makefile.ref
	mkdir -p $(JS_INSTALL_PREFIX)/include/js/
	cp js/src/*.h $(JS_INSTALL_PREFIX)/include/js/
	cp js/src/*.tbl $(JS_INSTALL_PREFIX)/include/js/
	cp js/src/Linux_All_DBG.OBJ/*.so $(JS_INSTALL_PREFIX)/lib/
	cp js/src/Linux_All_DBG.OBJ/*.a $(JS_INSTALL_PREFIX)/lib/
	cd $(LIBJS_PERL) && perl Makefile.PL -E4X && make && make test && make install


# Install libdbi and the postgres drivers
install_libdbi:
	if [ ! -d $(LIBDBI) ]; then wget $(LIBDBI_HOST)/$(LIBDBI).tar.gz; fi;
	if [ ! -d $(LIBDBI_DRIVERS) ]; then wget $(LIBDBI_HOST)/$(LIBDBI_DRIVERS).tar.gz; fi;
	tar -zxf $(LIBDBI).tar.gz
	tar -zxf $(LIBDBI_DRIVERS).tar.gz
	cd $(LIBDBI) && ./configure --disable-docs && make all install
	cd $(LIBDBI_DRIVERS) && ./configure  \
		--disable-docs --with-pgsql --enable-libdbi && make all install  


clean:
	make -C $(LIBDBI) clean
	make -C $(LIBDBI_DRIVERS) clean
	make -C $(LIBJS_PERL) clean
	make -f Makefile.ref -C js/src/ clean


# ------------------------------------------------------------------
# - DEBIAN ---------------------------------------------------------

debian_sys_config: 
	# link the apache modules in
	for m in $(DEB_APACHE_MODS); do a2enmod $$m; done;
	# keep the bad apache modules away
	for m in $(DEB_APACHE_DISMODS); do a2dismod $$m; done;
	# refresh the dynamic library cache
	ldconfig

# Install the debian-specific dependencies
install_debs:
	$(APT_TOOL) install $(DEBS)

install_pgsql_client_debs_83:
	$(APT_TOOL) install $(PGSQL_CLIENT_DEBS_83)

install_pgsql_server_debs_83:
	$(APT_TOOL) install $(PGSQL_SERVER_DEBS_83)

install_pgsql_client_debs_82:
	$(APT_TOOL) install $(PGSQL_CLIENT_DEBS_82)

install_pgsql_server_debs_82:
	$(APT_TOOL) install $(PGSQL_SERVER_DEBS_82)

# Etch requires an explicit version to avoid PostgreSQL 7.4
install_pgsql_client_debs_81:
	$(APT_TOOL) install $(PGSQL_CLIENT_DEBS_81)

# Etch requires an explicit version to avoid PostgreSQL 7.4
install_pgsql_server_debs_81:
	$(APT_TOOL) install $(PGSQL_SERVER_DEBS_81)

# Install the debian-specific dependencies for more modern distros
install_extra_debs:
	$(APT_TOOL) install $(EXTRA_DEBS)


# ------------------------------------------------------------------
# - GENTOO ---------------------------------------------------------

install_gentoos:
	emerge -n $(GENTOOS)

install_gentoo_perl:
	for m in $(GENTOO_PERL); do perl -MCPAN -e "install \"$$m\";"; done

# ------------------------------------------------------------------
    

# CENTOS
install_centos_rpms:
	yum -y install $(CENTOS)

install_centos_pgsql:
	yum -y install $(PGSQL_CENTOS)

install_centos_perl:
	for m in $(CENTOS_PERL_FORCE); do perl -MCPAN -e "CPAN::Shell->force(qw#install $$m#);"; done
	for m in $(CENTOS_PERL); do perl -MCPAN -e "install \"$$m\";"; done
	for m in $(CENTOS_PERL_VERSION); do perl -MCPAN -e "CPAN::Shell->install \"$$m\";"; done
	for m in $(CENTOS_PERL_NOTEST); do perl -MCPAN -e "CPAN::Shell->notest('install', \"$$m\";"; done

# We need to add /usr/local/lib to the ldconfig list of directories on CentOS,
# if it is not already there
create_ld_local:
	if [ "$$(ldconfig -v 2> /dev/null | grep '^/usr/local/lib' | wc -l)" -eq 0 ]; then \
		echo '/usr/local/lib' >> /etc/ld.so.conf.d/local.conf; \
		ldconfig; \
	fi;

# vim:noet:sw=4:ts=4:
