#!/bin/bash

die() {
	echo "$@" >&2
	exit 1
}       

inherit() {
	:
}

dumpfunc() {
	echo -e "\t# $1"
	type $1 2>/dev/null | grep -v "is a function$" \
		| sed 's/^ \+/\t/; s/\;$//; /^{/d; /^}/d; / \(\)$/d'
}


[ -z "$1" ] && die "usage: $(basename $0) PKGBUILD"

source $1

PF=$(basename $1 .ebuild)
P=${PF%-r[0-9]*}
if [ "$P" != "$PF" ]; then
	pkgrel=${PF##${P}-r}
else
	pkgrel=0
fi
PN=$(echo $P | sed 's/-[0-9].*//')
PV=${P##${PV}-}

maintainer="$(git config --get user.name) <$(git config --get user.email)>"
echo "# Maintainer: $maintainer"
echo "pkgname=$PN"
echo "pkgver=$PV"
echo "pkgrel=$pkgrel"
echo "pkgdesc=\"$DESCRIPTION\""
echo "url=\"$HOMEPAGE\""
echo "license=\"$LICENSE\""
echo "depends=\"$RDEPENDS\""
[ -n "${DEPENDS}" ] && echo "makedepends=\"${DEPENDS}\""
if grep enewuser $1 > /dev/null; then
	echo "install=$PN.install"
fi
echo "source=\"$(echo "$SRC_URI" | sed 's/ /\n\t/g; 
	s/\${PN}/\$pkgname/g;
	s/\${PV}/\$pkgver/g;
	s/\${P}/\$pkgname-\$pkgver/g')\""

echo ""

echo "build() {"
dumpfunc src_unpack
dumpfunc src_compile
dumpfunc src_install
echo "}"
type pkg_setup 2>/dev/null|grep -v "is a function$"|sed 's/^ \+/\t/; s/\;$//'
