#!/bin/bash

eval $(echo '#include "./patchlevel.h"
SETver=PERL_REVISION.PERL_VERSION;
SETsubver= PERL_SUBVERSION' | gcc -E -DPERL_PATCHLEVEL_H_IMPLICIT - \
 | sed -n '/^SET/{s///;s/ //gp;}')

fullver="$ver.$subver"
nextver="$ver."$(($subver+1))
ccflags=-DDEBIAN
ldflags=
host_arch=${DEB_HOST_ARCH:-$(dpkg-architecture -qDEB_HOST_ARCH)}
host_arch_cpu=${DEB_HOST_ARCH_CPU:-$(dpkg-architecture -qDEB_HOST_ARCH_CPU)}
host_gnu_type=${DEB_HOST_GNU_TYPE:-$(dpkg-architecture -qDEB_HOST_GNU_TYPE)}
build_gnu_type=${DEB_BUILD_GNU_TYPE:-$(dpkg-architecture -qDEB_BUILD_GNU_TYPE)}
optimize=-O2
debugging=-g

archtriplet=${DEB_HOST_MULTIARCH:-$(dpkg-architecture -qDEB_HOST_MULTIARCH)}

# this goes in config.h to make perl -V output reproducible
PERL_BUILD_DATE=$(LC_ALL=C date '+%b %e %Y %T' --utc -d "$(dpkg-parsechangelog | sed -n -e 's/^Date: //p')")

case "$1" in
    --static) # static perl
    	build_type=static
	opts="-Uuseshrplib -Dotherlibdirs=\"/usr/lib/$archtriplet/perl-base\"";;

    --debug) # debugperl
    	build_type=debug
	debugging=both # add -DDEBUGGING
	opts="-Uuseshrplib";;

    --shared) # shared library
    	build_type=shared
	opts="-Duseshrplib -Dlibperl=libperl.so.$fullver";;

    --version)
	exec echo $ver;;

    --full-version)
	exec echo $fullver;;

    --next-version)
	exec echo $nextver;;

    *)	echo "$0: need --shared, --static, or --debug option"
    	exit 2;;
esac

case "$host_arch_cpu:$build_type" in
    sh4:*) # required to correctly handle floating point on sh4
    	ccflags="$ccflags -mieee";;

    m68k:shared) # work around an optimiser bug
	ccflags="$ccflags -fno-regmove";;
esac

case ",$DEB_BUILD_OPTIONS," in
    *[,\ ]noopt[,\ ]*)
	optimize="$optimize${optimize:+ }-O0";;
esac

cc=${CC:-${host_gnu_type}-gcc}
cpp=${CPP:-${host_gnu_type}-cpp}
ld=${LD:-${host_gnu_type}-gcc}

if which dpkg-buildflags >/dev/null 2>&1; then
    ccflags="$ccflags $(dpkg-buildflags --get CPPFLAGS)"
    ccflags="$ccflags $(dpkg-buildflags --get CFLAGS)"
    ldflags="$ldflags $(dpkg-buildflags --get LDFLAGS)"
fi

mkdir build-$build_type
cd build-$build_type

# post-configure tweaks
cp ../debian/config.over .

if [ "$build_gnu_type" = "$host_gnu_type" ]
then
    # native builds
    # need bash when sourcing config.over
    eval /bin/bash ../Configure			\
    -Dmksymlinks                \
    -Dusethreads				\
    -Duselargefiles				\
    -Dcc=$cc					\
    -Dcpp=$cpp					\
    -Dld=$ld					\
    -Dccflags=\'$ccflags\'			\
    -Dldflags=\'$ldflags\'			\
    -Dlddlflags=\'-shared $ldflags\'			\
    -Dcccdlflags=-fPIC				\
    -Darchname=$host_gnu_type			\
    -Dprefix=/usr				\
    -Dprivlib=/usr/share/perl/$ver		\
    -Darchlib=/usr/lib/$archtriplet/perl/$ver		\
    -Dvendorprefix=/usr				\
    -Dvendorlib=/usr/share/perl5		\
    -Dvendorarch=/usr/lib/$archtriplet/perl5/$ver			\
    -Dsiteprefix=/usr/local			\
    -Dsitelib=/usr/local/share/perl/$fullver	\
    -Dsitearch=/usr/local/lib/$archtriplet/perl/$fullver	\
    -Dman1dir=/usr/share/man/man1		\
    -Dman3dir=/usr/share/man/man3		\
    -Dsiteman1dir=/usr/local/man/man1		\
    -Dsiteman3dir=/usr/local/man/man3		\
    -Duse64bitint                               \
    -Dman1ext=1					\
    -Dman3ext=3perl				\
    -Dpager=/usr/bin/sensible-pager		\
    -Uafs					\
    -Ud_csh					\
    -Ud_ualarm					\
    -Uusesfio					\
    -Uusenm					\
    -Ui_libutil					\
    -Ui_xlocale					\
    -Uversiononly \
    -DDEBUGGING=$debugging			\
    -Doptimize=\"$optimize\"			\
    $extra_path					\
    -dEs $opts

	# note about the debian/cross files for the build log
	crossconfig=../debian/cross/${host_arch}/config.sh.static
	if [ "$build_type" = "static" ]; then
	 if [ -f $crossconfig ]; then
	  if ! diff --ignore-matching-lines="cf_time" \
	          --ignore-matching-lines="Configuration time" \
	          --ignore-matching-lines="Extracted from Debian package" \
	          $crossconfig config.sh >/dev/null
	  then
	    echo "Note: source package config.sh for cross building differs from generated one"
	    echo "Note: consider refreshing with debian/cross/extract-config-sh after the build"
	    diff -u0 $crossconfig config.sh | head -100
	    echo --------------------------------------------------
	  else
	    echo "Note: source package config.sh for cross building is up to date with the generated one (good!)"
	  fi
	 else
	    echo "Note: no source package config.sh for cross building"
	    echo "Note: consider including it with debian/cross/extract-config-sh after the build"
	 fi
	fi

    # for the Configure -S call below
    crossargs=
