################################################################################
#
# gnustep.cygclass - functions for building GNUstep packages
#
# Part of cygport - Cygwin packaging application
# Copyright (C) 2006-2016 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/gnustep.cygclass
#  DESCRIPTION
#  GNUstep is an open-source implementation of the NeXT/OpenStep and Apple's
#  Cocoa API frameworks, written in Objective-C.  A number of additional
#  libraries (known as Frameworks) and programs, both console and GUI are
#  available, including some ported from Mac OS X.  GNUstep packages are built
#  with GNUmakefiles which use templates included in the gnustep-make package.
#
#  This cygclass builds packages which use the GNUstep framework and build system.
#  REQUIRES
#  gnustep-make
#  SEE ALSO
#  mirror_gnustep
#****

# cross-compiling is not (yet?) supported
__cross_compiling_error

check_prog_req gnustep-config gnustep-make

case ${PN} in
	*.app|*.bundle|*.framework|*.tool)
		ORIG_PN=${ORIG_PN:-${PN%.*}}
	;;
esac

#****o* gnustep.cygclass/CATEGORY (gnustep)
#  DEFINITION
CATEGORY="GNUstep"
#****
#****o* gnustep.cygclass/SUMMARY (gnustep)
#  DEFINITION
SUMMARY="GNUstep ${ORIG_PN:-${PN#gnustep-}} component"
#****
#****o* gnustep.cygclass/HOMEPAGE (gnustep)
#  DEFINITION
HOMEPAGE="http://www.gnustep.org/"
#****

#****o* gnustep.cygclass/SVN_URI (gnustep)
#  NOTE
#  svn.cygclass must be inherit()ed last for this to have any effect.
#  DEFINITION
SVN_URI="svn://svn.gna.org/svn/gnustep"
#****

SVN_MODULE=${ORIG_PN}

# Returns the full path to the given GNUstep directory type.
__gnustep_dir() {
	if (( $# != 1 ))
	then
		error "__gnustep_dir: requires exactly one argument"
	fi

	local d=${1}
	local suffix=

	case ${d} in
		BUNDLES)	d="SYSTEM_LIBRARY"; suffix="/Bundles" ;;
		FONTS)		d="SYSTEM_LIBRARY"; suffix="/Fonts" ;;
		FRAMEWORKS)	d="SYSTEM_LIBRARY"; suffix="/Frameworks" ;;
		MAKEFILES)	;;
		SERVICES)	d="SYSTEM_LIBRARY"; suffix="/Services" ;;
		APPS|DOC|HEADERS|LIBRARIES|TOOLS)	d="SYSTEM_${d}" ;;
		*)	error "__gnustep_dir: unknown directory type: ${d}"
	esac

	var="GNUSTEP_${d}"
	eval $(grep "^${var}=" /etc/GNUstep/GNUstep.conf)
	echo -n ${!var#/}${suffix}
}

#for dvar in APPS BUNDLES DOC FONTS FRAMEWORKS HEADERS LIBRARIES MAKEFILES SERVICES TOOLS
#do
#	declare GNUSTEP_${dvar}=$(__gnustep_dir ${dvar})
#done
#unset dvar

#****f* gnustep.cygclass/gnustep_env
#  DESCRIPTION
#  Loads the GNUstep environment variables.  This must be run before calling
#  gnustep_compile or gnustep_install.
#****
gnustep_env() {
	local mf=$(gnustep-config --variable=GNUSTEP_MAKEFILES)
	. ${mf}/GNUstep-reset.sh
	. ${mf}/GNUstep.sh
}

