#!/usr/bin/make -f

ruby_ver = 1.9.1
ruby_ver_major = 1.9
ruby_ver_dir = $(ruby_ver)
ruby_libpkgsfx = $(ruby_ver)
tcltk_ver = 8.5

arch_name = $(subst linux-gnu,linux-,$(patsubst %linux-gnu,%linux,$(DEB_HOST_GNU_TYPE)))
ifeq (i686-linux-lp,$(arch_name))
 arch_name = i686-linux
endif

ruby_name = ruby$(ruby_ver)
ruby_libbase = usr/lib/ruby
ruby_libdir = $(ruby_libbase)/$(ruby_ver_dir)
ruby_archdir = $(ruby_libbase)/$(ruby_ver_dir)/$(arch_name)
bin_dir = usr/bin
man_dir = usr/share/man/man1

el_etc = etc/emacs/site-start.d

examples_dir = $(CURDIR)/debian/ruby$(ruby_ver)-examples/usr/share/doc/ruby$(ruby_ver)-examples/examples

insns_cache_dir = debian/generated-incs

#deb_ver = $(shell dpkg-parsechangelog | awk '/^Version:/{print $2}')

include /usr/share/cdbs/1/rules/debhelper.mk
include /usr/share/cdbs/1/class/makefile.mk
include /usr/share/cdbs/1/class/autotools.mk

DEB_AUTO_UPDATE_AUTOCONF = YES

CFLAGS += -fno-strict-aliasing
CXXFLAGS += -fno-strict-aliasing
LDFLAGS += -L$(CURDIR)/debian/lib


# Renesas SH(sh4) needs -mieee option. #591785
ifeq (sh4,$(shell dpkg-architecture -qDEB_HOST_ARCH))
	CFLAGS += -mieee
endif

ifeq ($(DEB_HOST_GNU_CPU),i386)
	DEB_CONFIGURE_USER_FLAGS += --enable-frame-address
endif
ifneq (,$(filter $(DEB_HOST_GNU_CPU),i486 i586 i686))
	DEB_CONFIGURE_USER_FLAGS += --enable-frame-address
endif
DEB_CONFIGURE_USER_FLAGS += --enable-pthread
DEB_CONFIGURE_USER_FLAGS += --enable-shared
DEB_CONFIGURE_USER_FLAGS += --disable-rpath
DEB_CONFIGURE_USER_FLAGS += --disable-install-doc
DEB_CONFIGURE_USER_FLAGS += --with-vendordir='/$(ruby_libbase)/vendor_ruby'
DEB_CONFIGURE_USER_FLAGS += --with-sitedir='/usr/local/lib/site_ruby'
DEB_CONFIGURE_USER_FLAGS += --program-suffix=1.9.1
DEB_CONFIGURE_USER_FLAGS += --with-soname=ruby-1.9.1
# the following are ignored by ./configure, but used by some extconf.rb scripts
DEB_CONFIGURE_USER_FLAGS += --enable-ipv6
DEB_CONFIGURE_USER_FLAGS += --with-dbm-type=gdbm_compat
DEB_CONFIGURE_USER_FLAGS += --with-tklib=tk$(tcltk_ver)
DEB_CONFIGURE_USER_FLAGS += --with-tcllib=tcl$(tcltk_ver)
DEB_CONFIGURE_USER_FLAGS += --with-tcl-include=/usr/include/tcl$(tcltk_ver)
DEB_CONFIGURE_USER_FLAGS += --with-tk-include=/usr/include/tcl$(tcltk_ver)
DEB_CONFIGURE_USER_FLAGS += --with-tcl-lib=/usr/lib/$(DEB_HOST_MULTIARCH)
DEB_CONFIGURE_USER_FLAGS += --with-tk-lib=/usr/lib/$(DEB_HOST_MULTIARCH)
DEB_CONFIGURE_USER_FLAGS += --with-bundled-sha1
DEB_CONFIGURE_USER_FLAGS += --with-bundled-md5
DEB_CONFIGURE_USER_FLAGS += --with-bundled-rmd160

