#!/bin/sh
#
# dh-make-pear creates a debian package from a pear package
#
# Copyright (C) 2005-2006 by Uwe Steinmann <steinm@debian.org>
# under the GNU GPL license version 2.0 or 2.1.
# You should have received a copy of the GPL license
# along with this program if you did not you can find it
# at http://www.gnu.org/.

PROGNAME=dh-make-pear
PROGVERSION=0.2.0
PREFIX=/usr

DEBPACKAGEPREFIX=php
DEBTEMPDIR=${PREFIX}/share/dh-make-php/pear.template
PACKAGEDOCFILES="README readme CREDITS TODO AUTHORS"
# if you change the examples directory make sure to adjust the
# regular expression to set the PACKAGEDOCFILES
PACKAGEEXPFILES="examples"
# There has been versions of xmlstarlet which were called xml
# instead of xmlstarlet
if [ -x /usr/bin/xmlstarlet ]; then
	XMLSTARLET=/usr/bin/xmlstarlet
else
	XMLSTARLET=/usr/bin/xml
fi
DEBMAINTAINER=
DEPENDS=
DEPARCH=all

. ${PREFIX}/share/dh-make-php/dh-make-php.lib

# usage() {{{
# print summary of options
usage()
{
	cat <<eof
This is ${PROGNAME} Version ${PROGVERSION}
Copyright (c) 2004-2005 Uwe Steinmann <steinm@debian.org>

Usage: ${PROGNAME} [options] pear-package

pear-package is a tgz file as it is downloaded by 'pear download'.
Options:
  --help | -h     show this usage information
  --version | -v  show version of program
  --maintainer    maintainer of the debian package
  --arch          set architecture (defaults to 'all')
  --depends       set more depends aside php
  --prefix        set prefix for package name (default is '${DEBPACKAGEPREFIX}')
  --templatedir   set alternative directory for templates
                  (default is ${DEBTEMPDIR})

eof
}
# }}}

# command line options {{{
if [ $# = 0 ]; then
	usage
	exit 0
fi

while
	case $1 in
		--help|-h) # print usage
			usage
			exit 0
			;;
		--version|-v) # show version
      echo ${PROGVERSION}
			exit 0
			;;
		--maintainer) # set maintainer of debian package
			DEBMAINTAINER="$2"; shift
			;;
		--arch) # set architecture of debian package
			DEBARCH="$2"; shift
			;;
		--depends) # set extra depends for debian package
			DEPENDS=", $2"; shift
			;;
		--prefix) # set prefix for name of debian package
			DEBPACKAGEPREFIX="$2"; shift
			;;
		--templatedir) # set alternative template dir
			DEBTEMPDIR="$2"; shift
			;;
		"")
			break
			;;
		*)
			PEARPACKAGENAME="$1"
			break
			;;
	esac
do test $# -gt 0 && shift; done
# }}}

if [ -n "${PEARPACKAGENAME}" ]; then
	if [ -f ${PEARPACKAGENAME} ]; then
		tar xzf ${PEARPACKAGENAME}
	else
		if TEMPMSG=$(pear download ${PEARPACKAGENAME} 2> /dev/null); then
			echo ${TEMPMSG}
			PEARPACKAGENAME=$(echo ${TEMPMSG} | awk -F ' ' '{print $2'})
			tar xzf ${PEARPACKAGENAME}
		else
			echo "Downloading pear package '${PEARPACKAGENAME}' failed!";
			echo "Check http://pear.php.net/ for available packages.";
			exit 0
		fi
	fi
else
	usage
	exit 0
fi

# get information about package from package.xml
eval_package

echo "Creating debian source package: ${DEBPACKAGEPREFIX}-${PHP_PKG_LOWNAME}-${VERSION}"
echo "Upstream is: ${UPSTREAM}"

get_maintainer

if [ -d ${DEBPACKAGEPREFIX}-${PHP_PKG_LOWNAME}-${VERSION} ] ; then
	echo "Directory '${DEBPACKAGEPREFIX}-${PHP_PKG_LOWNAME}-${VERSION}' already exits."
	exit
fi
ln -s ${PEARPACKAGENAME} ${DEBPACKAGEPREFIX}-${PHP_PKG_LOWNAME}_${VERSION}.orig.tar.gz
mkdir ${DEBPACKAGEPREFIX}-${PHP_PKG_LOWNAME}-${VERSION}
mv ${PHP_PKG_NAME}-${VERSION} ${DEBPACKAGEPREFIX}-${PHP_PKG_LOWNAME}-${VERSION}
mv package.xml ${DEBPACKAGEPREFIX}-${PHP_PKG_LOWNAME}-${VERSION}
#tar czf ${DEBPACKAGEPREFIX}-${PHP_PKG_LOWNAME}_${VERSION}.orig.tar.gz ${DEBPACKAGEPREFIX}-${PHP_PKG_LOWNAME}-${VERSION}
mkdir -p ${DEBPACKAGEPREFIX}-${PHP_PKG_LOWNAME}-${VERSION}/debian

