# Template file for 'php8.1'
# rebuild php8.1-apcu and php8.1-imagick when updating this package
pkgname=php8.1
version=8.1.31
revision=1
_php_version=8.1
hostmakedepends="bison pkg-config apache-devel autoconf"
makedepends="apache-devel enchant2-devel freetds-devel freetype-devel gdbm-devel
 gmp-devel libcurl-devel libjpeg-turbo-devel libmariadbclient-devel
 libsodium-devel libtidy5-devel libxslt-devel libzip-devel net-snmp-devel
 postgresql-libs-devel readline-devel sqlite-devel unixodbc-devel pcre2-devel
 libffi-devel oniguruma-devel gd-devel acl-devel"
short_desc="HTML-embedded scripting language"
maintainer="Joel Beckmeyer <joel@beckmeyer.us>"
license="PHP-3.01"
homepage="https://www.php.net"
changelog="https://raw.githubusercontent.com/php/php-src/php-${version}/NEWS"
# this is the source where the www.php.net code pulls the tarballs it serves
# at https://www.php.net/distributions/
distfiles="https://github.com/php/web-php-distributions/raw/master/php-${version}.tar.gz"
checksum=618923b407c4575bfee085f00c4aaa16a5cc86d4b1eb893c0f352d61541bbfb1
provides="php-runtime-${version}_1"

system_accounts="_phpfpm"
_phpfpm_homedir="/var/empty"

conf_files="/etc/php${_php_version}/php.ini"

lib32disabled=yes

if [ "$XBPS_TARGET_LIBC" = "glibc" ]; then
	makedepends+=" libxcrypt-devel"
fi

if [ -n "$CROSS_BUILD" ]; then
	# phar and pear need php to build
	hostmakedepends+=" php${_php_version}"
	CFLAGS+=" -DHAVE_LIBDL
		 -DHAVE_DLOPEN
		 -DHAVE_DLSYM
		 -DHAVE_SHM_IPC
		 -DHAVE_SHM_MMAP_ANON
		 -DHAVE_SHM_MMAP_ZERO
		 -DHAVE_SHM_MMAP_POSIX
		 -DHAVE_SHM_MMAP_FILE"
fi

alternatives="
 php:phar.phar:/usr/bin/phar${_php_version}.phar
 php:php:/usr/bin/php${_php_version}
 php:phar:/usr/bin/phar${_php_version}
 php:phar.1:/usr/share/man/man1/phar${_php_version}.1
 php:phar.phar.1:/usr/share/man/man1/phar${_php_version}.phar.1
 php:php.1:/usr/share/man/man1/php${_php_version}.1"

do_configure() {
	autoconf -f
}

do_build() {
	local _phpconfig="--srcdir=.. \
		--config-cache \
		--prefix=/usr \
		--sbindir=/usr/bin \
		--sysconfdir=/etc/php${_php_version} \
		--localstatedir=/var \
		--with-layout=GNU \
		--with-config-file-path=/etc/php${_php_version} \
		--with-config-file-scan-dir=/etc/php${_php_version}/conf.d \
		--disable-rpath \
		--mandir=/usr/share/man \
		--libdir=/usr/lib/php${_php_version} \
		--datarootdir=/usr/share/php${_php_version} \
		--datadir=/usr/share/php${_php_version} \
		--program-suffix=${_php_version} \
		--includedir=/usr/include/php${_php_version} \
		"

	local _phpextensions="--enable-bcmath=shared \
		--enable-calendar=shared \
		--enable-exif=shared \
		--enable-ftp=shared \
		--enable-intl=shared \
		--enable-mbstring \
		--enable-opcache \
		--enable-pcntl \
		--enable-phar=shared \
		--enable-posix=shared \
		--enable-shmop=shared \
		--enable-soap=shared \
		--enable-sockets=shared \
		--enable-sysvmsg=shared \
		--enable-sysvsem=shared \
		--enable-sysvshm=shared \
		--enable-mysqlnd \
		--enable-gd=shared \
		--with-external-gd \
		--with-ffi=shared \
		--with-bz2=shared,$XBPS_CROSS_BASE/usr/ \
		--with-curl=shared \
		--with-db4=$XBPS_CROSS_BASE/usr \
		--with-enchant=shared,$XBPS_CROSS_BASE/usr \
		--with-gdbm=$XBPS_CROSS_BASE/usr \
		--with-gettext=shared \
		--with-gmp=shared \
		--with-iconv=shared \
		--with-ldap=shared,$XBPS_CROSS_BASE/usr \
		--with-ldap-sasl \
		--with-sodium=shared \
		--with-mhash \
		--with-mysql-sock=/run/mysqld/mysqld.sock \
		--with-mysqli=shared,mysqlnd \
		--with-openssl=shared \
		--with-external-pcre \
		--with-pear=/usr/share/pear${_php_version} \
		--with-pdo-mysql=shared,mysqlnd \
		--with-pdo-odbc=shared,unixODBC,$XBPS_CROSS_BASE/usr \
		--with-pdo-pgsql=shared,$XBPS_CROSS_BASE/usr \
		--with-pdo-sqlite=shared,$XBPS_CROSS_BASE/usr \
		--with-pgsql=shared,$XBPS_CROSS_BASE/usr \
		--with-readline=$XBPS_CROSS_BASE/usr \
		--with-snmp=shared,$XBPS_CROSS_BASE/usr \
		--with-sqlite3=shared,$XBPS_CROSS_BASE/usr \
		--with-unixODBC=shared,$XBPS_CROSS_BASE/usr \
		--with-xsl=shared,$XBPS_CROSS_BASE/usr \
		--with-tidy=shared,$XBPS_CROSS_BASE/usr \
		--with-zip=shared \
		--with-zlib \
		${configure_args} \
		"
	if [ -n "$CROSS_BUILD" ]; then
		local _make_env="PHAR_PHP=/usr/bin/php${_php_version} PHAR_PHP_MODULES=/usr/lib/php${_php_version}/modules"
	fi

	EXTENSION_DIR="/usr/lib/php${_php_version}/modules"
	export EXTENSION_DIR
	PEAR_INSTALLDIR=/usr/share/pear${_php_version}
	export PEAR_INSTALLDIR
	EXTRA_LIBS="-ldl"
	export EXTRA_LIBS

	# cgi,cli,embed,fcgi,fpm
	mkdir -p build
	cd build
	ln -s ${wrksrc}/configure
	./configure ${_phpconfig} \
		--host=${XBPS_CROSS_TRIPLET} \
		--enable-cgi \
		--enable-fpm \
		--with-fpm-user=_phpfpm \
		--with-fpm-group=_phpfpm \
		--with-fpm-acl \
		--enable-embed=shared \
		${_phpextensions}
	make ${makejobs} ${_make_env}

	# apache
	# reuse the previous run; this will save us a lot of time
	cp -a ${wrksrc}/build ${wrksrc}/build-apache
	cd ${wrksrc}/build-apache
	./configure ${_phpconfig} \
		--host=${XBPS_CROSS_TRIPLET} \
		--with-apxs2 \
		${_phpextensions}
	make ${makejobs} ${_make_env}
}

