# ---------------------------------------------------------------------
# Author: Bill Erickson <erickson@esilibrary.com>
# Author: Dan Scott <dscott@laurentian.ca>
#
# Makefile to install prerequisites for OpenSRF and Evergreen
#
# Currently supports Debian (lenny/squeeze), Ubuntu (hardy/lucid), and Fedora (13).
# 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-lenny
# 	- or -
# 	make -f Makefile.install debian-squeeze
# 	- or -
# 	make -f Makefile.install ubuntu-hardy
# 	- or -
# 	make -f Makefile.install ubuntu-lucid
# 	- or -
# 	make -f Makefile.install fedora-13
# 	- or -
# 	make -f Makefile.install centos
# 	- or -
# 	make -f Makefile.install rhel
#
# Notes:
#
# 	This makefile has been tested much more with Debian and Ubuntu than
# 	Fedora, CentOS, or RHEL.
#
# ---------------------------------------------------------------------
 
# Make any assumptions about the shell being used explicit
SHELL=/bin/bash 

LIBJS=js-1.7.0
LIBJS_PERL=JavaScript-SpiderMonkey-0.20
LIBJS_URL=http://ftp.mozilla.org/pub/mozilla.org/js/$(LIBJS).tar.gz
LIBJS_PERL_URL=http://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 -yq

# 64 or 32 bit os?
LBITS=$(shell getconf LONG_BIT)

# Fedora 64-bit?
FEDORA_64=$(shell uname -r | grep "fc[0-9][0-9].x86_64")

#RHEL/Centos PGSQL 
PGSQL_HOST=http://yum.pgsqlrpms.org/reporpms/8.4
PGSQL_CENTOS=pgdg-centos-8.4-2.noarch.rpm
PGSQL_REDHAT=pgdg-redhat-8.4-2.noarch.rpm	

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

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

EXTRA_DEBS_SQUEEZE = \
    libuuid-tiny-perl

# Ubuntu Hardy requires libencode-perl # to get a version of Encode
# > 2.12 - see bug 525069.
EXTRA_ENCODE = \
	libencode-perl

CENTOS = \
	aspell \
	aspell-devel \
	aspell-en \
	gd-devel \
	libssh2-devel \
	openssl-devel \
	perl-Business-CreditCard \
	perl-Business-ISBN \
	perl-Business-ISBN-Data \
	perl-Business-OnlinePayment \
	perl-Class-Data-Inheritable \
	perl-DateTime-Format-Builder \
	perl-DateTime-Format-Strptime \
	perl-Ima-DBI \
	perl-DBD-Pg \
	perl-GD-Graph3d \
	perl-IO-stringy \
	perl-Spreadsheet-WriteExcel \
	perl-Text-Aspell \
	perl-Text-CSV

CENTOS_PERL = \
	Encode \
	DBIx::ContextualFetch \
	Getopt::Long \
	Net::SSH2 \
	Net::XMPP \
	Net::Z3950::ZOOM

FEDORA_13_RPMS = \
	aspell \
	aspell-en \
	js-devel \
	libdbi \
	libdbi-dbd-pgsql \
	libdbi-devel \
	libssh2-devel \
	libyaz \
	libyaz-devel \
	mod_ssl \
	ncurses-devel \
	ncurses-libs \
	perl-parent \
	perl-Business-CreditCard \
	perl-Business-ISBN \
	perl-Business-ISBN-Data \
	perl-Email-Send \
	perl-GDGraph3d \
	perl-Net-SSH2 \
	perl-OLE-Storage_Lite \
	perl-Spreadsheet-WriteExcel \
	perl-Text-Aspell \
	perl-Text-CSV \
	perl-Text-CSV_XS \
	perl-XML-Writer \
	postgresql-devel \
	readline-devel \
	tcp_wrappers-devel \
	yaz

# Note: B:O:AuthorizeNet 3.21 fails with https://rt.cpan.org/Public/Bug/Display.html?id=55172
# Should be fixed in 3.22
# MARC::Record 2.0.1 is required but only 2.0.0 is packaged
FEDORA_13_CPAN = \
	Business::OnlinePayment \
	Business::OnlinePayment::AuthorizeNet \
	MARC::Record

PGSQL_84_RPMS = \
	postgresql-8.4* \
	postgresql-contrib-8.4* \
	postgresql-devel-8.4* \
	postgresql-plpe*-8.4* \
	postgresql-server-8.4*

PGSQL_CLIENT_DEBS_82 = \
	postgresql-client

PGSQL_CLIENT_DEBS_83 = \
	postgresql-client

PGSQL_CLIENT_DEBS_84 = \
	postgresql-client

PGSQL_SERVER_DEBS_84 = \
	postgresql \
	postgresql-contrib-8.4 \
	postgresql-plperl-8.4 \
	postgresql-server-dev-8.4

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

DEB_APACHE_DISMODS = \
    deflate

# Chronically unpackaged CPAN modules
CPAN_MODULES = \
    Business::EDI \
    Library::CallNumber::LC \
    Net::Z3950::Simple2ZOOM \
    SRU

# More chronically unpackaged CPAN modules (available in Squeeze though)
CPAN_MODULES_MORE = \
    Business::CreditCard::Object \
    MARC::Record \
    UUID::Tiny

# Are we sure most distros don't come with an acceptable version of Safe.pm?
CPAN_MODULES_SAFE = \
	Safe

# Recent Debian/Ubuntus have libmarc-charset-perl, libmarc-xml-perl, libnet-z3950-zoom-perl
CPAN_MODULES_MARC = \
	Business::ISBN \
	Business::ISBN::Data \
    MARC::Charset \
    MARC::File::XML \
    Net::Z3950::ZOOM

