#
# This file and its contents are supplied under the terms of the
# Common Development and Distribution License ("CDDL"). You may
# only use this file in accordance with the terms of the CDDL.
#
# A full copy of the text of the CDDL should have accompanied this
# source. A copy of the CDDL is also available via the Internet at
# http://www.illumos.org/license/CDDL.
#

#
# Copyright 2019 Michal Nowak
# Copyright 2021 Nona Hansel
#

%patch% 01-test-disable-failing.patch

%hook-begin%
# This project is not on PyPI so we need to provide all info here.
# See also https://github.com/powerline/powerline/issues/2116
VERSION='2.8.4'
HOMEPAGE='https://github.com/powerline/powerline/'
DOWNLOAD_URL='https://github.com/powerline/powerline/archive/refs/tags/$(HUMAN_VERSION).tar.gz'
SUMMARY='The ultimate statusline/prompt utility'

%include-2%
COMPONENT_FMRI =	python/powerline

# Manifest options for python/powerline-vim package
COMPONENT_FMRI.powerline-vim =			$(COMPONENT_FMRI)-vim
COMPONENT_SUMMARY.powerline-vim =		$(COMPONENT_SUMMARY) - Vim editor binding
COMPONENT_CLASSIFICATION.powerline-vim =	System/Text Tools

# This is a standalone application.  Since no other Python project depends on
# it we just provide single unversioned package.
SINGLE_PYTHON_VERSION = yes
%include-3%
# Needed for connect() and socket()
CFLAGS += -lsocket -lnsl

# Make sure compiled binaries are installed at proper location
PYTHON_DATA = $(USRDIR)

# Adjust powerline configuration for our location of bindings
COMPONENT_PREP_ACTION= \
	$(GSED) -i -e "/TMUX_CONFIG_DIRECTORY/ s@BINDINGS_DIRECTORY@'/usr/share/powerline'@" $(SOURCE_DIR)/powerline/config.py

# Move bindings where we want them
PROTOUSRLIBPOWERLINE=		$(PROTOUSRLIBDIR)/python$(PYTHON_VERSION)/vendor-packages/powerline
PROTOUSRSHAREPOWERLINEDIR=	$(PROTOUSRSHAREDIR)/powerline/
PROTOUSRSHAREVIM=		$(PROTOUSRSHAREDIR)/vim/vimfiles/powerline/
COMPONENT_POST_INSTALL_ACTION +=	\
	$(MKDIR) $(PROTOUSRSHAREPOWERLINEDIR){,awesome,bash,fish,shell,tcsh,tmux,zsh} ; \
	for file in awesome/powerline.lua \
		bash/powerline.sh \
		fish/powerline-setup.fish \
		shell/powerline.sh \
		tcsh/powerline.tcsh \
		tmux/powerline-base.conf \
		tmux/powerline.conf \
		tmux/powerline_tmux_1.7_plus.conf \
		tmux/powerline_tmux_1.8.conf \
		tmux/powerline_tmux_1.8_minus.conf \
		tmux/powerline_tmux_1.8_plus.conf \
		tmux/powerline_tmux_1.9_plus.conf \
		tmux/powerline_tmux_2.1_plus.conf \
		zsh/powerline.zsh; \
		do \
			$(MV) $(PROTOUSRLIBPOWERLINE)/bindings/$$file $(PROTOUSRSHAREPOWERLINEDIR)/$$file; \
	done ; \
	$(MKDIR) $(PROTOUSRSHAREVIM) ; \
	$(MV) $(PROTOUSRLIBPOWERLINE)/bindings/vim/autoload/powerline/debug.vim $(PROTOUSRSHAREVIM) ; \
	$(MV) $(PROTOUSRLIBPOWERLINE)/bindings/vim/plugin/powerline.vim $(PROTOUSRSHAREVIM) ;

# Remove files we do not need
COMPONENT_POST_INSTALL_ACTION +=	\
	( cd $(PROTOUSRLIBPOWERLINE) ; $(RM) \
		bindings/bar/powerline-bar.py \
		bindings/lemonbar/powerline-lemonbar.py \
		commands/lemonbar.py \
		dist/systemd/powerline-daemon.service \
		lemonbar.py \
		renderers/lemonbar.py \
	) ;

# Testing needs TEST_ROOT and HOME environment variables (the directory does
# not need to exist)
COMPONENT_TEST_ENV += TEST_ROOT="$(@D)/.test_home"
COMPONENT_TEST_ENV += HOME="$(@D)/.test_home"
%hook-manifest%
# Create manifest for python/powerline-vim package
cat "$WS_TOP/transforms/manifest-metadata-template" \
	| sed -e 's/^#.*Copyright.*<contributor>.*$/# This file was automatically generated using '"$THIS"'/g' \
	> "$DISTRIBUTION-vim.p5m"
cat <<-"EOF" >> "$DISTRIBUTION-vim.p5m"
# This binding needs Vim to be functional
depend type=require fmri=editor/vim

file path=usr/share/vim/vimfiles/powerline/debug.vim
file path=usr/share/vim/vimfiles/powerline/powerline.vim
EOF
# Remove vim files from base manifest
sed -i -e '/vimfiles/d' "$MANIFEST"
# Make shell files executable
printf '<transform file path=usr/share/powerline/.*sh$ -> default mode 0555>\n' >> "$MANIFEST"
MANIFEST="$MANIFEST $DISTRIBUTION-vim.p5m"