#
# Macro: %_DK6init
# Description: initial setup for DKF6 package
# Usage: %_DK6init key1 no_key2 key3 etc
# Keys:
#   <empty>     - put files to default place (recommended)
#   altplace    - put files to alternate place prefix /usr/share/dkf6
#   no_altplace - put files to system prefix /usr
#   man         - don't remove manpages
#   no_man      - remove manpages (default)
#   appdata     - don't remove appdata (default)
#   no_appdata  - remove appdata by default
# Default place may be changed in the future
# Path macros usage:
# %_DK6bin     - default switchable placement of executables
# %_dkf6_bin   - hard alternate placement of executables
# %_bindir    - hard system placement of executables

%_DK6if_ver_gt() %if "%(rpmvercmp '%1' '%2')" > "0"
%_DK6if_ver_gteq() %if "%(rpmvercmp '%1' '%2')" >= "0"
%_DK6if_ver_lt() %if "%(rpmvercmp '%2' '%1')" > "0"
%_DK6if_ver_lteq() %if "%(rpmvercmp '%2' '%1')" >= "0"
%_DK6if_ver_eq() %if "%(rpmvercmp '%1' '%2')" == "0"
%_DK6if_ver_not_gt() %if "%(rpmvercmp '%1' '%2')" <= "0"
%_DK6if_ver_not_gteq() %if "%(rpmvercmp '%1' '%2')" < "0"
%_DK6if_ver_not_lt() %if "%(rpmvercmp '%2' '%1')" <= "0"
%_DK6if_ver_not_lteq() %if "%(rpmvercmp '%2' '%1')" < "0"
%_DK6if_ver_not_eq() %if "%(rpmvercmp '%1' '%2')" != "0"

%__dkf6_altplace_default true
%__dkf6_desktop_subdir %nil
%__dkf6_opts_list ''
%__dkf6_libbindir %prefix/lib

%__dkf6_if_have_opt() \
    result_value= \
    option_name="%{1}" \
    option_yes=`echo "$option_name" | sed -e 's|^no_||'` \
    option_no=`echo "no_$option_yes"` \
    # setup defaults \
    case $option_yes in \
	altplace) \
	    result_value=%__dkf6_altplace_default \
	    ;; \
	appdata) \
	    result_value=true \
	    ;; \
    esac \
    [ -n "$result_value" ] || result_value=false \
    if echo "%__dkf6_opts_list" | grep -qe "\\<${option_yes}\\>"; then \
	result_value=true \
    fi \
    if echo "%__dkf6_opts_list" | grep -qe "\\<${option_no}\\>"; then \
	result_value=false \
    fi \
    if [ "$result_value" == "true" ] ; then \
	echo %{2} \
    else \
	echo %{3} \
    fi

%_DK6prefix %(%__dkf6_if_have_opt altplace %__dkf6_libbindir/dkf6 %_prefix)
%_DK6dir %_DK6prefix
%_dkf6_data %_datadir/dkf6
%_DK6data %(%__dkf6_if_have_opt altplace %_dkf6_data %_datadir)
%_DK6archdata %_dqt6_archdatadir
%_DK6libexecdir %prefix/libexec

%_dkf6_xdgconf %_sysconfdir/dkf6/xdg
%_DK6xdgconf %(%__dkf6_if_have_opt altplace %_dkf6_xdgconf %_sysconfdir/xdg)
%_dkf6_xdgmenu %_dkf6_xdgconf/menus
%_DK6xdgmenu %(%__dkf6_if_have_opt altplace %_dkf6_xdgmenu %_sysconfdir/xdg/menus)
%_dkf6_xdgapp %_dkf6_data/applications%__dkf6_desktop_subdir
%_DK6xdgapp %(%__dkf6_if_have_opt altplace %_dkf6_xdgapp %_desktopdir%__dkf6_desktop_subdir)
%_dkf6_xdgdir %_dkf6_data/desktop-directories
%_DK6xdgdir %(%__dkf6_if_have_opt altplace %_dkf6_xdgdir %_datadir/desktop-directories)
%_DK6xdgmime %_DK6data/mime/packages

