################################################################################
#
# aspell-dict.cygclass - functions for building Aspell dictionaries
#
# Part of cygport - Cygwin packaging application
# Copyright (C) 2006-2015 Yaakov Selkowitz
# Provided by the Cygwin Ports project <http://sourceware.org/cygwinports/>
#
# cygport 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 3 of the License, or
# (at your option) any later version.
#
# cygport 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 cygport.  If not, see <http://www.gnu.org/licenses/>.
#
################################################################################

#****h* Cygclasses/aspell-dict.cygclass
#  SYNOPSIS
#  [ASPELL_COMPAT=...]
#  inherit aspell-dict
#  DESCRIPTION
#  Aspell is a GNU project providing a spell checking library and command-line
#  tool.  Aspell can support multiple languages by installing dictonaries.
#
#  The purpose of this cygclass is to create Aspell dictionary packages for
#  the Cygwin Aspell package.  aspell-dict.cygclass expects the package name
#  to be "aspell-<language_code>".  If the source tarball is named "aspell5-*"
#  or "aspell6-*", ASPELL_COMPAT must be declared before inheriting.
#  EXAMPLE
#
#    ASPELL_COMPAT=6
#    inherit aspell-dict
#    
#    NAME="aspell-en"
#    VERSION=7.1-0
#    RELEASE=1
#    CATEGORY="Text"
#    SUMMARY="Aspell English dictionaries"
#    DESCRIPTION="GNU Aspell is a Free and Open Source spell checker designed
#    to eventually replace Ispell. It can either be used as a library or as an
#    independent spell checker.
#    This package provides English dictionaries for Aspell."
#
#  REQUIRES
#  aspell
#  SEE ALSO
#  mirror_gnu
#****

#****v* aspell-dict.cygclass/ASPELL_COMPAT
#  DESCRIPTION
#  Dictionary compatibility level:
#  * unset -- Source named "aspell-*". Built with old system for 0.50; usually compatible with 0.60.
#  * 5 -- Source named "aspell5-*". Built for 0.50 but usually compatible with 0.60.
#  * 6 -- Source named "aspell6-*". Built for 0.60 and incompatible with 0.50.
#  NOTE
#  This variable, if necessary, must be declared before inheriting aspell-dict.cygclass.
#****
case "${ASPELL_COMPAT}" in
	5|6|'') ;;
	*)	error "aspell-dict: invalid value for ASPELL_COMPAT: ${ASPELL_COMPAT}" ;;
esac

check_prog_req aspell

#****d* aspell-dict.cygclass/ASPELL_DATADIR
#  DESCRIPTION
#  Installation path for Aspell platform-independent dictionary files
#****
ASPELL_DATADIR=$(aspell dump config data-dir)

#****d* aspell-dict.cygclass/ASPELL_DICTDIR
#  DESCRIPTION
#  Installation path for Aspell compiled dictionary files
#****
ASPELL_DICTDIR=$(aspell dump config dict-dir)

#****d* aspell-dict.cygclass/ASPELL_LANG
#  DESCRIPTION
#  ISO 639 short code for language name
#****
ASPELL_LANG="${PN#aspell-}"

#****d* aspell-dict.cygclass/ASPELL_LANG_FULL
#  DESCRIPTION
#  The English name for the language specified in ASPELL_LANG.
#  NOTE
#  The iso-codes package must be installed in order to provide this definition.
#****
iso_639=/usr/share/xml/iso-codes/iso_639.xml

if [ -f ${iso_639} ]
then
	ASPELL_LANG_FULL=$(sed -e ':a;N;$!ba;s|\n\t\t| |g' ${iso_639} | grep \"${ASPELL_LANG}\" | sed -e 's|.* name="\([^"\;]*\).*|\1|')
fi

unset iso_639

# e.g. tarball version 6.0-0 => PV=6.0.0
ORIG_PN="aspell${ASPELL_COMPAT}-${ASPELL_LANG}"
case ${PV} in
	*-*) ORIG_PV=${PV} ;;
	*)   ORIG_PV=${PV%.*}-${PV##*.} ;;
esac

#****o* aspell-dict.cygclass/CATEGORY (aspell-dict)
#  DEFINITION
CATEGORY="Text"
#****
#****o* aspell-dict.cygclass/SUMMARY (aspell-dict)
#  DEFINITION
SUMMARY="Aspell ${ASPELL_LANG_FULL:-${ASPELL_LANG}} dictionary"
#****
#****o* aspell-dict.cygclass/DESCRIPTION (aspell-dict)
#  DEFINITION
DESCRIPTION="GNU Aspell is a Free and Open Source spell checker designed to
eventually replace Ispell. It can either be used as a library or as an
independent spell checker.
This package provides ${ASPELL_LANG_FULL:-${ASPELL_LANG}} dictionaries for Aspell."
#****
#****o* aspell-dict.cygclass/HOMEPAGE (aspell-dict)
#  DEFINITION
HOMEPAGE="http://aspell.net/"
#****

#****o* aspell-dict.cygclass/SRC_URI (aspell-dict)
#  DEFINITION
SRC_URI="mirror://gnu/aspell/dict/${ASPELL_LANG}/${ORIG_PN}-${ORIG_PV}.tar.bz2"
#****

SRC_DIR="${ORIG_PN}-${ORIG_PV}"

#****C* aspell-dict.cygclass/aspell_dict_compile
#  SYNOPSIS
#  aspell_dict_compile
#  DESCRIPTION
#  Compiles an Aspell dictionary by running the custom configure script, then
#  calling cygmake.
#****
aspell_dict_compile() {
	if [ ! -e configure ]
	then
		error "aspell-dict: configure not found"
	fi

	./configure || error "aspell-dict: configure failed"
	cygmake
}

#****I* aspell-dict.cygclass/doaspelldict
#  SYNOPSIS
#  doaspelldict FILES
#  DESCRIPTION
#  Installs the Aspell dictionary files to the correct directories.  Aspell
#  dictionaries usually install with the default src_install, so this is generally
#  unnecessary.
#****
doaspelldict() {
	local f

	for f
	do
		if [ ! -e ${f} ]
		then
			error "doaspelldict: ${f}: file not found"
		fi

		case ${f##*.} in
			cmap|cset|dat|kbd)
				insinto ${ASPELL_DATADIR} ;;
			alias|cwl|multi|rws)
				insinto ${ASPELL_DICTDIR} ;;
			*)
				warning "doaspelldict: skipping ${f##*/}: unknown file type"
				continue ;;
		esac

		doins ${f}
	done
}

#****I* aspell-dict.cygclass/doaspelldata
#  DESCRIPTION
#  Alias for doaspelldict.
#****
doaspelldata() { doaspelldict ${@} ; }

#****o* aspell-dict.cygclass/src_compile (aspell-dict)
#  DEFINITION
src_compile() {
	lndirs
	cd ${B}
	aspell_dict_compile
}
#****

readonly -f aspell_dict_compile doaspelldict doaspelldata