do_install() {
	cd ${wrksrc}/build
	local _env="INSTALL_ROOT=${DESTDIR}"
	if [ -n "$CROSS_BUILD" ]; then
		_env+=" PHAR_PHP=/usr/bin/php${_php_version}"
	fi
	make ${_env} install-{modules,cli,build,headers,programs,pharcmd}

	# install php.ini
	vinstall ${wrksrc}/php.ini-production 644 "etc/php${_php_version}" php.ini
	# remove static modules
	rm -f "${DESTDIR}/usr/lib/php${_php_version}/modules/*.a"
}

php8.1-devel_package() {
	short_desc+=" - development files"
	pkg_install() {
		vmove usr/include
		vmove "usr/lib/php${_php_version}/build"
		vmove "usr/bin/php-config${_php_version}"
		vmove "usr/bin/phpize${_php_version}"
		vmove "usr/share/man/man1/phpize${_php_version}.1"
		vmove "usr/share/man/man1/php-config${_php_version}.1"
	}
}

php8.1-phpdbg_package() {
	short_desc+=" - interactive debugger"
	depends="php${_php_version}>=${version}_${revision}"
	pkg_install() {
		cd ${wrksrc}/build
		make INSTALL_ROOT=${PKGDESTDIR} install-phpdbg
	}
}

php8.1-cgi_package() {
	short_desc+=' - CGI and FCGI SAPI'
	depends="php${_php_version}>=${version}_${revision}"
	pkg_install() {
		cd ${wrksrc}/build
		make INSTALL_ROOT=${PKGDESTDIR} install-cgi
	}
}

php8.1-apache_package() {
	short_desc+=' - Apache SAPI'
	depends="php${_php_version}>=${version}_${revision}"
	conf_files="/etc/apache/extra/php${_php_version}_module.conf"
	alternatives="
	 php-apache:libphp.so:/usr/libexec/httpd/modules/libphp${_php_version}.so
	 php-apache:php_module.conf:/etc/apache/extra/php${_php_version}_module.conf"
	pkg_install() {
		vinstall ${wrksrc}/build-apache/libs/libphp.so 755 usr/libexec/httpd/modules "libphp${_php_version}.so"
		vinstall ${FILESDIR}/apache.conf 644 etc/apache/extra "php${_php_version}_module.conf"
	}
}

php8.1-fpm_package() {
	short_desc+=' - FastCGI Process Manager'
	depends="php${_php_version}>=${version}_${revision}"
	conf_files="/etc/php${_php_version}/php-fpm.conf /etc/php${_php_version}/php-fpm.d/*"
	pkg_install() {
		cd ${wrksrc}/build
		make INSTALL_ROOT=${PKGDESTDIR} install-fpm
		vsv "php-fpm${_php_version}"
	}
}

