################################################################################
#
# lua.cygclass - functions for installing Lua modules
#
# 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/lua.cygclass
#  SYNOPSIS
#  inherit lua
#  DESCRIPTION
#  Lua is a lightweight programming language commonly used for embedded scripting
#  support in a variety of programs.  It is extensible through C/C++ modules
#  and lua scripts.
#
#  This cygclass provides definitions used by packages depending on Lua.
#  REQUIRES
#  lua, pkg-config
#****

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

check_prog_req lua
check_prog_req pkg-config

#****d* lua.cygclass/LUA
#  DESCRIPTION
#  Absolute path to the Lua interpreter.
#****
LUA=/usr/bin/lua

#****d* lua.cygclass/LUAC
#  DESCRIPTION
#  Absolute path to the Lua bytecode compiler.
#****
LUAC=/usr/bin/luac

#****d* lua.cygclass/LUA_VERSION
#  DESCRIPTION
#  The major.minor version of the Lua interpreter.
#****
LUA_VERSION=$(pkg-config --variable=V lua)

# FIXME: is this really needed for anything?
LUA_VERSION_NUM=${LUA_VERSION//./0}

#****d* lua.cygclass/LUA_INCLUDEDIR
#  DESCRIPTION
#  Path containing the Lua C library headers.
#****
LUA_INCLUDEDIR=$(pkg-config --variable=INSTALL_INC lua)

#****d* lua.cygclass/LUA_LIBDIR
#  DESCRIPTION
#  Installation path for Lua C modules.
#****
LUA_LIBDIR=$(pkg-config --variable=INSTALL_CMOD lua)

#****d* lua.cygclass/LUA_SCRIPTDIR
#  DESCRIPTION
#  Installation path for Lua script modules.
#****
LUA_SCRIPTDIR=$(pkg-config --variable=INSTALL_LMOD lua)

#****d* lua.cygclass/LUA_CFLAGS
#  DESCRIPTION
#  Compile flags for the Lua C library.
#****
LUA_CFLAGS=$(pkg-config --cflags lua)

#****d* lua.cygclass/LUA_LIBS
#  DESCRIPTION
#  Link flags for the Lua C library.
#****
LUA_LIBS=$(pkg-config --libs lua)

#****I* lua.cygclass/luainto
#  SYNOPSIS
#  luainto SUBDIRECTORY
#  DESCRIPTION
#  Subdirectory of LUA_LIBDIR or LUA_SCRIPTDIR into which dolua should install.
#  This is usually unnecessary.
#****
luainto() {
	if (( $# != 1 ))
	then
		error "luainto accepts exactly one argument";
	fi

	case ${1} in
		/*) error "luainto argument should be only a subdirectory" ;;
	esac

	_luainto_dir=${1};
}

#****I* lua.cygclass/dolua
#  SYNOPSIS
#  [luainto SUBDIRECTORY]
#  dolua MODULE1 [MODULE2] ...
#  DESCRIPTION
#  Installs the given Lua module(s) (.lua or .dll) into LUA_LIBDIR or
#  LUA_SCRIPTDIR under $D, depending on filetype, or a subdirectory thereof
#  if luainto was previously called.
#****
dolua() {
	local i

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

		case ${i} in
		*.dll|*.la)
			dodir ${LUA_LIBDIR}/${_luainto_dir}
			__doinstall 0755 ${i} ${LUA_LIBDIR}/${_luainto_dir} || error "dolua ${i} failed"
			;;
		*)
			dodir ${LUA_SCRIPTDIR}/${_luainto_dir}
			__doinstall 0644 ${i} ${LUA_SCRIPTDIR}/${_luainto_dir} || error "dolua ${i} failed"
			;;
		esac
	done
}

#****I* lua.cygclass/lua_fix_shebang
#  SYNOPSIS
#  lua_fix_shebang SCRIPT [SCRIPT ...]
#  DESCRIPTION
#  Fixes the designated interpreter of SCRIPT to LUA.  This would be necessary
#  if the original uses an incorrect path (e.g. /usr/local/bin) or an
#  incorrectly versioned binary.  SCRIPT need not be prefixed by $D.
#****
lua_fix_shebang() {
    __fix_shebang ${LUA} ${D}/${@#${D}}
}

readonly -f luainto dolua lua_fix_shebang