#****C* gnustep.cygclass/gnustep_compile
#  SYNOPSIS
#  cd $B
#  gnustep_compile [MAKEFILE_ARGUMENTS]
#  DESCRIPTION
#  Builds a GNUstep package with the following steps:
#  - cygconf, if a configure script is present (usually not).
#  - cygmake
#  - cygmake in the Documentation directory, if present.
#  Arguments to gnustep_compile are passed to both cygmake calls.
#  NOTE
#  gnustep_env must be called before gnustep_compile.
#****
gnustep_compile() {
	if ! defined GNUSTEP_HOST
	then
		error "gnustep_env must be called first"
	fi

	if [ -e configure ]
	then
		cygconf --with-installation-domain=SYSTEM
	fi

	cygmake GNUSTEP_INSTALLATION_DOMAIN=SYSTEM ${@}

	if [ -e Documentation/GNUmakefile ]
	then
		cygmake -C Documentation GNUSTEP_INSTALLATION_DOMAIN=SYSTEM ${@}
	fi
}

#****I* gnustep.cygclass/gnustep_install
#  SYNOPSIS
#  cd $B
#  gnustep_install [MAKEFILE_ARGUMENTS]
#  DESCRIPTION
#  Installs a GNUstep package under $D.  Arguments to gnustep_install are
#  passed to cyginstall.
#  NOTE
#  gnustep_env must be called before gnustep_install.
#****
gnustep_install() {
	if ! defined GNUSTEP_HOST
	then
		error "gnustep_env must be called first"
	fi

	cyginstall GNUSTEP_INSTALLATION_DOMAIN=SYSTEM ${@}

	if [ -e Documentation/GNUmakefile ]
	then
		cyginstall -C Documentation GNUSTEP_INSTALLATION_DOMAIN=SYSTEM ${@}
	fi
}

#****f* gnustep.cygclass/gnustep_pkg_apps
#  SYNOPSIS
#  gnustep_pkg_apps APP_NAME [APP_NAME] ...
#  DESCRIPTION
#  Create a binary subpackage $APP_NAME.app containing the given application(s).
#****
gnustep_pkg_apps() {
	local a an

	for a in ${@}
	do
		an=${a%.app}

		PKG_NAMES+=" ${an}.app"
		export ${an}_app_CONTENTS="usr/bin/${an} $(__gnustep_dir APPS)/${an}.app/"
	done
}

#****f* gnustep.cygclass/gnustep_pkg_frameworks
#  SYNOPSIS
#  gnustep_pkg_frameworks FRAMEWORK_NAME:VERSION [FRAMEWORK_NAME:VERSION] ...
#  DESCRIPTION
#  Create binary subpackages lib$FRAMEWORK_NAME$VERSION and lib$FRAMEWORK_NAME-devel
#  containing the runtime and development components of the given frameworks(s).
#****
gnustep_pkg_frameworks() {
	local fd fn fv f_v _v

	for f_v in ${@}
	do
		fn=${f_v%:*}
		fv=${f_v#*:}
		_v=${fv//./_}
		fd=$(__gnustep_dir FRAMEWORKS)/${fn}.framework

		PKG_NAMES+=" lib${fn}${fv} lib${fn}-devel"
		export lib${fn}${_v}_CONTENTS="usr/bin/cyg${fn}-${_v}.dll \
		                               ${fd}/Resources \
		                               ${fd}/Versions/${fv}/Resources/ \
		                               ${fd}/Versions/${fv}/cyg${fn}-${_v}.dll \
		                               ${fd}/Versions/Current"
		export lib${fn}_devel_CONTENTS="$(__gnustep_dir HEADERS)/${fn}/ \
		                                ${fd}/Headers \
		                                ${fd}/${fn} \
		                                ${fd}/Versions/${fv}/Headers \
		                                ${fd}/Versions/${fv}/${fn} \
		                                ${fd}/Versions/${fv}/lib${fn}.dll.a \
		                                ${fd}/lib${fn}.dll.a \
		                                usr/lib/lib${fn}.dll.a"
	done
}

#****o* gnustep.cygclass/src_compile (gnustep)
#  DEFINITION
src_compile() {
	lndirs
	cd ${B}
	gnustep_env
	gnustep_compile
}
#****

#****o* gnustep.cygclass/src_install (gnustep)
#  DEFINITION
src_install() {
	cd ${B}
	gnustep_env
	gnustep_install
}
#****

readonly -f __gnustep_dir gnustep_env gnustep_compile gnustep_install \
            gnustep_pkg_apps gnustep_pkg_frameworks
