# Helper macroses for pear packages in ALT Linux.
# According to ALT Linux PEAR policy: http://www.freesource.info/wiki/Altlinux/Policy/Pear

# Assure pear_name contains name of PEAR package

# Possible runtime method (from RH):
#pear_datadir       %(%{__pear} config-get data_dir 2> /dev/null || echo undefined)

#%_peardir %_datadir/php/pear
%pear_dir %php_peardir
%pear_datadir %pear_dir/data
%pear_docdir %pear_dir/docs
%pear_testdir %pear_dir/tests
# RH compatible path
%pear_xmldir %pear_dir/.pkgxml


# FIXME --soft makes "Undefined offset:  5 in /usr/share/php/pear/Console/Getopt.php on line 227"
# for use in %%post section
%register_pear_module() \
pear install --nodeps -s --force --register-only %pear_xmldir/%{pear_name}.xml >/dev/null || : \
%nil

# FIXME --register-only makes "Undefined offset:  5 in /usr/share/php/pear/Console/Getopt.php on line 227"
# for use in %%preun section
%unregister_pear_module() \
if [ "$1" = "0" ]; then \
	pear uninstall --nodeps --ignore-errors -r %{pear_name} > dev/null || : \
fi \
%nil

# FIXME: altbug # https://bugzilla.altlinux.org/show_bug.cgi?id=13866
# Prepare XML description, LICENSE and CHANGELOG
%pear_build() \
[ -f package2.xml ] || mv package.xml package2.xml \
%_bindir/php -d safe_mode=0 %pear_dir/xml2changelog package2.xml >CHANGELOG || echo "xml2changelog is failed" >CHANGELOG \
[ -d %{pear_name}-%{version} ] && mv package2.xml %{pear_name}-%{version}/%{pear_name}.xml || mv package2.xml %{pear_name}.xml \
install -m 644 -c %pear_dir/PHP-LICENSE-3.01 LICENSE \
%nil

# Install XML package description
%pear_install_xml() \
install -D -m 644 %{pear_name}.xml %buildroot%pear_xmldir/%{pear_name}.xml \
%nil

# TODO: use just pear_install in future?
# Install module and clean up unnecessary files
%pear_install_std() \
# cd to module dir if not jet \
[ -d %{pear_name}-%{version} ] && cd %{pear_name}-%{version} \
pear install --nodeps --offline --packagingroot=%buildroot ./%pear_name.xml \
rm -rf %buildroot%pear_dir/.??* \
%pear_install_xml \
%nil

# compatibility:
%pear_prepare_module %pear_build
%pear_install_module %pear_install_std

%pear_install %register_pear_module
%pear_uninstall %unregister_pear_module

#%pear_install_std %pear_install
