# SliTaz package receipt.

PACKAGE="bash"
VERSION="5.1.16"
BASEVERSION="${VERSION%.*}"
CATEGORY="system-tools"
TAGS="shell"
SHORT_DESC="The GNU bourne SHell."
MAINTAINER="pankso@slitaz.org"
LICENSE="GPL3"
WEB_SITE="https://www.gnu.org/software/bash/"

TARBALL="$PACKAGE-$BASEVERSION.tar.gz"
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"

# What is the latest version available today?
current_version()
{
	wget -O - ${WGET_URL%/*} 2>/dev/null | \
	sed "/alpha/d;/doc/d;/$PACKAGE-/!d;/tar/!d;s|.*$PACKAGE-\\(.*\\).tar.*\".*|\\1|" | sort -Vr | sed q
}

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

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

# Rules to configure and make the package.
compile_rules()
{
	# Patch and then build.
	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					\
		--cache-file=config.cache		\
		--bindir=/bin				\
		--enable-history			\
		--enable-alias				\
		--disable-nls				\
		--without-bash-malloc			\
		--disable-help-builtin			\
		--with-installed-readline=no		\
		$CONFIGURE_ARGS &&
	make &&
	# Bash doesn't care about DESTDIR in environment variable.
	make install DESTDIR=$DESTDIR
}

# Rules to gen a SliTaz package suitable for Tazpkg.
genpkg_rules()
{
	mkdir $fs/etc

	cook_copy_folders		bin

	# Configuration files.
	cp $stuff/example.bashrc	$fs/etc/bashrc
}

testsuite()
{
	[ -s "$SOURCES_REPOSITORY/$BASHCHECK" ] ||
	busybox 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"
}

post_install()
{
	mkdir -p "$1/etc/skel"
	cp -a "$1/etc/bashrc"	"$1/etc/skel/.bashrc"

	# Exit function in non-interactive mode (when user can't answer question)
	! tty -s && return

	echo
	echo -n 'Do you want to set Bash to default (y/N) ? : '
	read -t 30 answer
	[ "$answer" != 'y' ] &&
	return

	echo
	echo 'Setting bash as default shell for all users'
	sed -i 's|:/bin/[ckz]*sh$|:/bin/bash|'	"$1/etc/passwd"

	echo 'Creating ~/.bashrc file for all users'
	for i in $(awk -F: '$7=="/bin/bash"{print $6}' "$1/etc/passwd")
	  do
		[ -e "$1$i/.bashrc" ] && continue
		cp "$1/etc/bashrc"		"$1$i/.bashrc"
		chown $(stat -c%U:%G "$1$i")	"$1$i/.bashrc"
	  done
}


pre_remove()
{
	echo
	echo 'Setting sh as default shell for all users'
	sed -i 's|:/bin/bash$|:/bin/sh|'	"$1/etc/passwd"
	[ -f "$1/etc/skel/.bashrc" ] &&
	rm "$1/etc/skel/.bashrc"
}