# Copyright © 2013 Brandon Invergo <brandon@invergo.net>
#
# This file is part of GSRC.
#
# GSRC 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.
#
# GSRC 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 GSRC.  If not, see <http://www.gnu.org/licenses/>.

NAME = Ncurses
GARNAME = ncurses
GARVERSION = 5.9
HOME_URL = http://www.gnu.org/software/ncurses/
DESCRIPTION = Terminal emulation (termcap, terminfo) library
define BLURB
Ncurses is a library which provides capabilities to write text to a
terminal in a terminal-independent manner.  It supports pads and
color as well as multiple highlights and forms characters.  It is
typically used to implement user interfaces for command-line
applications.
endef

######################################################################

MASTER_SITES = $(MASTER_GNU)
MASTER_SUBDIR = $(GARNAME)/
DISTFILES = $(DISTNAME).tar.gz
SIGFILES = $(DISTNAME).tar.gz.sig
CONFIGURE_SCRIPTS = ncurses ncursesw
BUILD_SCRIPTS = ncurses ncursesw
INSTALL_SCRIPTS = ncurses ncursesw


BUILDDEPS = libtool
LIBDEPS =

######################################################################

include ../../gar.lib/auto.mk
include config.mk

CONFIGURE_ARGS += --with-shared --with-normal --without-debug \
	--with-pkg-config=$(packageprefix)/lib/pkgconfig
ifeq ($(GARARCH),x86_64)
	ARCH_ARG = --with-chtype=long
else
	ARCH_ARG =
endif

configure-ncurses:
	@printf "[$(OK)configure$(OFF)] $(MSG)Running $(CONFIGURE_NAME) in $(OFF)$*\n"
	mkdir $(WORKDIR)/ncurses-build
	cd $(WORKDIR)/ncurses-build && $(CONFIGURE_ENV) ../$(DISTNAME)/configure $(CONFIGURE_ARGS) $(ARCH_ARG) $(OUTPUT)
	$(MAKECOOKIE)

configure-ncursesw:
	@printf "[$(OK)configure$(OFF)] $(MSG)Running $(CONFIGURE_NAME) in $(OFF)$*\n"
	mkdir $(WORKDIR)/ncursesw-build
	cd $(WORKDIR)/ncursesw-build && $(CONFIGURE_ENV) ../$(DISTNAME)/configure \
		$(CONFIGURE_ARGS) --enable-widec --enable-pc-files $(OUTPUT)
	$(MAKECOOKIE)

build-ncurses:
	@printf "[$(OK)build$(OFF)] $(MSG)Running make in $(OFF)$*\n"
	$(BUILD_ENV) $(MAKE) -C $(WORKDIR)/ncurses-build $(MAKE_ARGS) $(BUILD_ARGS) \
		$(OUTPUT) || $(BUILD_FAIL)
	$(MAKECOOKIE)

build-ncursesw:
	@printf "[$(OK)build$(OFF)] $(MSG)Running make in $(OFF)$*\n"
	$(BUILD_ENV) $(MAKE) -C $(WORKDIR)/ncursesw-build $(MAKE_ARGS) $(BUILD_ARGS) \
		$(OUTPUT) || $(BUILD_FAIL)
	$(MAKECOOKIE)

install-ncurses:
	@printf "[$(OK)install$(OFF)] $(MSG)Running make $(INSTALL_TARGET) in $(OFF)$*\n"
	cd $(WORKDIR)/ncurses-build && for lib in ncurses form panel menu; do \
		install -Dm755 lib/lib$${lib}.so.$(GARVERSION) \
			$(packageprefix)/lib/lib$${lib}.so.$(GARVERSION); \
		ln -s lib$${lib}.so.$(GARVERSION) $(packageprefix)/lib/lib$${lib}.so.5; \
	done
	$(MAKECOOKIE)

pre-install:
	mkdir -p $(packageprefix)/lib/pkgconfig
	$(MAKECOOKIE)

install-ncursesw:
	@printf "[$(OK)install$(OFF)] $(MSG)Running make $(INSTALL_TARGET) in $(OFF)$*\n"
	$(INSTALL_ENV) $(MAKE) -C $(WORKDIR)/ncursesw-build $(MAKE_ARGS) \
		$(INSTALL_ARGS) install $(OUTPUT)
	cd $(WORKDIR)/ncursesw-build && for lib in ncurses form panel menu; do \
		echo "INPUT(-l$${lib}w)" >$(packageprefix)/lib/lib$${lib}.so; \
	done
	cd $(WORKDIR)/ncursesw-build && for lib in ncurses ncurses++ fom panel menu; do \
		ln -s $${lib}w.pc $(packageprefix)/lib/pkgconfig/$${lib}.pc; \
	done
	cd $(WORKDIR)/ncursesw-build && echo "INPUT(-lncursesw)" \
		>$(packageprefix)/lib/libcursesw.so
	ln -s $(WORKDIR)/ncursesw-build/libncurses.so $(packageprefix)/lib/libcurses.so
	$(MAKECOOKIE)
