# -*- makefile -*-
# rules to unpack the source tarballs in $(srcdir); if the source dir already
# exists, the rule exits with an error to prevent deletion of modified
# source files. It has to be deleted manually.

tarballs = $(gcc_tarball) # $(gcj_tarball)
ifeq ($(with_d),yes)
  tarballs += $(gdc_tarball)
endif

unpack_stamps = $(foreach i,$(tarballs),$(unpack_stamp)-$(i))

unpack: stamp-dir $(unpack_stamp) debian-chmod
$(unpack_stamp): $(unpack_stamps)
$(unpack_stamp): $(foreach p,$(debian_tarballs),unpacked-$(p))
	echo -e "\nBuilt from Debian source package $(PKGSOURCE)-$(SOURCE_VERSION)" \
		> pxxx
	echo -e "Integrated upstream packages in this version:\n" >> pxxx
	for i in $(tarballs); do echo "  $$i" >> pxxx; done
	mv -f pxxx $@

debian-chmod:
	@chmod 755 debian/dh_*

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

gfdl_texinfo_files = \
	gcc/doc/arm-neon-intrinsics.texi \
	gcc/doc/bugreport.texi \
	gcc/doc/c-tree.texi \
	gcc/doc/cfg.texi \
	gcc/doc/collect2.texi \
	gcc/doc/compat.texi \
	gcc/doc/configfiles.texi \
	gcc/doc/configterms.texi \
	gcc/doc/contrib.texi \
	gcc/doc/contribute.texi \
	gcc/doc/cpp.texi \
	gcc/doc/cppenv.texi \
	gcc/doc/cppinternals.texi \
	gcc/doc/cppopts.texi \
	gcc/doc/extend.texi \
	gcc/doc/fragments.texi \
	gcc/doc/frontends.texi \
	gcc/doc/gccint.texi \
	gcc/doc/gcov.texi \
	gcc/doc/generic.texi \
	gcc/doc/gimple.texi \
	gcc/doc/gnu.texi \
	gcc/doc/gty.texi \
	gcc/doc/headerdirs.texi \
	gcc/doc/hostconfig.texi \
	gcc/doc/implement-c.texi \
	gcc/doc/implement-cxx.texi \
	gcc/doc/install-old.texi \
	gcc/doc/install.texi \
	gcc/doc/interface.texi \
	gcc/doc/invoke.texi \
	gcc/doc/languages.texi \
	gcc/doc/libgcc.texi \
	gcc/doc/loop.texi \
	gcc/doc/lto.texi \
	gcc/doc/makefile.texi \
	gcc/doc/md.texi \
	gcc/doc/objc.texi \
	gcc/doc/options.texi \
	gcc/doc/passes.texi \
	gcc/doc/plugins.texi \
	gcc/doc/portability.texi \
	gcc/doc/rtl.texi \
	gcc/doc/service.texi \
	gcc/doc/sourcebuild.texi \
	gcc/doc/standards.texi \
	gcc/doc/tm.texi.in \
	gcc/doc/tm.texi \
	gcc/doc/tree-ssa.texi \
	gcc/doc/trouble.texi \
	gcc/doc/include/gcc-common.texi \
	gcc/doc/include/funding.texi \
	gcc/fortran/gfc-internals.texi \
	gcc/fortran/invoke.texi \
	gcc/fortran/intrinsic.texi \


gfdl_toplevel_texinfo_files = \
	gcc/doc/gcc.texi \
	gcc/java/gcj.texi \
	gcc/ada/gnat-style.texi \
	gcc/ada/gnat_rm.texi \
	gcc/ada/gnat_ugn.texi \
	gcc/fortran/gfortran.texi \
	gcc/go/gccgo.texi \
	libgomp/libgomp.texi \
	libquadmath/libquadmath.texi \

gfdl_manpages = \
	gcc/doc/cpp.1 \
	gcc/doc/g++.1 \
	gcc/doc/gc-analyze.1 \
	gcc/doc/gcc.1 \
	gcc/doc/gccgo.1 \
	gcc/doc/gcj.1 \
	gcc/doc/gcj-dbtool.1 \
	gcc/doc/gcjh.1 \
	gcc/doc/gcov.1 \
	gcc/doc/gfortran.1 \
	gcc/doc/gij.1 \
	gcc/doc/grmic.1 \
	gcc/doc/grmiregistry.1 \
	gcc/doc/jcf-dump.1 \
	gcc/doc/jv-convert.1 \
	gcc/doc/fsf-funding.7 \

# ---------------------------------------------------------------------------
$(unpack_stamp)-$(gcc_tarball): $(gcc_tarpath)
	: # unpack gcc tarball
	-mkdir $(stampdir)
	if [ -d $(srcdir) ]; then \
	  echo >&2 "Source directory $(srcdir) exists. Delete by hand"; \
	  false; \
	fi
	rm -rf $(gcc_srcdir)
	case $(gcc_tarball) in \
	  *.bz2)  tar -x --bzip2 -f $(gcc_tarpath);; \
	  *.gz)   tar -x --gzip  -f $(gcc_tarpath);; \
	  *.lzma) lzcat $(gcc_tarpath) | tar -x -f -;; \
	  *.xz) xzcat $(gcc_tarpath) | tar -x -f -;; \
	  *)     false; \
	esac
	mv $(gcc_srcdir) $(srcdir)
ifneq (,$(wildcard java-class-files.tar.xz.uue))
# work around #533356
#	uudecode -o - java-class-files.tar.xz.uue | tar -C src -xvz
	uudecode java-class-files.tar.xz.uue
	xzcat java-class-files.tar.xz | tar -C src -xv -f -
	rm -f java-class-files.tar.xz
