# SliTaz package receipt.

PACKAGE="bash"
VERSION="5.1.16"
BASEVERSION="${VERSION%.*}"
CATEGORY="system-tools"
SHORT_DESC="The GNU bourne SHell."
MAINTAINER="pankso@slitaz.org"
LICENSE="GPL3"
TARBALL="$PACKAGE-$BASEVERSION.tar.gz"
WEB_SITE="http://www.gnu.org/software/bash/"
WGET_URL="$GNU_MIRROR/$PACKAGE/$TARBALL"
BASHCHECKURL="https://raw.githubusercontent.com/hannob/bashcheck/master/bashcheck"
BASHCHECK="$(basename $BASHCHECKURL)"
BASHPATCHES="$(for i in $(seq 1 ${VERSION##*.}); do printf "bash${BASEVERSION/./}-%03d " $i; done)"
EXTRA_SOURCE_FILES="$BASHCHECK $BASHPATCHES"

DEPENDS="libtinfo"
BUILD_DEPENDS=" bison texinfo"

patch_bash()
{
	for i in $BASHPATCHES ; do
		[ -s "$SOURCES_REPOSITORY/$i" ] || 
			wget -O "$SOURCES_REPOSITORY/$i" \
			http://ftp.gnu.org/gnu/bash/bash-$BASEVERSION-patches/$i
		echo "Apply $i ..."
		patch -p0 < $SOURCES_REPOSITORY/$i
	done
}

# Rules to compile & install the temporary toolchain.
cook_tmp_toolchain()
{
	cd $src
	patch_bash
	./configure --without-bash-malloc &&
	make && make install
}

# Rules to configure and make the package.
compile_rules()
{
	# Patch and then build.
	cd $src
	patch_bash
	# Skip tests that can not run while cross-compiling.
	cat > config.cache << "EOF"
ac_cv_func_mmap_fixed_mapped=yes
ac_cv_func_strcoll_works=yes
ac_cv_func_working_mktime=yes
bash_cv_func_sigsetjmp=present
bash_cv_getcwd_malloc=yes
bash_cv_job_control_missing=present
bash_cv_printf_a_format=yes
bash_cv_sys_named_pipes=present
bash_cv_ulimit_maxfds=yes
bash_cv_under_sys_siglist=yes
bash_cv_unusable_rtsigs=no
gt_cv_int_divbyzero_sigfpe=yes
EOF
	./configure \
		$CONFIGURE_ARGS \
		--cache-file=config.cache \
		--bindir=/bin \
		--enable-history --enable-alias  \
		--disable-nls --without-bash-malloc \
		--disable-help-builtin \
		--with-installed-readline &&
	make &&
	# Bash doesn't care about DESTDIR in environnment variable.
	make DESTDIR=$DESTDIR install
	[ -s "$SOURCES_REPOSITORY/$BASHCHECK" ] || 
		wget --no-check-certificate -O "$SOURCES_REPOSITORY/$BASHCHECK" $BASHCHECKURL
	$DESTDIR/bin/bash $SOURCES_REPOSITORY/$BASHCHECK $DESTDIR/bin/bash | tee bashcheck.log
	grep Vulnerable bashcheck.log && echo "ERROR: Vulnerable"
}

# Rules to gen a SliTaz package suitable for Tazpkg.
genpkg_rules()
{
	cp -a $_pkg/bin $fs
	# Config files.
	#
	mkdir $fs/etc
	cp $stuff/example.bashrc $fs/etc/bashrc
}

# Post install commands for Tazpkg.
# Check /bin/sh stat.
#
post_install()
{
	local root
	root=$1
	echo "Processing post-install commands..."
	sh=`readlink $root/bin/sh`
	if [ ! "$sh" = "/bin/bash" ]; then
		echo ""
		echo "**** Actual SH link : $sh"
		echo ""
		echo -n "Do you want Bash for /bin/sh (yes/No) ? : "
		read -t 30 anser
		if [ "$anser" == "yes" ]; then
			echo ""
			echo -n "Removing sh link to make a new one pointing on /bin/bash..."
			rm $root/bin/sh && ln -s /bin/bash $root/bin/sh
			status
		else
			echo ""
			echo "Leaving /bin/sh to : $sh"
		fi
	fi
}

# Restore a valid sh link if needed.
pre_remove()
{
	local root
	root=$1
	sh=`readlink $root/bin/sh`
	if [ ! "$sh" = "busybox" ]; then
		rm $root/bin/sh &&
		ln -s /bin/busybox $root/bin/sh
	fi
}