ifeq (,$(filter nordoc,$(DEB_BUILD_OPTIONS)))
DEB_MAKE_BUILD_TARGET = all
else
DEB_MAKE_BUILD_TARGET = main
endif

DEB_INSTALL_DOCS_libtcltk-ruby$(ruby_ver) += $(DEB_SRCDIR)/ext/tk/README*
DEB_INSTALL_DOCS_libtcltk-ruby$(ruby_ver) += $(DEB_SRCDIR)/ext/tk/MANUAL*

DEB_INSTALL_DIRS_ruby$(ruby_ver) += $(bin_dir)
DEB_INSTALL_DIRS_ruby$(ruby_ver) += $(man_dir)
DEB_INSTALL_DOCS_ruby$(ruby_ver) += 
DEB_INSTALL_DOCS_ruby$(ruby_ver) += $(DEB_SRCDIR)/LEGAL
DEB_INSTALL_DOCS_ruby$(ruby_ver) += $(DEB_SRCDIR)/README
DEB_INSTALL_DOCS_ruby$(ruby_ver) += $(DEB_SRCDIR)/README.ja
DEB_INSTALL_DOCS_ruby$(ruby_ver) += $(DEB_SRCDIR)/ToDo
DEB_INSTALL_DOCS_ruby$(ruby_ver) += $(DEB_SRCDIR)/doc/NEWS-1.8.7
DEB_INSTALL_MANPAGES_ruby$(ruby_ver) += debian/manpages/testrb$(ruby_ver).1
DEB_INSTALL_MANPAGES_ruby$(ruby_ver) += debian/manpages/erb$(ruby_ver).1
DEB_INSTALL_MANPAGES_ruby$(ruby_ver) += debian/manpages/rake$(ruby_ver).1
DEB_INSTALL_MANPAGES_ruby$(ruby_ver) += debian/manpages/rdoc$(ruby_ver).1
DEB_INSTALL_MANPAGES_ruby$(ruby_ver) += debian/manpages/irb$(ruby_ver).1
DEB_INSTALL_MANPAGES_ruby$(ruby_ver) += debian/manpages/gem$(ruby_ver).1
DEB_INSTALL_MANPAGES_ruby$(ruby_ver) += debian/manpages/ri$(ruby_ver).1

