# Copyright 1999-2011 Gentoo Foundation
# Distributed under the terms of the GNU General Public License v2
# $Header: $

EAPI="2"

inherit eutils

DESCRIPTION="Trigraph Password Generator"
HOMEPAGE="http://www.multicians.org/thvv/tvvtools.html#gpw"
# original files on autor's homepage, but debian sources are packed and provide
# a man page
#SRC_URI="ftp://ftp.multicians.org/gpw.C
#	ftp://ftp.multicians.org/loadtris.C
#	ftp://ftp.multicians.org/gpw.Makefile"

# wordfile taken from sys-apps/miscfiles which is not needed at runtime
MISCFILES_PV=1.4.2
SRC_URI="ftp://ftp.de.debian.org/debian/pool/main/g/gpw/gpw_0.0.19940601.orig.tar.gz
	ftp://ftp.de.debian.org/debian/pool/main/g/gpw/gpw_0.0.19940601-8.1.diff.gz
	mirror://gnu/miscfiles/miscfiles-${MISCFILES_PV}.tar.gz"

LICENSE="as-is"
SLOT="0"
KEYWORDS="~alpha ~amd64 ~arm ~hppa ~ia64 ~m68k ~mips ~ppc ~ppc64 ~s390 ~sh ~sparc ~x86 ~sparc-fbsd ~x86-fbsd ~x86-interix ~amd64-linux ~x86-linux ~ppc-macos ~x64-macos ~x86-macos ~m68k-mint ~sparc-solaris ~sparc64-solaris ~x86-solaris"

src_prepare() {
	cd ${S}
	epatch "${WORKDIR}/gpw_0.0.19940601-8.1.diff"
}

src_compile() {
    $(tc-getCC) ${CFLAGS} -W -Wall -pedantic -o loadtris.o -c loadtris.c || die 
    $(tc-getCC) ${CFLAGS} -W -Wall -pedantic -o loadtris loadtris.o || die 

	./loadtris "${WORKDIR}/miscfiles-${MISCFILES_PV}/web2" | sed "s/, }/}/" > trigram.h.wait \
		 || die "Failed to generate trigram.h"

	# this patch provides a workaround for this warning while compiling gpw
	# trigram.h:5:1:
	# trigram.h:11:1:
	# trigram.h:15:1: warning: overflow in implicit constant conversion
	sed -e "s/const short sing/const long sing/g" \
		-e "s/const short duos/const long duos/g" \
		trigram.h.wait > trigram.h \
		 || die "Failed to patch trigram.h"

    $(tc-getCC) ${CFLAGS} -W -Wall -pedantic -o gpw.o -c gpw.c || die
    $(tc-getCC) ${CFLAGS} -W -Wall -pedantic -o gpw gpw.o || \
		die "Died compiling gpw"
}

src_install() {
	dobin gpw || die
	doman debian/gpw.1 || die
	dodoc README || die
	dodoc LICENSE || die
	newdoc debian/changelog changelog.Debian || die
}