CPAN_MODULES_FORCE = \
	Class::DBI::Frozen::301

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

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

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

centos: install_centos_pgsql centos_like
rhel: install_redhat_pgsql centos_like
centos_like: install_centos_rpms install_yaz install_cpan_marc install install_centos_perl create_ld_local install_cpan_safe install_cpan_force

fedora-13: install_fedora_13_rpms install_cpan install_cpan_fedora install_cpan_marc install_spidermonkey install_cpan_force

debian-lenny: lenny generic_debian install_cpan_more install_cpan_safe
debian-squeeze: squeeze generic_debian
lenny: install_pgsql_client_debs_83 install_extra_debs
squeeze: install_pgsql_client_debs_84  install_extra_debs_squeeze
generic_debian:  install_debs install debian_sys_config install_cpan_force

ubuntu-hardy: hardy generic_ubuntu
ubuntu-lucid: lucid generic_ubuntu
hardy: install_pgsql_client_debs_82 install_yaz install_cpan_marc install_extra_encode
lucid: install_pgsql_client_debs_84 install_extra_debs
generic_ubuntu: install_debs install debian_sys_config install_cpan_more install_cpan_safe install_cpan_force

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

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

install_cpan_more: 
	for m in $(CPAN_MODULES_MORE); 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_cpan_force:
	for m in $(CPAN_MODULES_FORCE); do \
		echo "force install $$m" | perl -MCPAN -e shell ;\
	done

# Install the Safe Perl module
# Is this really unpackaged everywhere except for Fedora?
install_cpan_safe: 
	for m in $(CPAN_MODULES_SAFE); do perl -MCPAN -e "install \"$$m\";"; done

# Install the CPAN modules for Fedora 13
install_cpan_fedora: 
	for m in $(FEDORA_13_CPAN); 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 && ldconfig

# Install the custom spidermonkey libs and JavaScript-SpiderMonkey Perl modules
install_js_sm: install_libjs install_spidermonkey

install_libjs: 
	if [ ! -f $(LIBJS).tar.gz ]; then wget $(LIBJS_URL); fi;
	tar -zxf $(LIBJS).tar.gz
	cd js/src/ && JS_THREADSAFE=true JS_DIST=/usr 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/

install_spidermonkey:
	if [ ! -f $(LIBJS_PERL).tar.gz ]; then wget $(LIBJS_PERL_URL); fi;
	tar -zxf $(LIBJS_PERL).tar.gz
	if [ ! -z $(FEDORA_64) ]; then \
		sed -i -e 's/"\/usr\/lib"/"\/usr\/lib64"/' $(LIBJS_PERL)/Makefile.PL ; \
		sed -i -e 's/js32.dll/libjs.so/' $(LIBJS_PERL)/Makefile.PL ; \
	fi;
	cd $(LIBJS_PERL) && perl Makefile.PL -E4X -JS_THREADSAFE && 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_84:
	$(APT_TOOL) install $(PGSQL_CLIENT_DEBS_84)

install_pgsql_server_debs_84:
	LANG=C $(APT_TOOL) install $(PGSQL_SERVER_DEBS_84)

install_pgsql_client_debs_83:
	$(APT_TOOL) install $(PGSQL_CLIENT_DEBS_83)

install_pgsql_client_debs_82:
	$(APT_TOOL) install $(PGSQL_CLIENT_DEBS_82)

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

install_extra_debs:
	$(APT_TOOL) install $(EXTRA_DEBS)

# Install specific modules required by Ubuntu Hardy - see bug 525069
install_extra_encode:
	$(APT_TOOL) install $(EXTRA_ENCODE)

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

# FEDORA 13
install_fedora_13_rpms:
	yum -y update
	yum -y install $(FEDORA_13_RPMS)

install_fedora_13_pgsql_server:
	yum -y install $(PGSQL_84_RPMS)

# CENTOS
install_centos_rpms:
	yum -y install $(CENTOS)
	yum -y update
	if [ $(LBITS) -eq 64 ]; then \
		ln -sf /usr/lib64/libpq.* /usr/lib/ && \
		ln -sf /usr/lib64/perl5/5.8.8/x86_64-linux-thread-multi/CORE/libperl.so /usr/lib/ && \
		ldconfig ; \
	fi;
	if [ $(LBITS) -eq 32 ]; then \
		ln -sf /usr/lib/perl5/5.8.8/i386-linux-thread-multi/CORE/libperl.so /usr/lib && \
		ldconfig ; \
	fi;

install_centos_pgsql:
	if [ $(LBITS) -eq 64 ]; then yum remove -y postgresql-libs-8.1*i386 apr-util-devel-*i386 ; fi;
	wget $(PGSQL_HOST)/$(PGSQL_CENTOS)
	rpm -Uvh --force ./$(PGSQL_CENTOS)
	yum update -y
	yum -y install $(PGSQL_84_RPMS)

install_redhat_pgsql:
	if [ $(LBITS) -eq 64 ]; then yum remove -y postgresql-libs-8.1*i386 apr-util-devel-*i386 ; fi;
	wget $(PGSQL_HOST)/$(PGSQL_REDHAT)
	rpm -Uvh --force ./$(PGSQL_REDHAT)
	yum update -y
	yum -y install $(PGSQL_84_RPMS)

install_centos_perl:
	for m in $(CENTOS_PERL); do perl -MCPAN -e "install \"$$m\";"; done
	echo "force install Business::OnlinePayment::AuthorizeNet" | perl -MCPAN -e shell
	echo "force install Scalar::Util" | perl -MCPAN -e shell	

# 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:
