# Contributor: <kalonji@gmail.com>
# Contributor: Jakub Jirutka <jakub@jirutka.cz>
# Maintainer: Natanael Copa <ncopa@alpinelinux.org>
pkgname=zsh
pkgver=5.2
pkgrel=3
pkgdesc="A very advanced and programmable command interpreter (shell)"
url="http://www.zsh.org/"
arch="all"
license="BSD"
depends=""
makedepends="ncurses-dev"
install="zsh.post-install zsh.post-upgrade zsh.pre-deinstall"
source="http://www.zsh.org/pub/$pkgname-$pkgver.tar.xz
	zprofile
	fix-zle-segfault.patch
	fix-oom-fatal-error.patch
	fix-vcs_info.patch"
subpackages="$pkgname-doc $pkgname-calendar::noarch $pkgname-vcs::noarch $pkgname-zftp::noarch"
builddir="$srcdir/$pkgname-$pkgver"

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

# Move some bigger completion files to subpackages.
_comps="android-tools:Unix/_adb
	bzr:Unix/_bzr
	cvs:Unix/_cvs
	gcc:Unix/_gcc
	git:Unix/_git
	graphicsmagick:Unix/_graphicsmagick
	imagemagick:Unix/_imagemagick
	lynx:Unix/_lynx
	mercurial:Unix/_hg
	rsync:Unix/_rsync
	subversion:Unix/_subversion
	tmux:Unix/_tmux
	zfs:Unix/_zfs*:Unix/_zpool"
for _i in $_comps; do
	subpackages="$subpackages ${_i%%:*}-zsh-completion:_completion"
done

prepare() {
	cd "$builddir"

	default_prepare || return 1
	update_config_sub || return 1

	# Remove completions for other systems.
	cd 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 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 _yodl
	cd ../../Linux/Command
	rm -f _acpitool _mondo _tpb _tpconfig _uml _vserver
	cd ../../X/Command
	rm -f _acroread _dcop _gnome-gv _gqview _gv _kfmclient _matlab \
		_nautilus _netscape _okular _qiv _vnc _xfig _xloadimage \
		_xournal _xv _xwit
}

build() {
	cd "$builddir"

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

package() {
	cd "$builddir"

	make DESTDIR="$pkgdir" install || return 1
	install -Dm644 "$srcdir"/zprofile "$pkgdir"/etc/zsh/zprofile
}

doc() {
	default_doc || return 1
	_submv $_sharedir/help
}

calendar() {
	pkgdesc="Calendar Function System for ZSH"
	depends="$pkgname"

	_submv $_sharedir/functions/Calendar
}

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

	_submv $_sharedir/functions/VCS_Info
}

zftp() {
	pkgdesc="Zftp Function System for ZSH"
	depends="$pkgname"

	_submv $_libdir/zsh/zftp.so || return 1
	_submv $_sharedir/functions/Zftp
}

_completion() {
	local name="${subpkgname%-zsh-completion}"
	pkgdesc="Zsh completions for $name"
	depends="$pkgname"
	install_if="$pkgname=$pkgver-r$pkgrel $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
		_submv $_sharedir/functions/Completion/$f || return 1
	done
}

_submv() {
	local path="$1"
	mkdir -p "$subpkgdir"/${path%/*}
	mv "$pkgdir"/$path "$subpkgdir"/${path%/*}/
}

md5sums="afe96fde08b70e23c1cab1ca7a68fb34  zsh-5.2.tar.xz
a5e93cd6880da04a63fc4fcaaa03987f  zprofile
1413688affa980b053dd2cdc784f8626  fix-zle-segfault.patch
e928fc47e21e8d6fed9008b397d4f2f7  fix-oom-fatal-error.patch
0220f95cc0bceefdc13ebf994716ad9d  fix-vcs_info.patch"
sha256sums="f17916320ffaa844bbd7ce48ceeb5945fc5f3eff64b149b4229bbfbdf3795a9d  zsh-5.2.tar.xz
e3c3625d966c8277274798f26241857df5fc520e18674deba6cc356f6b022d63  zprofile
7b8bfec6f7c0df6954eb27a000ff1d077e37a1ec9fd9f37c247a53922cf0c9be  fix-zle-segfault.patch
08285e5794b72be52adf891673342ad3a0faca6912792bff120c81229c80d29e  fix-oom-fatal-error.patch
ab5557588a0530f1ed423f5b0fae1445465abeac4eb7820f7df8604b456a1d39  fix-vcs_info.patch"
sha512sums="9938af95e09203a0a313f441e0c23279136806f6f087475ca9a66c678c57ecffc87f73ece8133a36700af8de7cb5d1bec8559eabdec2b66102062e64026c2e72  zsh-5.2.tar.xz
59182b99447872ded8adf0d890e9359ee47fce0b7acb2808f4308f945885fbf6d977a0917bbb5c0f21454caf3ba06ab092127732da4f84292d6ab0989a0110fe  zprofile
f17016b4cb95932caabdfca273696f4a444f1e45c8e7d458db54ae12542fce87bdd6fc6c4cd070948041f50760389179c69c12296665b7b9651007faafbdbee7  fix-zle-segfault.patch
3a6060af73e3aa29bf3606fb8d7c8c68080f83a7e9d0b5614356ef0981773d836bbb6abfc7c3142e5037dd9c36a688ccdb44b81bbca5206c67ae3d8e1b6b23a6  fix-oom-fatal-error.patch
addf598f1f380f08fd5682efedc493bd37f7a3c606da74ad87efe18906dcecfad7f19fdfe1b9a03bf2dc8d265d2738fd43f3d0fae78767837699c5614a5369bb  fix-vcs_info.patch"
