# Contributor: Leonardo Arena <rnalrd@alpinelinux.org>
# Maintainer: Natanael Copa <ncopa@alpinelinux.org>
_php=php7
pkgname=roundcubemail
pkgver=1.3.3
pkgrel=0
pkgdesc="A PHP web-based mail client"
url="http://www.roundcube.net"
arch="noarch"
license="GPL-3.0-or-later"
install="$pkgname.post-upgrade"
depends="${_php} ${_php}-imap ${_php}-xml ${_php}-json ${_php}-dom
	${_php}-exif ${_php}-pear-net_idna2 ${_php}-pear-mail_mime
	${_php}-pear-net_smtp ${_php}-pear-auth_sasl ${_php}-openssl
	${_php}-session ${_php}-mbstring ${_php}-iconv ${_php}-intl
	"
options="!check"
subpackages="$pkgname-installer $pkgname-doc"
source="https://github.com/roundcube/$pkgname/releases/download/$pkgver/${pkgname}-$pkgver.tar.gz
	fix-dirs.patch"
builddir="$srcdir"/roundcubemail-$pkgver

# secfixes:
#   1.2.7-r0:
#     - CVE-2017-16651
#   1.2.5-r0:
#     - CVE-2017-8114

prepare() {
	cd "$builddir"
	default_prepare

	# fix permissions
	find . -type f -print | xargs chmod a-x
	# remove .htaccess
	find . -name \.htaccess -print | xargs rm -f

	# fixup paths to use the right paths
	sed -i -e 's|temp/|/tmp/|' \
		-e 's|config/|/etc/roundcube/|' \
		-e 's|logs/|/var/log/roundcube/|' \
		config/defaults.inc.php || return 1

	# cleanup
	sed -i 's/\r//' SQL/mssql.initial.sql
	rm -rf logs temp
}

build() {
	return 0
}

package() {
	_instdir="$pkgdir"/usr/share/webapps/roundcube
	mkdir -p "${_instdir}"
	cd "${_instdir}"
	cp -rp "$builddir"/* .
	# install config in /etc/roundcube so config files are not overwritten
	# on upgrades
	mkdir -p "$pkgdir"/etc/
	mv config "$pkgdir"/etc/roundcube

	install -d "$pkgdir"/var/log/roundcube
	mkdir -p "$pkgdir"/usr/share/doc/roundcube
	mkdir -p "$pkgdir"/usr/share/licenses/roundcube
	for file in CHANGELOG INSTALL README.md UPGRADING
	do
		mv "$pkgdir"/usr/share/webapps/roundcube/$file \
		  "$pkgdir"/usr/share/doc/roundcube || return 1
	done
	mv "$pkgdir"/usr/share/webapps/roundcube/LICENSE \
	  "$pkgdir"/usr/share/licenses/roundcube || return 1
}

installer() {
	pkgdesc="Roundcubemail installer script"
	mkdir -p "$subpkgdir"/usr/share/webapps/roundcube
	mv "$pkgdir"/usr/share/webapps/roundcube/installer \
		"$subpkgdir"/usr/share/webapps/roundcube
}

sha512sums="7cd1c5198e29a20145d70258e2792599f96383f142f0db7322fc348940e71c46c6961c8506b237463d78e6a1ce5c1bd51787f3659e24de9258270ea26b8133fd  roundcubemail-1.3.3.tar.gz
5c645ab7f130f8f3b17b7821e2cd0be88b6d7999da38876bde36c9b116bf7d34bcf52e2d8939d9359649e3fc5ddfb1cab7798ffb9e649be11bebd1d2c4ee006b  fix-dirs.patch"