else
	# cross builds

	# find a ready made config.sh, currently in debian/cross/<arch>
	basedir=../debian/cross/${host_arch}
	baseconfig=${basedir}/config.sh.static
	if [ ! -f $baseconfig ]; then
	    echo "need $baseconfig for cross build!" 1>&2
	    exit 1
	fi

	# check for a new upstream version, invalidating old config.sh files
	crossversion=$(sed -n "s/^version='\(.*\)'/\1/ p" $baseconfig)
	if [ "$fullver" != "$crossversion" ]; then
	    echo "Fatal: need $baseconfig for cross build but it needs refreshing from $crossversion to $fullver!" 1>&2
	    echo "To fix: make a native build and run debian/cross/extract-config-sh on the results" 1>&2
	    echo "        (or fix $baseconfig by hand)" 1>&2
	    exit 1
	fi

	# arch-specific REFRESH files can be used to invalidate the old config.sh
	# files regardless of the version
	#   there can be a comment inside the REFRESH file
	#   example use case: changed Configure parameters
	if [ -e $basedir/REFRESH ]; then
	    echo "Fatal: need $baseconfig for cross build but it needs refreshing!" 1>&2
	    cat $basedir/REFRESH 1>&2
	    echo "To fix: make a native build and run debian/cross/extract-config-sh on the results" 1>&2
	    echo "        (or fix $baseconfig by hand)" 1>&2
	    echo "Then remove $basedir/REFRESH" 1>&2
	    exit 1
	fi

	# Next, ask Configure to compile a build system miniperl and
	# generate_uudmap executable in host/
	#
	# It's tempting to use the system perl for this, but that fails
	# when building extensions because it tries to dynamically load
	# XS modules from lib/, particularly as <*> uses File::Glob on
	# full perls (but not miniperl)
	if [ ! -x ../build-miniperl ]; then
	    echo "Cross build: making a native miniperl for the build process"
	    cd ..
	    ./Configure -dE -Dusecrosscompile
	    # host/ gets cleaned between builds by 'make distclean', so
	    # stash the results
	    cp host/miniperl build-miniperl
	    cp host/generate_uudmap build-generate_uudmap
	    cd build-$build_type
	fi

	# create symlinks for build directory
	# we can't seem to make Configure create these symlinks without probing
	# so make a separate mksymlinks helper for now
	sed '/^\?/d' ../regen-configure/U/perl/Mksymlinks.U > ../mksymlinks
	src=$(pwd)/..
	mkdir UU
	cd UU
	src="$src" lns="ln -s" issymlink="test -h" bash ../../mksymlinks 4>&2
	cd ..

	# override configuration for cross building

	# Actually use the old config.sh files,
	# removing the absolute build path in 'src'
	sed 's/^src=.*/src=./' $baseconfig > ./config.sh

	if [ "$build_type" = "static" ]; then
		# notes for the build log
		echo "Cross build: recycling old probed configuration from $baseconfig"
		grep '^cf_time' $baseconfig
		md5sum $baseconfig
		# debian/cross/extract-config-sh appends a comment so show that
		grep "Extracted from Debian package version" $baseconfig
	else
		patchconfig=${basedir}/config.sh.$build_type.patch
		if [ ! -f "$patchconfig" ]; then
			echo "need $patchconfig for cross build!" 1>&2
			exit 1
		fi
		patch config.sh < $patchconfig
		echo "Cross build: recycling old probed configuration from $patchconfig"
		md5sum $patchconfig
	fi

	crossargs="-Dusecrosscompile \
               -Dhostgenerate=../build-generate_uudmap \
               -Dhostperl=../build-miniperl \
               -Dperl=/usr/bin/perl"
fi

# now continue with extracting Makefile et al.
/bin/bash ./Configure -S $crossargs

# append PERL_BUILD_DATE before the last #endif in config.h
# massive quoting problems prevent passing this to Configure
sed -i "\$i #define PERL_BUILD_DATE \"$PERL_BUILD_DATE\"" config.h