# Contributor: <kalonji@gmail.com>
# Contributor: Jakub Jirutka <jakub@jirutka.cz>
# Maintainer: Natanael Copa <ncopa@alpinelinux.org>
#
# secfixes:
#   5.8.1-r0:
#     - CVE-2021-45444
#   5.8-r0:
#     - CVE-2019-20044
#   5.4.2-r1:
#     - CVE-2018-1083
#     - CVE-2018-1071
#
pkgname=zsh
pkgver=5.9
pkgrel=5
pkgdesc="Very advanced and programmable command interpreter (shell)"
url="https://www.zsh.org/"
arch="all"
# GPL-2.0-only: license of Completion/Linux/Command/_qdbus
license="MIT-Modern-Variant AND GPL-2.0-only"
makedepends="
	autoconf
	diffutils
	libcap-dev
	pcre-dev
	ncurses-dev
	"
install="
	$pkgname.post-install
	$pkgname.post-upgrade
	$pkgname.pre-deinstall
	"
subpackages="
	$pkgname-doc
	$pkgname-calendar::noarch
	$pkgname-pcre
	$pkgname-vcs::noarch
	$pkgname-zftp
	"
source="https://sourceforge.net/projects/zsh/files/zsh/$pkgver/zsh-$pkgver.tar.xz
	skip-test-failing-on-musl.patch
	zsh-newuser-install-alpine.patch
	fix-gcc14-incompatible-pointer-types.patch
	implicit.patch
	0001-50278-use-man-w-in-preference-to-manpath-fix-caching.patch
	zprofile
	zshenv
	zshrc
	"

_libdir="usr/lib/zsh/$pkgver"
_sharedir="usr/share/zsh/$pkgver"

# Move some bigger completion files to subpackages.
# <pkgname-prefix>:[<revdep>]:<file1>[:<file2>...]
_comps="android-tools::Unix/_adb
	bzr::Unix/_bzr
	composer::Unix/_composer
	cvs::Unix/_cvs
	gcc::Unix/_gcc
	git::Unix/_git
	graphicsmagick::Unix/_graphicsmagick
	imagemagick::Unix/_imagemagick
	lynx::Unix/_lynx
	postgresql:postgresql-client:Unix/_postgresql
	rsync::Unix/_rsync
	subversion::Unix/_subversion
	tmux::Unix/_tmux
	zfs::Unix/_zfs*
	"
for _i in $_comps; do
	subpackages="$subpackages ${_i%%:*}-zsh-completion:_completion:noarch"
done

prepare() {
	default_prepare
	update_config_sub

	# Remove completions for other systems.
	cd "$builddir/Completion"
	rm -Rf AIX BSD Cygwin Darwin Debian Mandriva Redhat Solaris openSUSE

	# Remove completions for programs that are not available on Alpine
	# (just to decrease size of the package).
	cd "$builddir/Completion/Unix/Command"
	rm -f _aap _apm _baz _bittorrent _bpython _ccal _cdcd _chkconfig _clay \
		_cowsay _cplay _cssh _darcs _devtodo _dict _dsh _elfdump _elm \
		_enscript _finger _flasher _fsh _gnupod _guilt _initctl _lzop \
		_mencal _module _monotone _moosic _mysqldiff _nkf \
		_pack _pax _perforce _pine _pkgadd _pkginfo _pkgrm _prcs \
		_quilt _raggle _rcs _rlogin _rubber _sablotron _sisu _socket \
		_stgit _surfraw _tardy _telnet _tin _tla _topgit _totd _twidge \
		_unace _unison _units _uzbl _vcsh _vux _wiggle _xmms2
	cd "$builddir/Completion/Linux/Command"
	rm -f _acpitool _mondo _tpb _tpconfig _uml _vserver
	cd "$builddir/Completion/X/Command"
	rm -f _acroread _dcop _gnome-gv _gqview _gv _kfmclient _matlab \
		_nautilus _netscape _okular _qiv _vnc _xfig _xloadimage \
		_xournal _xv _xwit

	# remove the failing test suites
	cd "$builddir/Test"
	# SPLATTER: applet not found
	rm -f A01grammar.ztst
	# [[ $(strftime '%@' 0 2> /dev/null) == (%|)@ || $? != 0 ]]
	rm -f V09datetime.ztst

}

build() {
	./configure \
		--build=$CBUILD \
		--host=$CHOST \
		--prefix=/usr \
		--bindir=/bin \
		--enable-etcdir=/etc/zsh \
		--enable-pcre \
		--enable-cap \
		--enable-multibyte \
		--enable-function-subdirs \
		--enable-zsh-secure-free \
		--sysconfdir=/etc \
		--with-tcsetpgrp \
		--mandir=/usr/share/man \
		--infodir=/usr/share/info
	make
}

