################################################################################
#
# rox.cygclass - functions for building ROX Desktop 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/rox.cygclass
#  SYNOPSIS
#  inherit rox
#  DESCRIPTION
#  The ROX Desktop is based on RISC OS.  The desktop revolves around
#  the file manager and drag-and-drop is used instead of Open/Save file
#  dialogs.
#
#  ROX applications are standalone application directories (AppDirs) which
#  can be run from anywhere without installation.  Most applications are
#  written in Python with ROX-Lib2 and some are written in C with ROX-CLib;
#  either way, GTK+ is used as the underlying toolkit.
#
#  This cygclass handles the building and installing of ROX packages.
#  AppDirs are installed under /usr/lib/rox.
#  INHERITS
#  python.cygclass
#****

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

inherit python

ORIG_PN=${ORIG_PN:-${PN#rox-}}

#****o* rox.cygclass/CATEGORY (rox)
#  DEFINITION
CATEGORY="ROX"
#****
#****o* rox.cygclass/SUMMARY (rox)
#  DEFINITION
SUMMARY="ROX Desktop ${ORIG_PN} component"
#****
#****o* rox.cygclass/DESCRIPTION (rox)
#  DEFINITION
DESCRIPTION="ROX is a fast, user friendly desktop based on RISC OS which makes
extensive use of drag-and-drop. The interface revolves around the file manager,
or filer, following the traditional Unix view that 'everything is a file' rather
than trying to hide the filesystem beneath start menus, wizards, or druids."
#****
#****o* rox.cygclass/HOMEPAGE (rox)
#  DEFINITION
HOMEPAGE="http://rox.sourceforge.net/desktop/"
#****

DEPS_PATH="/usr/lib/ROX-Lib2/python"

#****v* rox.cygclass/ROX_APP
#  DESCRIPTION
#  The name of the directory containing the AppRun script.  This variable
#  is required.
#  NOTE
#  Often this directory is the toplevel of the tarball, in which case
#  SRC_DIR must be set to '.'.
#****

#****v* rox.cygclass/ROX_WRAPPER
#  DESCRIPTION
#  Due to the DnD nature of the ROX desktop, most applications are not
#  usable outside the filer.  For the exceptions (e.g. OroboROX window
#  manager) or for the filer (rox) and session manager (rox-session) which
#  need to be launched from the command line (or in .xinitrc), define
#  ROX_WRAPPER to the name of the wrapper script.  This wrapper will be
#  installed in /usr/bin.
#****

#****C* rox.cygclass/rox_autoreconf
#  DESCRIPTION
#  Runs autoconf, the only autotool which is generally used in ROX C
#  packages, in order to pick up any patches to configure.{ac,in}.
#  REQUIRES
#  autoconf
#****
rox_autoreconf() {
	if [ ! -e configure.in ]
	then
		error "not in src directory"
	fi

	__config_set with_autoconf 1

	rm -f configure
	autoconf --force || error "autoconf failed"
}

#****C* rox.cygclass/rox_compile
#  DESCRIPTION
#  This function builds a ROX C package through the AppRun script.
#  NOTE
#  Be aware that some AppRun scripts create the executable in a
#  platform-specific subdir, named based on uname(1) output.  Because
#  Cygwin's `uname -s` also contains the Windows version, applications
#  compiled on one computer will not be found on another if their underlying
#  Windows versions differ.  To avoid this, change the definition of PLATFORM
#  in AppRun (and AppletRun, if it exists) from
#    PLATFORM=`uname -s`-$ARCH
#  to
#    PLATFORM=CYGWIN-$ARCH
#  This will allow the application to run on all Cygwin installations.
#****
rox_compile() {
	if [ ! -e AppRun ]
	then
		error "AppRun: not found"
	fi

	./AppRun --compile || error "compile failed"
}

#****I* rox.cygclass/rox_install
#  DESCRIPTION
#  Install the (possibly compiled) AppDir into ${D}/usr/lib/rox,
#  removes extraneous files (e.g. sources), compiles Python modules, and
#  creates the wrapper script if ROX_WRAPPER is defined.
#****
rox_install() {
	local wrapper=${ROX_WRAPPER:-none}

	if [ ! -d ${ROX_APP} ]
	then
		error "${ROX_APP}: directory not found"
	fi

	dodir /usr/lib/rox
	cp -LR ${ROX_APP} ${D}/usr/lib/rox/
	rm -fr ${D}/usr/lib/rox/${ROX_APP}/{=build,build,src,.cvsignore}

	python_optimize /usr/lib/rox/${ROX_APP}

	if [ "${wrapper}" != "none" ]
	then
		dodir /usr/bin
		cat > ${D}/usr/bin/${wrapper} <<-_EOF
			#! /bin/sh
			exec /usr/lib/rox/${ROX_APP}/AppRun "\$@"
			_EOF
		chmod 0755 ${D}/usr/bin/${wrapper}
	fi

	dodoc ${ROX_APP}/Help/{COPYING,Changes,README}
}

#****o* rox.cygclass/src_compile (rox)
#  DEFINITION
src_compile() {
	lndirs
	if [ -d ${S}/${ROX_APP}/src ]
	then
		cd ${B}/${ROX_APP}/src
		rox_autoreconf
		cd ${B}/${ROX_APP}
		rox_compile
	fi
}
#****

#****o* rox.cygclass/src_install (rox)
#  DEFINITION
src_install() {
	cd ${B}
	rox_install
}
#****

readonly -f rox_autoreconf rox_compile rox_install