%_dkf6_bin %__dkf6_libbindir/dkf6/bin
%_DK6bin %(%__dkf6_if_have_opt altplace %_dkf6_bin %_bindir)
%_dkf6_sbin %__dkf6_libbindir/dkf6/sbin
%_DK6sbin %(%__dkf6_if_have_opt altplace %_dkf6_sbin %_sbindir)
%_dkf6_icon %_dkf6_data/icons
%_DK6icon %(%__dkf6_if_have_opt altplace %_dkf6_icon %_iconsdir)
%_DK6lib %_dqt6_libdir
%_DK6mod %_DK6lib/kf6
%_DK6plug %_dqt6_plugindir
%_DK6qml %_dqt6_qmldir
%_DK6import %_dqt6_importdir
%_dkf6_exec %_DK6libexecdir
%_DK6exec %_DK6libexecdir/dkf6
%_dkf6_start %_dkf6_xdgconf/autostart
%_DK6start %(%__dkf6_if_have_opt altplace %_dkf6_start %_sysconfdir/xdg/autostart)
%_DK6app %_DK6data
%_DK6emo %_DK6data/emoticons
%_DK6snd %_DK6data/sounds
%_DK6tmpl %_DK6data/templates
%_DK6wall %_DK6data/wallpapers
%_DK6srv %_dkf6_data/kservices6
%_DK6srvtyp %_dkf6_data/kservicetypes6
%_DK6notif %_dkf6_data/knotifications6
%_DK6xmlgui %_dkf6_data/kxmlgui6
%_DK6doc %_DK6data/doc/HTML
%_DK6i18n %_DK6data/locale
%_DK6l10n %_DK6i18n

%_DK6cf %_DK6data/config
%_DK6conf %_DK6cf
%_DK6cfg %_DK6data/config.kcfg
%_DK6conf_up %_DK6app/kconf_update
%_DK6cf_upd %_DK6conf_up
%_DK6conf_bin %_DK6lib/kconf_update_bin
%_DK6cf_bin %_DK6conf_bin

%_DK6inc %_includedir/DKF6
%_DK6link %_libdir/dkf6/devel

%_DK6dbus %_dkf6_data/dbus-1
%_DK6conf_dbus_sessd %_sysconfdir/dkf6/dbus-1/session.d
%_DK6conf_dbus_sysd %_sysconfdir/dkf6/dbus-1/system.d
%_DK6dbus_iface %_datadir/dkf6/dbus-1/interfaces
%_DK6dbus_srv %_datadir/dkf6/dbus-1/services
%_DK6dbus_sys_srv %_datadir/dkf6/dbus-1/system-services

%DK6init() \
    %add_findpackage_path %_dkf6_bin \
    %global __dkf6_opts_list '%{*}'

# ifdef _DK6buildsubdir
%__dkf6_buildsubdir %{!?_DK6buildsubdir:BUILD}%{?_DK6buildsubdir:%?_DK6buildsubdir}
# ifdef _DK6buildtype
%__dkf6_buildtype %{!?_DK6buildtype:Release}%{?_DK6buildtype:%?_DK6buildtype}