check() {
	# See: https://www.zsh.org/mla/workers/2021/msg00805.html
	unset LC_ALL
	unset LC_COLLATE
	unset LC_NUMERIC
	unset LC_MESSAGES
	unset LANG

	make test
}

package() {
	make DESTDIR="$pkgdir" install

	install -Dm644 LICENCE "$pkgdir"/usr/share/licenses/$pkgname/LICENCE

	install -d "$pkgdir"/usr/share/zsh/plugins
	install -d "$pkgdir"/etc/zsh/zshrc.d

	cd "$srcdir"
	install -Dm644 -t "$pkgdir"/etc/zsh/ \
		zprofile \
		zshenv \
		zshrc
}

doc() {
	default_doc
	amove $_sharedir/help
}

calendar() {
	pkgdesc="Calendar Function System for ZSH"
	depends="$pkgname=$pkgver-r$pkgrel"

	amove $_sharedir/functions/Calendar
}

pcre() {
	pkgdesc="PCRE module for ZSH"
	depends="$pkgname=$pkgver-r$pkgrel"
	install_if="$pkgname=$pkgver-r$pkgrel pcre"

	amove $_libdir/zsh/pcre.so
}

vcs() {
	pkgdesc="Version Control Information module for ZSH (vcs_info)"
	depends="$pkgname=$pkgver-r$pkgrel"

	amove $_sharedir/functions/VCS_Info
}

zftp() {
	pkgdesc="Zftp Function System for ZSH"
	depends="$pkgname=$pkgver-r$pkgrel"

	amove $_libdir/zsh/zftp.so
	amove $_sharedir/functions/Zftp
}

_completion() {
	local name="${subpkgname%-zsh-completion}"
	pkgdesc="Zsh completions for $name"
	depends="$pkgname"

	local revdep="$(printf '%s\n' $_comps \
		| sed -En "s|^$name:([^:]*):.*|\1|p")"

	install_if="$pkgname=$pkgver-r$pkgrel ${revdep:-$name}"

	local files="$(printf '%s\n' $_comps \
		| sed -En "s|^$name:[^:]*:(.*)|\1|p" | tr : ' ')"
	test -n "$files" || { echo "$name not found in \$_comps" >&2; return 1; }

	local f; for f in $files; do
		amove $_sharedir/functions/Completion/$f
	done
}

sha512sums="
d9138b7f379ad942a5f46819d2dd52d31f3a1129f2a0d1b53d4c5cd43c318b60396da6d37c57c477b8e958fb750209aca0ae93f8c9dd42ac958de006a0ff067e  zsh-5.9.tar.xz
4fb6bf20f27ff4b36c5519dc0f7ac43b0cbf299ab0525c0f49920ed848166367207a58160eaa8d573e7ed0373540bde7a5363386e41a36945deba016a255a61d  skip-test-failing-on-musl.patch
3be36a30af2c33c5d4d0412acd30fbb52fbaad49c692f12ab1a9c57d87cee62ad0b4737127585e2dc5824b98428ff097977f610a4d1fa3d83c9370b7cf2956f7  zsh-newuser-install-alpine.patch
905562600f157ae745d837a9c2e2790dc373ee151b9b05c50e59862473db20f356e7aea528452fbfb1ddf2e3b985f6959b7850884c3218ece6232a679f210997  fix-gcc14-incompatible-pointer-types.patch
0fd193a92c24dfd3b97f6294469cad8c799c91c27bb1f298bb64dcd10982543e80ab9e63517915ff648096e85105044c9d2d4368977266f7e3f6b34e69277768  implicit.patch
0cda29109164fb798f26cc6c6492915569373c8316a8bf10979ec1d8551495dd89978a2231190be29aab4ab6dd90a8fe0c236ffcc82e11f98165328e0a387a3f  0001-50278-use-man-w-in-preference-to-manpath-fix-caching.patch
66a6bdf8d915c818a2c31c35c90c2a19f91d15e2d8547d050e980ef63080f27033ef1f59da5befb30dbf46050ce907690a680e48db11711d40930420f7ec1c2b  zprofile
e1c40649c940b91fb6dbcf9e7878e3c96d3729c8eee7b97cfe007d14d19add500863cef588ea4818ab83921fef9f9b4384c2003b26e9d753a8d59e9aca837d43  zshenv
a7f4f5267cf0a9d1e0c480e89ede0811435803e4822b522dfc0326a9015a44af5e3163160b5595dc055acfca28f56811521c728e020389c7a5e4e0e4b3811d38  zshrc
"
