#!/bin/bash
#
#   fwcpan
#
#   Copyright (c) 2006 by Miklos Vajna <vmiklos@frugalware.org>
#
#   This program is free software; you can redistribute it and/or modify
#   it under the terms of the GNU General Public License as published by
#   the Free Software Foundation; either version 2 of the License, or
#   (at your option) any later version.
#
#   This program is distributed in the hope that it will be useful,
#   but WITHOUT ANY WARRANTY; without even the implied warranty of
#   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
#   GNU General Public License for more details.
#
#   You should have received a copy of the GNU General Public License
#   along with this program; if not, write to the Free Software
#   Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307,
#   USA.
#

if [ "$1" == "-h" -o "$1" == "--help" ]; then
	man fwcpan
	exit $?
fi

if [ "$1" == "-v" -o "$1" == "--version" ]; then
	pacman-g2 -Q pacman-tools
	exit $?
fi

if [ "$1" = "-i" -o "$1" = "--install" ]; then
	inst=1
	shift
fi

if [ -z "$1" ]; then
	echo "What is the name of the module to install?"
	read needle
else
	needle="$1"
fi

## grep: Übereinstimmungen in Binärdatei (Standardeingabe)
## seems to be some bug in grep
## well 2 fixes for this issue ..
## 1) use grep -a all over the place
## 2) use LC_ALL=C or LC_ALL=en_US

OLD_LC_ALL=`echo $LC_ALL`
unset LC_ALL
export LC_ALL=C

echo -n "Searching CPAN... "
dump=`mktemp`
lynx -dump "http://search.cpan.org/search?query=$needle&mode=module" >$dump
needle=`echo $needle|sed 's/::/-/g'`
modname=`cat $dump|grep -i "$needle-" | sed 's/.*\]\([^ ]*\)-.*\].*/\1/;q'`
if [ -z "$modname" ]; then
	echo "Sorry, I can't find just a module!"
	rm -f $dump
	exit 0
fi
pkgname="perl-`echo $modname|tr [:upper:] [:lower:]`"
pkgver=`cat $dump|grep $modname-|sed "s/.*$modname-\([^ ]*\) .*/\1/;q"`
pkgdesc=`cat $dump|grep -1 $modname-|sed 's/   //g;q'`
suburl=`cat $dump|grep $modname-$pkgver|sed -n 's/.*[0-9]\. \(.*\)/\1/;$ p'`
echo -e "done.\n"

echo "Guessing module: $modname"
echo -e "Targets: $pkgname-$pkgver-1\n"
echo -n "Proceed with upgrade? [Y/n]"
read junk
if [ ! -z "$junk" -a ! "`echo $junk|tr [A-Z] [a-z]`" = "y" -a ! "`echo $junk|tr [A-Z] [a-z]`" = "yes" ]; then
	rm -f $dump
	exit 0
fi

echo -ne "\nWriting the FrugalBuild... "
modauthor=`lynx -dump $suburl|grep authors|sed "s|.*id/\(.*\)/$modname.*|\1|;q"`
src="http://search.cpan.org/CPAN/authors/id/$modauthor/$modname-$pkgver.tar.gz"
srcdump=`mktemp`
lynx -dump -source $src >$srcdump
sha1sum=`cat $srcdump|sha1sum |sed 's/  -$//'`
makedump=`mktemp`
tar xOzf $srcdump $modname-$pkgver/Makefile.PL >$makedump 2>/dev/null

# TODO: depends(). though this is not so problematic: the build will properly
# abort on any missing depend

cat << EOF > FrugalBuild
# Compiling Time: 0.1 SBU
# Maintainer:

_F_perl_name=$modname
_F_perl_author=$modauthor
pkgver=$pkgver
pkgdesc="$pkgdesc"
archs=('`uname -m`')
Finclude perl
sha1sums=('$sha1sum')
EOF

# clean up the unnecessary directives, they're inherited from perl.sh
sed -i "/depends=()/d" FrugalBuild
sed -i "/archs=('i686')/d" FrugalBuild

echo "done."

unset LC_ALL
export LC_ALL=$OLD_LC_ALL

rm -f $dump $srcdump $makedump

if [ "$inst" = "1" ]; then
	makepkg -ci
fi