%DK6cmake \
    __dkf6_cmake() \
    { \
	export LC_ALL=C.UTF-8; \
	export PATH=%_dqt6_bindir:%_dkf6_bin:$PATH; \
	export QTDIR=%_dqt6_prefix; \
	export CMAKE_PREFIX_PATH=%_DK6lib/cmake:$CMAKE_PREFIX_PATH; \
	export ECM_DIR=%_datadir/DDE-ECM; \
	export CFLAGS="${CFLAGS:-%optflags}"; \
	export CXXFLAGS="${CXXFLAGS:-%optflags}"; \
	pushd %__dkf6_buildsubdir; \
	cmake .. \\\
	    -GNinja \\\
	    -DCMAKE_POLICY_VERSION_MINIMUM=3.5 \\\
	    -DCMAKE_USE_RELATIVE_PATHS:BOOL=ON \\\
	    -DCMAKE_VERBOSE_MAKEFILE:BOOL=ON \\\
	    -DCMAKE_SKIP_RPATH:BOOL=OFF \\\
	    -DCMAKE_SKIP_INSTALL_RPATH:BOOL=FALSE \\\
	    -DCMAKE_INSTALL_RPATH:STRING=%_DK6lib \\\
	    -DCMAKE_BUILD_WITH_INSTALL_RPATH:BOOL=TRUE \\\
	    -DBUILD_TESTING:BOOL=FALSE \\\
	    -DKDE_INSTALL_USE_QT_SYS_PATHS:BOOL=ON \\\
	    -DCMAKE_BUILD_TYPE=%__dkf6_buildtype \\\
	    \\\
	    -DCMAKE_EXE_LINKER_FLAGS:STRING='-L%_DK6lib -L%_DK6link' \\\
	    -DCMAKE_MODULE_LINKER_FLAGS:STRING='-L%_DK6lib -L%_DK6link' \\\
	    -DCMAKE_SHARED_LINKER_FLAGS:STRING='-L%_DK6lib -L%_DK6link' \\\
	    -DCMAKE_LIBRARY_PATH='%_DK6link;%_DK6lib;%_libdir;/%_lib' \\\
	    -DCMAKE_INCLUDE_PATH='%_DK6inc;%_dqt6_headerdir;%_includedir' \\\
	    \\\
	    -DCMAKE_INSTALL_PREFIX=%_DK6prefix \\\
	    -DCMAKE_PREFIX_PATH=%_DK6prefix \\\
	    \\\
	    -DCMAKE_INSTALL_BINDIR=%_DK6bin \\\
	    -DCMAKE_INSTALL_FULL_BINDIR=%_DK6bin \\\
	    -DKDE_INSTALL_FULL_BINDIR=%_DK6bin \\\
	    -DBIN_INSTALL_DIR:PATH=%_DK6bin \\\
	    \\\
	    -DCMAKE_INSTALL_SBINDIR=%_DK6sbin \\\
	    -DCMAKE_INSTALL_FULL_SBINDIR=%_DK6sbin \\\
	    -DKDE_INSTALL_FULL_SBINDIR=%_DK6sbin \\\
	    -DSBIN_INSTALL_DIR:PATH=%_DK6sbin \\\
	    \\\
	    -DLIBEXEC_INSTALL_DIR:PATH=%_DK6libexecdir \\\
	    -DKDE_INSTALL_LIBEXECDIR:PATH=%_DK6exec \\\
		-DKDE_INSTALL_FULL_LIBEXECDIR:PATH=%_DK6exec \\\
	    -DKDE_INSTALL_FULL_LIBEXECDIR_KF:PATH=%_DK6exec \\\
	    \\\
	    -DINCLUDE_INSTALL_DIR:PATH=%_includedir \\\
	    -DCMAKE_INSTALL_INCLUDEDIR_KF6=%_DK6inc \\\
	    -DKDE_INSTALL_FULL_INCLUDEDIR=%_DK6inc \\\
	    \\\
	    -DDATA_INSTALL_DIR:PATH=%_datadir \\\
	    -DCONFIG_INSTALL_DIR:PATH=%_DK6xdgconf \\\
	    -DSERVICES_INSTALL_DIR:PATH=%_DK6srv \\\
	    -DSERVICETYPES_INSTALL_DIR:PATH=%_DK6srvtyp \\\
	    -DKNOTIFYRC_INSTALL_DIR:PATH=%_DK6notif \\\
	    -DKXMLGUI_INSTALL_DIR:PATH=%_DK6xmlgui \\\
	    -DKDE_INSTALL_FULL_APPDIR=%_DK6app \\\
	    -DKDE_INSTALL_FULL_CMAKEPACKAGEDIR=%_DK6lib/cmake \\\
	    -DKDE_INSTALL_FULL_QMLDIR=%_DK6qml \\\
	    -DKDE_INSTALL_FULL_DATADIR=%_DK6data \\\
	    -DKDE_INSTALL_FULL_LIBDIR=%_DK6lib \\\
	    \\\
	    -DLIB_INSTALL_DIR:PATH=%_DK6lib \\\
	    -DLIB_DESTINATION=%_lib \\\
	    %if "%_lib" == "lib64" \
	    -DLIB_SUFFIX="64" \\\
	    %else \
	    -DLIB_SUFFIX="" \\\
	    %endif \
	    \\\
	    -DSYSCONF_INSTALL_DIR:PATH=%_sysconfdir \\\
	    -DAUTOSTART_INSTALL_DIR=%_DK6start \\\
	    \\\
	    -DHTML_INSTALL_DIR=%_DK6doc \\\
	    -DICON_INSTALL_DIR=%_DK6icon \\\
	    -DXDG_MIME_INSTALL_DIR=%_DK6xdgmime \\\
	    -DXDG_APPS_INSTALL_DIR=%_DK6xdgapp \\\
	    -DECM_MKSPECS_INSTALL_DIR:PATH=%_DK6archdata/mkspecs/modules \\\
	    -DKCFG_INSTALL_DIR:PATH=%_DK6cfg \\\
	    -DPLUGIN_INSTALL_DIR:PATH=%_DK6plug \\\
	    -DQT_PLUGIN_INSTALL_DIR:PATH=%_DK6plug \\\
	    -DKDE_INSTALL_FULL_QTPLUGINDIR:PATH=%_DK6plug \\\
	    -DQML_INSTALL_DIR:PATH=%_DK6qml \\\
	    -DIMPORTS_INSTALL_DIR:PATH=%_DK6import \\\
	    -DDBUS_INTERFACES_INSTALL_DIR=%_DK6dbus_iface \\\
	    -DDBUS_SERVICES_INSTALL_DIR=%_DK6dbus_srv \\\
	    -DDBUS_SYSTEM_SERVICES_INSTALL_DIR=%_DK6dbus_sys_srv \\\
	    "$@"; \
	popd \
    } \
    mkdir -p %__dkf6_buildsubdir; \
    __dkf6_cmake

