# --- SDE-COPYRIGHT-NOTE-BEGIN ---
# This copyright note is auto-generated by ./scripts/Create-CopyPatch.
#
# Filename: package/.../musl/parse-config-9
# Copyright (C) 2012 - 2016 The OpenSDE Project
#
# More information can be found in the files COPYING and README.
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; version 2 of the License. A copy of the
# GNU General Public License can be found in the file COPYING.
# --- SDE-COPYRIGHT-NOTE-END ---

if [ "$SDECFG_LIBC" == "musl" ]; then
	pkg_musl_dir=$base/package/base/musl

	if [ $pkg != "gettext" ]; then
		var_append flistdel "|" "usr/share/locale/locale.alias"
	fi

	# I don't know what this is for //morfoh
	var_append flistdel "|" "$( echo $libdir | cut -d '/' -f2- )/charset.alias"

	# cross-compiling help
	if atstage cross; then
		# assuming a gnu compatible malloc, realloc
		var_append configcache ' ' 'ac_cv_func_malloc_0_nonnull=yes'
		var_append configcache ' ' 'ac_cv_func_realloc_0_nonnull=yes'
		var_append configcache ' ' 'ac_cv_header_sys_sysctl_h=no'
	fi

	# the most tipical fix is to config.sub
	#
	musl_fix_configsub() {
	local x; for x; do
		echo_warning "Adding linux-musl target support to $x"
		echo "musl_fix_configsub: adding musl_arch_target support to $x"
        	cp -f $x $x.orig
		sed -e 's,\([-]\?linux\)-gnu\*,\1-gnu\* | \1-musl\*,g' $x.orig > $x
		diff -u $x.orig $x || true
	done
	}
	musl_auto_fix_configsub() {
		local f
		for f in . ./build-aux ./config ./support; do
			if [ -e $f/config.sub ] ; then
				if ! grep -q '\-musl' $f/config.sub; then
				musl_fix_configsub $f/config.sub
				fi
			fi
		done
	}
	musl_findall_fix_configsub() {
		local f
		for f in `find . -name config.sub`; do
			musl_fix_configsub $f
		done
	}


	# FIXME: i forgot what makes musl_fix_addcases different than musl_fix_configsub //mnemoc
	musl_fix_addcases() {
		local file; for file; do
			echo "musl_fix_addcases: adding *-linux-musl* support to $file"
			cp -f $file $file.orig
			sed -i -e 's,\([^ ]*\)linux-gnu\*\(.*\)\([\)\\]\),\1linux-gnu* | \1linux-musl*\2\3,g' $file
			diff -u $file.orig $file || true
		done
	}

	musl_fix_addcases2() {
		local file; for file; do
			echo "musl_fix_addcases2: adding *-linux-musl* support to $file"
			cp -f $file $file.orig
			sed -i -e 's,\(  *.*\)linux-gnu\*)\(.*\)\;\;$,\1linux-musl*)\2;;\n\1linux-gnu*)\2;;,g' \
			       -e 's,\(  *.*\)linux-gnu\*)\(.*[^;][^;]\)$,\1linux-musl* \| \\\n\1linux-gnu*)\2,g' \
				$file
			diff -u $file.orig $file || true
		done
	}
	# fix typical locations
	hook_add postpatch 5 'musl_auto_fix_configsub'

	# fix issues with included gnulib
	# http://openwall.com/lists/musl/2012/06/19/13
	musl_fix_gnulib() {
		local dir="$1"

		echo_status "fixing gnulib weirdness in $dir"

		gl_config_cache="
			gl_cv_func_fflush_stdin=yes \
			gl_cv_func_fprintf_posix=yes \
			gl_cv_func_fpurge_works=yes \
			gl_cv_func_fseeko=yes \
			gl_cv_func_isnanl_works=yes \
			gl_cv_header_working_stdint_h=yes \
			gl_cv_func_fpurge_works=yes \
			gl_cv_func_fcntl_f_dupfd_cloexec=yes \
			gl_cv_func_getcwd_path_max=yes \
			gl_cv_func_fprintf_posix=yes \
			gl_cv_func_printf_sizes_c99=yes \
			gl_cv_func_printf_long_double=yes \
			gl_cv_func_printf_infinite=yes \
			gl_cv_func_printf_infinite_long_double=yes \
			gl_cv_func_printf_directive_a=yes \
			gl_cv_func_printf_directive_f=yes \
			gl_cv_func_printf_directive_n=yes \
			gl_cv_func_printf_directive_ls=yes \
			gl_cv_func_printf_positions=yes \
			gl_cv_func_printf_flag_grouping=yes \
			gl_cv_func_printf_flag_leftadjust=yes \
			gl_cv_func_printf_flag_zero=yes \
			gl_cv_func_printf_precision=yes \
			gl_cv_func_printf_enomem=yes"
		for x in $gl_config_cache; do
			var_append configcache ' ' "$x"
		done

		empty_file() {
			echo "gnulib fix: emptying file $1"
			rm -f $1
			touch $1
		}

		for x in fseterr.c freadahead.c fseeko.c; do
			empty_file "$dir/$x"
		done
		echo "void close_stdin(void) {}" > "$dir"/closein.c

		echo "#include <stdio_ext.h>" > "$dir"/fseterr.h
		echo "#define fseterr(fp) __fseterr (fp)" >> "$dir"/fseterr.h
		echo "#include "fseterr.h"" > "$dir"/fsetserr.c
	}

	#fix libiberty because we already have a libc, thanks...
	musl_fix_libiberty() {
		empty_file() {
			echo "libiberty fix: emptying file $1"
			rm -f $1
			touch $1
		}

		local dir="$1"
		echo_status "fixing libiberty weirdness in $dir"
		culprits="getcwd strtoul clock strncasecmp fnmatch waitpid \
			basename strchr snprintf rindex index memcpy mempcpy \
			strcasecmp strsignal vsprintf strndup bcmp tmpnam bzero \
			stpncpy memchr insque ffs vfork memmove stpcpy bsearch \
			copysign strtod vsnprintf strncmp strtol bcopy rename strstr \
			strerror putenv strdup memset memcmp vprintf calloc gettimeofday \
			atexit getpagesize strverscmp random setenv strrchr asprintf msdos \
			vasprintf vfprintf"
			
			for i in $culprits ; do empty_file "$dir"/$i.c ; done
	}

	# and some others
	case "$pkg" in
	binutils)
		hook_add postpatch 4 'musl_fix_addcases ./bfd/config.bfd ./bfd/configure \
			./ld/configure.tgt'
		hook_add postpatch 5 'musl_fix_addcases2 ./gas/configure'
		hook_add postpatch 6 'musl_fix_libiberty libiberty'
		;;
	gmp)
		hook_add postpatch 6 'musl_fix_configsub configfsf.sub'
		;;
	lzo|lzop|lvm2)
		hook_add postpatch 6 'musl_fix_configsub autoconf/config.sub'
		;;
	expat)
		hook_add postpatch 6 'musl_fix_configsub conftools/config.sub'
		;;
	bdb)
		hook_add postpatch 6 'musl_fix_configsub dist/config.sub'
		;;
	libtool)
		hook_add preconf 9 'musl_findall_fix_configsub'
		;;

	# packages to fix gnulib weirdness
	gzip|m4)
		hook_add preconf 9 'musl_fix_gnulib lib'
		;;	
	findutils)
		hook_add preconf 9 'musl_fix_gnulib gl/lib'
		;;
	gcc)
		hook_add postpatch 6 'musl_fix_libiberty libiberty'
		;;
	esac
	
	# same package translations to look for patches
	case "$pkg" in
		*)
			pkg_musl_pkg=${pkg} ;;
	esac

	if atstage toolchain; then
		pkg_musl_toolchain_patches=$( ls -1 $pkg_musl_dir/pkg/$pkg_musl_pkg/*.patch.toolchain 2> /dev/null | tr '\n' ' ' )
		if [ -n "$pkg_musl_toolchain_patches" ]; then
			echo_status "Appending musl libc specific toolchain patches"
			var_append patchfiles ' ' "$pkg_musl_toolchain_patches"
		fi
	fi

	pkg_musl_patches=$( ls -1 $pkg_musl_dir/pkg/$pkg_musl_pkg/*.patch 2> /dev/null | tr '\n' ' ' )
	if [ -n "$pkg_musl_patches" ]; then
		echo_status "Appending musl libc specific patches"
		var_append patchfiles ' ' "$pkg_musl_patches"
	fi

	if [ -f $pkg_musl_dir/pkg/$pkg/$pkg.conf ]; then
		. $pkg_musl_dir/pkg/$pkg/$pkg.conf
	fi
fi