cp ${DEBTEMPDIR}/compat ${DEBPACKAGEPREFIX}-${PHP_PKG_LOWNAME}-${VERSION}/debian
cp ${DEBTEMPDIR}/dirs ${DEBPACKAGEPREFIX}-${PHP_PKG_LOWNAME}-${VERSION}/debian

install_docfiles
install_expfiles

PACKAGEXML=${DEBPACKAGEPREFIX}-${PHP_PKG_LOWNAME}-${VERSION}/package.xml

sed -e "s/##date##/`(LC_ALL=C; date -R)`/g" \
    -e "s/##maintainer##/${DEBMAINTAINER}/g" \
    -e "s/##upstream##/${UPSTREAM}/g" \
    -e "s/##license##/${LICENSE}/g" \
    -e "s/##pearpkgname##/${PHP_PKG_NAME}/g" \
		${DEBTEMPDIR}/copyright > ${DEBPACKAGEPREFIX}-${PHP_PKG_LOWNAME}-${VERSION}/debian/copyright
if [ -n "${LICENSEFILE}" ]; then
	cat "${LICENSEFILE}" >> ${DEBPACKAGEPREFIX}-${PHP_PKG_LOWNAME}-${VERSION}/debian/copyright
fi
sed -e "s/##packagename##/${DEBPACKAGEPREFIX}-${PHP_PKG_LOWNAME}/g" \
    -e "s/##maintainer##/${DEBMAINTAINER}/g" \
    -e "s##summary##${SUMMARY}g" \
    -e "s/##depends##/${DEPENDS}/g" \
    -e "s/##architecture##/${DEPARCH}/g" \
		${DEBTEMPDIR}/control > ${DEBPACKAGEPREFIX}-${PHP_PKG_LOWNAME}-${VERSION}/debian/control
		xsltproc --nonet --novalid  --param Element "/*/description" ${PREFIX}/share/dh-make-php/xslt/common.xsl ${PACKAGEXML} | fold -s -w 70|sed -e "s^[ \t]*$.g" -e "s^ g" >> ${DEBPACKAGEPREFIX}-${PHP_PKG_LOWNAME}-${VERSION}/debian/control
#		sgrep -e '"<description>"__"</description>"' ${PACKAGEXML} | fold -s -w 70|sed -e "s^[ \t]*$.g" -e "s^ g" >> ${DEBPACKAGEPREFIX}-${PHP_PKG_LOWNAME}-${VERSION}/debian/control
sed -e "s/##packagename##/${DEBPACKAGEPREFIX}-${PHP_PKG_LOWNAME}/g" \
    -e "s/##version##/${VERSION}/g" \
    -e "s/##pearpkgname##/${PHP_PKG_NAME}/g" \
		${DEBTEMPDIR}/rules > ${DEBPACKAGEPREFIX}-${PHP_PKG_LOWNAME}-${VERSION}/debian/rules
chmod 755 ${DEBPACKAGEPREFIX}-${PHP_PKG_LOWNAME}-${VERSION}/debian/rules

sed -e "s/##packagename##/${DEBPACKAGEPREFIX}-${PHP_PKG_LOWNAME}/g" \
    -e "s/##date##/`(LC_ALL=C; date -R)`/g" \
    -e "s/##maintainer##/${DEBMAINTAINER}/g" \
    -e "s/##version##/${VERSION}/g" \
		${DEBTEMPDIR}/changelog > ${DEBPACKAGEPREFIX}-${PHP_PKG_LOWNAME}-${VERSION}/debian/changelog

sed -e "s/##packagename##/${DEBPACKAGEPREFIX}-${PHP_PKG_LOWNAME}/g" \
    -e "s/##date##/`(LC_ALL=C; date -R)`/g" \
    -e "s/##maintainer##/${DEBMAINTAINER}/g" \
		${DEBTEMPDIR}/README.Debian > ${DEBPACKAGEPREFIX}-${PHP_PKG_LOWNAME}-${VERSION}/debian/README.Debian

sed -e "s/##pearpkgname##/${PHP_PKG_NAME}/g" \
		${DEBTEMPDIR}/watch > ${DEBPACKAGEPREFIX}-${PHP_PKG_LOWNAME}-${VERSION}/debian/watch

