default_pre_build  &&

# seems perl script scripts/test-installation.pl is broken now
sed -i 's:CC="$(CC)" $(PERL) scripts/test-installation.pl $(common-objpfx)::' $SOURCE_DIRECTORY/Makefile &&

  cd      $SOURCE_DIRECTORY                  &&
  #
  # Unpack glibc-kernel-headers
  #
  #
  # Set GLIBC_HEADERS_DIR for use later
  #
  persistent_add  GLIBC_HEADERS_DIR                        &&

GLIBC_HEADERS_DIR=$SOURCE_DIRECTORY/${SOURCE3/.tar.xz} &&
if [[ "${HEADERS_VERSION}" < 6.0 ]]; then
  message "${MESSAGE_COLOR}xz -d $SOURCE_CACHE/$SOURCE3${DEFAULT_COLOR}" &&
  xz -d -k "$SOURCE_CACHE/$SOURCE3" &&
  SOURCE3_ORI=$SOURCE3 &&
  SOURCE3=${SOURCE3/.xz} &&
  unpack_file 3 &&
  message "${MESSAGE_COLOR}rm $SOURCE_CACHE/$SOURCE3${DEFAULT_COLOR}" &&
  rm "$SOURCE_CACHE/$SOURCE3" &&
  SOURCE3=$SOURCE3_ORI
else
  unpack_file 3
  fi &&

    cd  $GLIBC_HEADERS_DIR &&
    if [[ -n $SOURCE7 ]]; then
      if [[ ${HEADERS_VERSION} > 3.0.4 ]] &&
         ! [[ ${HEADERS_VERSION} > 4.17.9 ]]; then
        message "${MESSAGE_COLOR}xz -d $SOURCE_CACHE/$SOURCE7${DEFAULT_COLOR}" &&
        xz -d -k ${SOURCE_CACHE}/$SOURCE7 &&
        SOURCE7=${SOURCE7/.xz} &&
        verify_file 7 &&
        message "${MESSAGE_COLOR}rm $SOURCE_CACHE/$SOURCE7${DEFAULT_COLOR}" &&
        rm $SOURCE_CACHE/$SOURCE7 &&
        SOURCE7="${SOURCE7}.xz"
      else
        verify_file 7 || return 1
      fi
      xzcat ${SOURCE_CACHE}/$SOURCE7 > ${SOURCE7/.xz} &&
      message "${MESSAGE_COLOR}Patching ${SOURCE7/.xz}${DEFAULT_COLOR}" &&
      patch -p1 < ${SOURCE7/.xz} &&
      if [[ $? != 0 ]]; then
        message "${PROBLEM_COLOR}Patch ${SOURCE7/.xz} failed${DEFAULT_COLOR}" &&
        return 1
      fi
    fi &&
    make mrproper &&

#
# Remove kernel DRM headers
# Fixes Bug #15547
#
# [ -f "${GLIBC_HEADERS_DIR}/include/Kbuild" ] && sed -i '/^header-y += drm\//d' $GLIBC_HEADERS_DIR/include/Kbuild ||
# rm -rf $GLIBC_HEADERS_DIR/include/drm &&



#
# Now fixup the normal glibc
#
cd  $SOURCE_DIRECTORY                           &&
# no idea why or how this is missing from configure
# patch -p0 < $SPELL_DIRECTORY/as_fn_executable_p.patch &&

apply_patch_dir patches &&

# disabled libgd detection/building memusagestat for now until a better
# fix has been found, bug #8277
#sedit  's/LIBGD=yes/LIBGD=no/'  $SOURCE_DIRECTORY/configure  &&

#
# Create the build directory
#
mk_source_dir  $SOURCE_DIRECTORY.bld &&

# fix #11832 - linking in /tmp doesn't work with grsecurity so do it elsewhere
local test_path=$SOURCE_DIRECTORY.bld/test-installation &&
mkdir $test_path &&
sed -i "s,/tmp,$test_path,g" scripts/test-installation.pl &&
sed -i 's/ot \$/ot:\n\ttouch $@\n$/' manual/Makefile &&
# install x86_64 linker to lib instead of lib64
sed -i "s/264/2/" sysdeps/unix/sysv/linux/x86_64/ldd-rewrite.sed &&

# manual creation seems broken
sed -i 's/manual//' Makeconfig &&
rm -r manual