DEB_INSTALL_DOCS_libruby$(ruby_libpkgsfx) += $(DEB_SRCDIR)/lib/README
DEB_INSTALL_DOCS_libruby$(ruby_libpkgsfx) += $(DEB_SRCDIR)/doc/NEWS-1.8.7
DEB_INSTALL_DOCS_libruby$(ruby_libpkgsfx) += $(DEB_SRCDIR)/doc/*.rd*


DEB_INSTALL_DOCS_ruby$(ruby_ver)-dev += $(DEB_SRCDIR)/README.EXT*

DEB_COMPRESS_EXCLUDE = .rb .pl .py .scm .awk .dat
DEB_SHLIBDEPS_INCLUDE = $(CURDIR)/debian/libruby$(ruby_libpkgsfx)/usr/lib
DEB_DH_MAKESHLIBS_ARGS_libruby$(ruby_libpkgsfx) = -V

pre-build::
	(find $(insns_cache_dir) -type f -print; \
	  echo configure; \
	  echo parse.c; \
	) | \
	  while read f; \
	  do \
	    bf=`basename $$f`; \
	    if [ ! -e $$bf.upstream-orig ] && [ -e $$bf ]; then \
	      cp -p $$bf $$bf.upstream-orig; \
	    fi; \
	    if [ "$$f" != "$$bf" ]; then \
	      cp -p $$f $$bf; \
	    fi; \
	  done
	echo -n "uname -a: "
	uname -a
	echo "/proc/cpuinfo: ----------"
	-cat /proc/cpuinfo
	echo "-------------------------"
	mkdir -p debian/lib
	ln -sf /usr/lib/$(DEB_HOST_MULTIARCH)/libtcl$(tcltk_ver).so debian/lib/libtcl.so
	ln -sf /usr/lib/$(DEB_HOST_MULTIARCH)/libtk$(tcltk_ver).so debian/lib/libtk.so

common-post-build-arch::
ifeq (,$(filter nocheck,$(DEB_BUILD_OPTIONS)))
# Disable test suites on some broken architectures.
# See http://lists.debian.org/debian-release/2011/10/msg00279.html
ifeq (,$(filter $(DEB_BUILD_ARCH),ia64 kfreebsd-i386 kfreebsd-amd64 sparc))
	debian/run-test-suites.bash
endif
endif

binary-install/libtcltk-ruby$(ruby_ver)::
	dh_movefiles -p$(cdbs_curpkg) \
	    $(ruby_archdir)/tcltklib.so \
	    $(ruby_archdir)/tkutil.so
	chrpath --delete $(CURDIR)/debian/$(cdbs_curpkg)/$(ruby_archdir)/tcltklib.so
	sh $(CURDIR)/debian/extfixup_rubylibs.sh $(ruby_libdir) $(cdbs_curpkg) \
	    $(DEB_SRCDIR)/ext/tk

	install -d \
	    $(CURDIR)/debian/$(cdbs_curpkg)/usr/share/doc/$(cdbs_curpkg)/examples
	(cd $(DEB_SRCDIR)/ext/tk/sample && \
	 tar cf - .) | \
	  (cd $(CURDIR)/debian/$(cdbs_curpkg)/usr/share/doc/$(cdbs_curpkg)/examples && tar xf -)
	mv $(CURDIR)/debian/tmp/$(ruby_libdir)/tkextlib/SUPPORT_STATUS \
	    $(CURDIR)/debian/$(cdbs_curpkg)/usr/share/doc/$(cdbs_curpkg)/
	install \
	    $(DEB_SRCDIR)/ext/tk/lib/README \
	    $(CURDIR)/debian/$(cdbs_curpkg)/usr/share/doc/$(cdbs_curpkg)/README.lib
	install \
	    $(DEB_SRCDIR)/ext/tk/lib/tkextlib/tcllib/README \
	    $(CURDIR)/debian/$(cdbs_curpkg)/usr/share/doc/$(cdbs_curpkg)/README.tcllib
	install \
	    $(DEB_SRCDIR)/ext/tk/lib/tkextlib/tkimg/README \
	    $(CURDIR)/debian/$(cdbs_curpkg)/usr/share/doc/$(cdbs_curpkg)/README.tkimg

binary-install/libruby$(ruby_libpkgsfx):: $(patsubst %,binary-install/%,$(filter-out libruby$(ruby_libpkgsfx),$(DEB_PACKAGES)))
	install -d $(CURDIR)/debian/$(cdbs_curpkg)/$(ruby_archdir)
	for s in $(CURDIR)/debian/$(cdbs_curpkg).*.in; do \
	    sed -e 's/@@arch@@/$(arch_name)/g' \
		-e 's/@@verdir@@/$(ruby_ver_dir)/g' \
		< $$s > `expr $$s : '\(.*\)\.in$$'`; \
	done

	(cd $(CURDIR)/debian/tmp/usr/lib && \
	    for f in libruby-$(ruby_ver).so.$(ruby_ver) libruby-$(ruby_ver).so.$(ruby_ver_major); do \
		echo usr/lib/$$f; \
	    done) | xargs dh_movefiles -p$(cdbs_curpkg) 
	# Do not install the SSL certs bundled in the upstream source
	dh_movefiles -p$(cdbs_curpkg) -Xssl_certs $(ruby_libdir)

	cd $(DEB_SRCDIR)/ext && \
	for dir in \
	    bigdecimal continuation curses digest dl etc \
	    fcntl iconv io json nkf openssl psych pty racc readline \
	    ripper sdbm socket stringio strscan syck syslog zlib \
	    ; \
	do \
	    install -d "$(CURDIR)/debian/$(cdbs_curpkg)/usr/share/doc/$(cdbs_curpkg)/$$dir"; \
	    find $$dir \
	  	    -name '*README*' -or -name '*.html*' -or \
	  	    -name '*.txt*' -or -name '*.rd*' | \
		while read fname; \
		do \
		    cp "$$fname" \
		     	"$(CURDIR)/debian/$(cdbs_curpkg)/usr/share/doc/$(cdbs_curpkg)/$$dir"; \
		done; \
		rmdir "$(CURDIR)/debian/$(cdbs_curpkg)/usr/share/doc/$(cdbs_curpkg)/$$dir" 2>/dev/null || :; \
	    done

	find $(CURDIR)/debian/etc -type f -name "*.in" | \
	  while read fname; \
	  do \
	    sed -e 's/@@VERSION@@/1\.9\.1/g' -e 's/@@DOTLESS_VERSION@@/191/g' \
		< $$fname > `expr $$fname : '\(.*\)\.in$$'`1.9.1; \
	  done; \
	  : \
	done
	dh_install -p$(cdbs_curpkg) \
	  debian/etc/bash_completion.d/gem1.9.1 etc/bash_completion.d/

binary-install/ruby$(ruby_ver)-dev::
	dh_movefiles -p$(cdbs_curpkg) \
	    usr/lib/libruby-$(ruby_ver).so \
	    usr/lib/libruby-$(ruby_ver)-static.a \
	    $(ruby_libdir)/mkmf.rb \
	    usr/lib/pkgconfig/ruby-1.9.pc
	(cd $(CURDIR)/debian/tmp && \
	    find usr/include/ruby-$(ruby_ver) -name '*.h' -type f) | \
	    xargs dh_movefiles -p$(cdbs_curpkg)

binary-install/ruby$(ruby_ver)::
	dh_movefiles -p$(cdbs_curpkg) \
	    usr/bin/ruby1.9.1 \
	    usr/bin/erb1.9.1 \
	    usr/bin/testrb1.9.1 \
	    usr/bin/rake1.9.1 \
	    usr/bin/rdoc1.9.1 \
	    usr/bin/irb1.9.1 \
	    usr/bin/ri1.9.1 \
	    usr/bin/gem1.9.1 \
	    usr/share/man/man1/ruby1.9.1.1

binary-post-install/ri$(ruby_ver)::
ifeq (,$(filter nordoc,$(DEB_BUILD_OPTIONS)))
	+$(DEB_MAKE_INVOKE) install-doc DESTDIR=$(CURDIR)/debian/$(cdbs_curpkg) \
	    RDOCFLAGS="--exclude debian --exclude test"
endif

binary-post-install/ruby$(ruby_ver)-examples::
	install -d $(examples_dir)/bigdecimal
	cp -a $(DEB_SRCDIR)/ext/bigdecimal/sample/* $(examples_dir)/bigdecimal
	install -d $(examples_dir)/curses
	cd $(DEB_SRCDIR)/ext/curses && \
	  cp hello.rb mouse.rb rain.rb view.rb view2.rb \
	    $(examples_dir)/curses
#	install -d $(examples_dir)/digest
#	install -d $(examples_dir)/dl
#	install -d $(examples_dir)/enumerator
#	install -d $(examples_dir)/etc
#	install -d $(examples_dir)/fcntl
#	install -d $(examples_dir)/iconv
#	install -d $(examples_dir)/io
#	install -d $(examples_dir)/nkf
	install -d $(examples_dir)/pty
#	install -d $(examples_dir)/racc
	install -d $(examples_dir)/ripper
	cd $(DEB_SRCDIR)/ext/ripper && \
	  cp tools/* $(examples_dir)/ripper
#	install -d $(examples_dir)/sdbm
#	install -d $(examples_dir)/socket
#	install -d $(examples_dir)/stringio
#	install -d $(examples_dir)/strscan
#	install -d $(examples_dir)/syck
#	install -d $(examples_dir)/syslog
#	install -d $(examples_dir)/zlib
	(cd $(DEB_SRCDIR)/sample && tar cf - .) | \
	  (cd $(examples_dir) && tar xf -)
	find $(examples_dir) -type f -name '*.rb' -perm /111 -exec chmod a-x {} \+

$(patsubst %,binary-post-install/%,$(DEB_PACKAGES))::
	bash $(CURDIR)/debian/fixshebang.sh ruby$(ruby_ver) \
	    '$(CURDIR)/debian/$(cdbs_curpkg)/$(bin_dir)'
	bash $(CURDIR)/debian/fixshebang.sh ruby$(ruby_ver) \
	    '$(CURDIR)/debian/$(cdbs_curpkg)/usr/share/doc/$(cdbs_curpkg)/examples'
	sh $(CURDIR)/debian/rmshebang.sh \
	    '$(CURDIR)/debian/$(cdbs_curpkg)/$(ruby_libdir)'

clean::
	rm -rf \
	    .installed.list \
	    .ext \
	    exts.mk \
	    prelude.c \
	    debian/libruby$(ruby_libpkgsfx).postinst \
	    debian/libruby$(ruby_libpkgsfx).prerm \
	    debian/libruby$(ruby_libpkgsfx).postrm \
	    debian/etc/bash_completion.d/gem1.9.1
	(find $(insns_cache_dir) -type f -print; \
	  echo configure; \
	  echo parse.c; \
	) | \
	  while read f; \
	  do \
	    bf=`basename $$f`; \
	    if [ -e $$bf.upstream-orig ]; then \
	      mv -f $$bf.upstream-orig $$bf; \
	    fi; \
	  done
	echo -n "uname -a: "

	if [ -f $(DEB_SRCDIR)/config.guess.cdbs-orig ]; \
	then \
	    mv $(DEB_SRCDIR)/config.guess.cdbs-orig \
		$(DEB_SRCDIR)/config.guess; \
	fi
	if [ -f $(DEB_SRCDIR)/config.sub.cdbs-orig ]; \
	then \
	    mv $(DEB_SRCDIR)/config.sub.cdbs-orig \
		$(DEB_SRCDIR)/config.sub; \
	fi
	rm -rf debian/lib

PACKAGE = ruby1.9.1
SRC_VERSION := $(shell dpkg-parsechangelog | sed -ne 's/^Version: \(\([0-9]\+\):\)\?\(.*\)-.*/\3/p')
SVN_REVISION := $(shell echo $(SRC_VERSION) | awk -F"+" '{ print $$2 }' | sed 's/svn//' )
TARBALL = $(PACKAGE)_$(SRC_VERSION).orig.tar.gz
SVN_PATH = http://svn.ruby-lang.org/repos/ruby/branches/ruby_1_9_3
#SVN_PATH = http://svn.ruby-lang.org/repos/ruby/trunk
.PHONY: get-orig-source check-orig-source
get-orig-source:
	rm -rf get-orig-source $(TARBALL)
	mkdir get-orig-source
	svn export -r $(SVN_REVISION) $(SVN_PATH) \
	get-orig-source/$(PACKAGE)-$(SRC_VERSION).orig
	GZIP=--best tar czf $(TARBALL) -C get-orig-source $(PACKAGE)-$(SRC_VERSION).orig
	rm -rf get-orig-source
	echo "  "$(TARBALL)" created; move it to the right destination to build the package"

check-orig-source:
	svn log $(SVN_PATH) | head -20