php8.1-embed_package() {
	lib32disabled=yes
	depends="php${_php_version}>=${version}_${revision}"
	short_desc+=' - Embed SAPI'
	pkg_install() {
		cd ${wrksrc}/build
		make INSTALL_ROOT=${PKGDESTDIR} PHP_SAPI=embed install-sapi
		mv ${PKGDESTDIR}/usr/lib/libphp.so ${PKGDESTDIR}/usr/lib/libphp${_php_version}.so
	}
}

php8.1-pear_package() {
	lib32disabled=yes
	depends="php${_php_version}>=${version}_${revision}
	 php${_php_version}>=devel-${version}_${revision} autoconf"
	short_desc+=' - PHP Extension and Application Repository'
	conf_files="/etc/php${_php_version}/pear.conf"
	pkg_install() {
		cd ${wrksrc}/build
		local _env="INSTALL_ROOT=${PKGDESTDIR} PHP_PEAR_PHP_BIN=php${_php_version}"
		if [ -n "$CROSS_BUILD" ]; then
			_env+=" PEAR_PHP=/usr/bin/php${_php_version}"
		fi
		make install-pear ${_env}
		rm -rf ${PKGDESTDIR}/.{channels,depdb,depdblock,filemap,lock,registry}
		rm -rf ${PKGDESTDIR}/usr/share/pear/.{channels,depdb,depdblock,filemap,lock,registry}
		mv ${PKGDESTDIR}/usr/bin/pear{,${_php_version}}
		mv ${PKGDESTDIR}/usr/bin/peardev{,${_php_version}}
		mv ${PKGDESTDIR}/usr/bin/pecl{,${_php_version}}
	}
}

php8.1-enchant_package() {
	lib32disabled=yes
	depends="php${_php_version}>=${version}_${revision}"
	short_desc+=' - enchant module'
	pkg_install() {
		vmove "usr/lib/php${_php_version}/modules/enchant.so"
	}
}

php8.1-gd_package() {
	lib32disabled=yes
	depends="php${_php_version}>=${version}_${revision}"
	short_desc+=' - gd module'
	pkg_install() {
		vmove "usr/lib/php${_php_version}/modules/gd.so"
	}
}

php8.1-intl_package() {
	lib32disabled=yes
	depends="php${_php_version}>=${version}_${revision}"
	short_desc+=' - Internationalization module'
	pkg_install() {
		vmove "usr/lib/php${_php_version}/modules/intl.so"
	}
}

php8.1-ldap_package() {
	lib32disabled=yes
	depends="php${_php_version}>=${version}_${revision}"
	short_desc+=' - LDAP module'
	pkg_install() {
		vmove "usr/lib/php${_php_version}/modules/ldap.so"
	}
}

php8.1-mysql_package() {
	lib32disabled=yes
	depends="php${_php_version}>=${version}_${revision}"
	short_desc+=' - MySQL modules'
	pkg_install() {
		vmove "usr/lib/php${_php_version}/modules/*mysql*.so"
	}
}

php8.1-odbc_package() {
	lib32disabled=yes
	depends="php${_php_version}>=${version}_${revision}"
	short_desc+=' - ODBC modules'
	pkg_install() {
		vmove "usr/lib/php${_php_version}/modules/*odbc.so"
	}
}

php8.1-pgsql_package() {
	lib32disabled=yes
	depends="php${_php_version}>=${version}_${revision}"
	short_desc+=' - PostgreSQL modules'
	pkg_install() {
		vmove "usr/lib/php${_php_version}/modules/*pgsql.so"
	}
}

php8.1-snmp_package() {
	lib32disabled=yes
	depends="php${_php_version}>=${version}_${revision}"
	short_desc+=' - snmp module'
	pkg_install() {
		vmove "usr/lib/php${_php_version}/modules/snmp.so"
	}
}

php8.1-sqlite_package() {
	lib32disabled=yes
	depends="php${_php_version}>=${version}_${revision}"
	short_desc+=' - sqlite module'
	pkg_install() {
		vmove "usr/lib/php${_php_version}/modules/*sqlite*.so"
	}
}

php8.1-tidy_package() {
	lib32disabled=yes
	depends="php${_php_version}>=${version}_${revision}"
	short_desc+=' - tidy HTML module'
	pkg_install() {
		vmove "usr/lib/php${_php_version}/modules/*tidy*.so"
	}
}

php8.1-xsl_package() {
	lib32disabled=yes
	depends="php${_php_version}>=${version}_${revision}"
	short_desc+=' - XSL module'
	pkg_install() {
		vmove "usr/lib/php${_php_version}/modules/xsl.so"
	}
}

php8.1-sodium_package() {
	lib32disabled=yes
	depends="php${_php_version}>=${version}_${revision}"
	short_desc+=' - sodium module'
	pkg_install() {
		vmove "usr/lib/php${_php_version}/modules/sodium.so"
	}
}

php8.1-ffi_package() {
	lib32disabled=yes
	depends="php${_php_version}>=${version}_${revision}"
	short_desc+=' - FFI module'
	pkg_install() {
		vmove "usr/lib/php${_php_version}/modules/ffi.so"
	}
}