%DK6make \
    __dkf6_make() \
    { \
	pushd %__dkf6_buildsubdir \
	%ninja_build "$@" \
	popd \
    } \
    __dkf6_make

%DK6build \
    __dkf6_build() \
    { \
	%DK6cmake "$@" \
	%DK6make \
    } \
    __dkf6_build

%__dkf6_install_fix_files \
    if [ -d %buildroot/%_dkf6_data ]; then \
	ls -1d %buildroot/%_dkf6_data/*k*6 | \\\
	while read d ; do \
	    mv "$d" %buildroot/%_dkf6_data/ ||: \
	done \
    fi \
    if [ -d %buildroot/%_DK6data/%_lib ]; then \
	mkdir -p %buildroot/%_DK6lib \
	mv %buildroot/%_DK6data/%_lib/* %buildroot/%_DK6lib/ ||: \
	rm -rf %buildroot/%_DK6data/%_lib \
    fi \
    if [ -d %buildroot/%_DK6data/lib ]; then \
	mkdir -p %buildroot/%_DK6lib \
	mv %buildroot/%_DK6data/lib/* %buildroot/%_DK6lib/ ||: \
	rm -rf %buildroot/%_DK6data/lib \
    fi \
    if [ -d %buildroot/%_DK6prefix/%_lib ]; then \
	mkdir -p %buildroot/%_DK6lib \
	mv %buildroot/%_DK6prefix/%_lib/* %buildroot/%_DK6lib/ ||: \
    fi \
    if [ -d %buildroot/%_DK6prefix/lib ]; then \
	mkdir -p %buildroot/%_DK6lib \
	mv %buildroot/%_DK6prefix/lib/* %buildroot/%_DK6lib/ ||: \
    fi \
    if [ -d %buildroot/%_DK6prefix/include/DKF6 ]; then \
    mkdir -p %buildroot/%_DK6inc \
    mv %buildroot/%_DK6prefix/include/DKF6/* %buildroot/%_DK6inc/ ||: \
    fi \
    if [ -d %buildroot/%_DK6prefix/share ]; then \
    mkdir -p %buildroot/%_DK6data \
    mv %buildroot/%_DK6prefix/share/* %buildroot/%_DK6data/ ||: \
    fi \
    if [ "%_DK6link" != "%_DK6lib" ]; then \
	ls -1 %buildroot/%_DK6lib/lib*.so 2>/dev/null | \\\
	while read p \
	do \
	    [ -L "$p" ] || continue \
	    mkdir -p %buildroot/%_DK6link \
	    f=`readlink "$p"` \
	    l=`basename "$p"` \
	    ln -sf `relative "%buildroot/%_DK6lib/$f" "%buildroot/%_DK6link/$f"` "%buildroot/%_DK6link/$l" \
	    rm -f "$p" \
	done \
    fi \
    if [ -d %buildroot/%_DK6data/bin ]; then \
	mkdir -p %buildroot/%_dkf6_bin \
	mv %buildroot/%_DK6data/bin/* %buildroot/%_dkf6_bin/ ||: \
	rm -rf %buildroot/%_DK6data/bin \
	fi \
	if [ -d %buildroot/%_DK6data/include ]; then \
	mkdir -p %buildroot/%_DK6inc \
	mv %buildroot/%_DK6data/include/* %buildroot/%_DK6inc/ ||: \
	rm -rf %buildroot/%_DK6data/include \
	fi \
	if [ -d %buildroot/%_DK6data/kf6 ]; then \
	mkdir -p %buildroot/%_DK6data \
	mv %buildroot/%_DK6data/kf6/* %buildroot/%_DK6data/ ||: \
	rm -rf %buildroot/%_DK6data/kf6 \
	fi \
	if [ -d %buildroot/%_DK6data/dkf6 ]; then \
	mkdir -p %buildroot/%_DK6data \
	mv %buildroot/%_DK6data/dkf6/* %buildroot/%_DK6data/ ||: \
	rm -rf %buildroot/%_DK6data/dkf6 \
	fi \
	if [ -d %buildroot/%_DK6exec/dkf6 ]; then \
	mv %buildroot/%_DK6exec/dkf6/* %buildroot/%_DK6exec/ ||: \
	rm -rf %buildroot/%_DK6exec/dkf6 \
	fi \
	if [ -d %buildroot/%_DK6bin -a "%(%__dkf6_if_have_opt altplace true false)" == "true" ]; then \
	mkdir -p %buildroot/%_bindir \
	ls -1d %buildroot/%_DK6bin/*6 2>/dev/null | \\\
	while read b ; do \
	    ln -s `relative "$b" %buildroot/%_bindir/` %buildroot/%_bindir/ ||: \
	done \
	ls -1 %buildroot/%_DK6dbus_srv/*6*.service 2>/dev/null | \\\
	while read s \
	do \
	    mkdir -p %buildroot/%_dkf6_data/dbus-1/services \
	    mv $s %buildroot/%_dkf6_data/dbus-1/services/ \
	done \
    fi \
    if [ -d %buildroot/%_mandir -a "%(%__dkf6_if_have_opt man keep remove)" == "remove" ]; then \
	rm -rf %buildroot/%_mandir/* 2>/dev/null ||: \
    fi \
    if [ -d %buildroot/%_datadir/appdata -a "%(%__dkf6_if_have_opt appdata keep remove)" == "remove" ]; then \
	rm -rf %buildroot/%_datadir/appdata/* 2>/dev/null ||: \
    fi \
    if [ -d %buildroot/%_datadir/metainfo -a "%(%__dkf6_if_have_opt appdata keep remove)" == "remove" ]; then \
	rm -rf %buildroot/%_datadir/metainfo/*.{appdata,metainfo}.xml 2>/dev/null ||: \
    fi \
    if [ -d %buildroot/%_DK6lib/pkgconfig/ ]; then \
        find %buildroot/%_DK6lib/pkgconfig/ -type f -name \\*.pc | while read f ; do \
            sed -i -e 's|${prefix}/include/KF6|%_DK6inc|g' $f \
        done \
    fi \
    if [ -d %buildroot/%_DK6lib/cmake/ ]; then \
        find %buildroot/%_DK6lib/cmake/ -type f -name \\*.cmake | while read f ; do \
            sed -i -e 's|\\(set(.*\\)${PACKAGE_PREFIX_DIR}//usr/|\\1${PACKAGE_PREFIX_DIR}/|' $f \
            sed -i -e 's|/usr//usr/|/usr/|' $f \
            sed -i -e 's|${_IMPORT_PREFIX}/%_lib|%_DK6lib|g' $f \
            sed -i -e 's|${_IMPORT_PREFIX}/include/DKF6|%_DK6inc|g' $f \
            sed -i -e 's|%_DK6prefix/%_lib|%_DK6lib|g' $f \
            sed -i -e 's|%_DK6exec/dkf6|%_DK6exec|g' $f \
            sed -i -e 's|%_DK6data/kf6|%_DK6data|g' $f \
            sed -i -e 's|%_DK6data/dkf6|%_DK6data|g' $f \
            sed -i -e 's|${PACKAGE_PREFIX_DIR}/share/dkf6|%_DK6data|g' $f \
        done \
    fi

%DK6install \
    __dkf6_install() \
    { \
    pushd %__dkf6_buildsubdir \
	%ninja_install "$@" \
    popd \
	%__dkf6_install_fix_files \
    } \
    __dkf6_install

%DK6find_qtlang rpm-build-dkf6-find-qtlang

%DK6install_move \
    __dkf6_install_move() \
    { \
	if [ "%(%__dkf6_if_have_opt altplace true false)" == "false" ]; then \
	    return 0 \
	fi \
	local src_dir="%buildroot/%_prefix" \
	local dst_dir="%buildroot/%_datadir" \
	local type="$1" \
	shift \
	local item="$1" \
	case "$type" in \
	    data) \
		src_dir="%buildroot/%_datadir" \
		dst_dir="%buildroot/%_DK6data" \
	    ;; \
	    icon) \
		src_dir="%buildroot/%_datadir/icons" \
		dst_dir="%buildroot/%_DK6icon" \
	    ;; \
	    exec) \
		src_dir="%buildroot/%_DK6libexecdir" \
		dst_dir="%buildroot/%_DK6exec" \
	    ;; \
	    menu) \
		src_dir="%buildroot/%_xdgmenusdir" \
		dst_dir="%buildroot/%_DK6xdgmenu" \
	    ;; \
	    bin) \
		src_dir="%buildroot/%_bindir" \
		dst_dir="%buildroot/%_DK6bin" \
	    ;; \
	esac \
	if [ -d "$src_dir" ] ; then \
	    if [ "$item" == "all" ] ; then \
		mkdir -p "$dst_dir" \
		ls -1d "$src_dir"/* | \\\
		while read d ; do \
		    [ "`basename $d`" != "dkf6" ] || continue \
		    [ "`basename $d`" != "DKF6" ] || continue \
		    mv "$d" "$dst_dir/" ||: \
		done \
	    else \
		mkdir -p `dirname "$dst_dir/$item"` \
		while [ -n "$item" ] ; do \
		    mv "$src_dir/$item" `dirname "$dst_dir/$item"` ||: \
		    shift \
		    item="$1" \
		done \
	    fi \
	fi \
    } \
    __dkf6_install_move