endif
#ifeq ($(with_java),yes)
#	tar -x -C $(srcdir)/libjava/testsuite/libmauve.exp \
#		$(wildcard /usr/src/mauve*.tar.*)
#endif
ifeq (0,1)
	cd $(srcdir) && tar cfj ../gcc-4.1.1-doc.tar.bz2 \
		$(gfdl_texinfo_files) \
		$(gfdl_toplevel_texinfo_files) \
		$(gfdl_manpages)
endif
ifeq ($(GFDL_INVARIANT_FREE),yes)
  ifneq ($(single_package),yes)
	rm -f $(srcdir)/gcc/doc/*.1
	rm -f $(srcdir)/gcc/doc/fsf-funding.7
	rm -f $(srcdir)/gcc/doc/*.info
	rm -f $(srcdir)/gcc/fortran/*.info
	rm -f $(srcdir)/libgomp/*.info
	rm -f $(srcdir)/gcc/java/*.1
	rm -f $(srcdir)/gcc/java/*.info
	for i in $(gfdl_texinfo_files); do \
	  if [ -f $(srcdir)/$$i ]; then \
	    cp $(SOURCE_DIR)debian/dummy.texi $(srcdir)/$$i; \
	  else \
	    echo >&2 "$$i does not exist, fix debian/rules.unpack"; \
	  fi; \
	done
	for i in $(gfdl_toplevel_texinfo_files); do \
	  n=$$(basename $$i .texi); \
	  if [ -f $(srcdir)/$$i ]; then \
	    sed "s/@name@/$$n/g" $(SOURCE_DIR)debian/gcc-dummy.texi \
	      > $(srcdir)/$$i; \
	  else \
	    echo >&2 "$$i does not exist, fix debian/rules.unpack"; \
	  fi; \
	done
	for i in $(gfdl_manpages); do \
	  touch $(srcdir)/$$i; \
	done
	rm -f $(srcdir)/INSTALL/*.html
  endif
endif
	echo "$(gcc_tarball) unpacked." > $@

# ---------------------------------------------------------------------------
ifneq (,$(gcj_tarball))
$(unpack_stamp)-$(gcj_tarball): $(gcj_tarpath) $(unpack_stamp)-$(gcc_tarball)
	: # unpack gcj tarball
	rm -rf $(srcdir)/gcc/java $(srcdir)/libjava
	tar -x -C $(srcdir) -f $(gcj_tarpath)
ifeq ($(GFDL_INVARIANT_FREE),yes)
  ifneq ($(single_package),yes)
	rm -f $(srcdir)/gcc/java/*.1
	rm -f $(srcdir)/gcc/java/*.info
	for i in $(gfdl_texinfo_files); do \
	  if [ -f $(srcdir)/$$i ]; then \
	    cp $(SOURCE_DIR)debian/dummy.texi $(srcdir)/$$i; \
	  else \
	    echo >&2 "$$i does not exist, fix debian/rules.unpack"; \
	  fi; \
	done
	for i in $(gfdl_toplevel_texinfo_files); do \
	  n=$$(basename $$i .texi); \
	  if [ -f $(srcdir)/$$i ]; then \
	    sed "s/@name@/$$n/g" $(SOURCE_DIR)debian/gcc-dummy.texi \
	      > $(srcdir)/$$i; \
	  else \
	    echo >&2 "$$i does not exist, fix debian/rules.unpack"; \
	  fi; \
	done
  endif
endif
	echo "$(gcj_tarball) unpacked." > $@
endif

# ---------------------------------------------------------------------------
ifneq (,$(gdc_tarball))
$(unpack_stamp)-$(gdc_tarball): $(gdc_tarpath)
  ifneq ($(with_libphobos),yes)
	echo >&2 "libphobos v2 needs porting: $(with_libphobos)"
	false
  endif
	: # unpack gdc tarball
	-mkdir $(stampdir)
	if [ -d $(srcdir)/gcc/d ]; then \
	  echo >&2 "Source directory $(srcdir)/gcc/d exists. Delete by hand";\
	  false; \
	fi
	#rm -rf $(gdc_srcdir)
	rm -rf d
	case $(gdc_tarball) in \
	  *.bz2)  tar -x --bzip2 -f $(gdc_tarpath);; \
	  *.gz)   tar -x --gzip  -f $(gdc_tarpath);; \
	  *.lzma) lzcat $(gdc_tarpath) | tar -x -f -;; \
	  *.xz) xzcat $(gdc_tarpath) | tar -x -f -;; \
	 *)     false; \
	esac
	if [ -d d ]; then \
	  mv d $(srcdir)/gcc/. ; \
	else \
	  mv $(gdc_srcdir)/d $(srcdir)/gcc/. ; \
	  rm -rf $(gdc_srcdir)/CVS; \
	  rmdir $(gdc_srcdir); \
	fi
  ifeq ($(with_libphobos),yes)
	[ -d $(srcdir)/libphobos ] && rm -rf $(srcdir)/libphobos || true
	mkdir $(srcdir)/libphobos && \
	  cd $(srcdir)/libphobos && \
	    ../symlink-tree ../gcc/d/phobos$(libphobos_version) 2>&1 && \
	cd $(srcdir)
  endif
  ifeq ($(libphobos_version),2)
	cd $(srcdir)/libphobos && \
	    ../symlink-tree ../gcc/d/druntime 2>&1 && \
	cd $(srcdir)
	echo "D_LANGUAGE_VERSION=2" > $(srcdir)/gcc/d/d-make-include
  else
	echo "D_LANGUAGE_VERSION=1" > $(srcdir)/gcc/d/d-make-include
  endif
	echo "$(gdc_tarball) unpacked." > $@
endif
