File manage-translations of Package kmymoney4

#!/bin/sh
#
# Manage KDE translations on OBS
#
# @author: Ralf Habacker <ralf.habacker@freenet.de>
# @author: Thomas Baumgart <thb@net-bembel.de>
#
# Syntax: 
#     manage-translations <mode> [<options>]
# 
# Options:
#    --fetch[5] [<svnrootpath>] [<msgrelpath>] [<docrelpath>]
#    --clean[5]
#    --pack[5] (includes --clean)
#    --unpack[5] <tar ball>

KDE_VERSION=4

fetch_translations () {
	if ! test -d po; then
		mkdir -p po
	fi
	cd po

	# setup defaults
	case $KDE_VERSION in
	  5)
		SVNROOTPATH=svn://anonsvn.kde.org/home/kde/branches/stable/l10n-kf5/
		APPSVNROOTPATH=$SVNROOTPATH
		MSG_RELPATH=messages/extragear-office
		DOC_RELPATH=docs/extragear-office/kmymoney
		;;
	  4)
		SVNROOTPATH=svn://anonsvn.kde.org/home/kde/branches/stable/l10n-kde4/
		APPSVNROOTPATH=$SVNROOTPATH
		MSG_RELPATH=messages/extragear-office
		DOC_RELPATH=docs/extragear-office/kmymoney

		cat > CMakeLists.txt <<EOF
# Search KDE installation
find_package(KDE4 REQUIRED)
find_package(Gettext REQUIRED)
include (KDE4Defaults)
include(MacroOptionalAddSubdirectory)

if (NOT GETTEXT_MSGMERGE_EXECUTABLE)
MESSAGE(FATAL_ERROR "Please install the msgmerge binary")
endif (NOT GETTEXT_MSGMERGE_EXECUTABLE)

IF(NOT GETTEXT_MSGFMT_EXECUTABLE)
MESSAGE(FATAL_ERROR "Please install the msgfmt binary")
endif (NOT GETTEXT_MSGFMT_EXECUTABLE)
EOF
		;;
	esac

	if test -n "$2"; then
		SVNROOTPATH=$2
	fi
	if test -n "$3"; then
		MSG_RELPATH=$3
	fi
	if test -n "$4"; then
		DOC_RELPATH=$4
	fi
	MSG_SVNPATH=$APPSVNROOTPATH/@LANG@/$MSG_RELPATH
	DOC_SVNPATH=$APPSVNROOTPATH/@LANG@/$DOC_RELPATH

	svn co --depth=files $SVNROOTPATH .

	for i in $(cat subdirs); do
		mkdir -p $i/docs/kmymoney
		if ! test -d "$i/.svn"; then
			(cd $i; p=$(echo $MSG_SVNPATH | sed "s,@LANG@,$i,g"); svn co $p .)
			(cd $i; p=$(echo $DOC_SVNPATH | sed "s,@LANG@,$i,g"); svn co $p docs/kmymoney)
			# remove docs dir if translated docs are not present
			rmdir --ignore-fail-on-non-empty -p $i/docs/kmymoney
		fi
		if test -f $i/kmymoney.po; then
			case $KDE_VERSION in
			  5)
				;;
			  4)
				echo "add_subdirectory($i)" >>CMakeLists.txt
				echo "GETTEXT_PROCESS_PO_FILES($i ALL INSTALL_DESTINATION \${LOCALE_INSTALL_DIR} kmymoney.po)" >$i/CMakeLists.txt
				;;
			esac
		fi
	done
}

clean_translations() {
	# remove svn internal files
	find po -name .svn -exec rm -rf {} \;
	# remove non kmymoney files
	find po -name *.po	| grep -v kmymoney | xargs rm
	# remove non program binary translation files
	find po -name *_* -a -type f | xargs rm
	find po -name org.kde* | xargs rm
	# remove files that do not belong into a package
	rm -rf po/x-test
	rm po/README po/subdirs po/teamnames
	# remove emtpy directories
	rmdir --ignore-fail-on-non-empty po/*
}

pack_translations() {
	# collect remaining files
	tar -cJf translations.tar.xz po
}

#
# supports tar ball with the following directory layout
#   po/...
#   <some-dir>/po/...
# 
unpack_translations() {
	mkdir tmp
	cd tmp
	t=$(echo $1 | grep "7z")
	if test -n "$t"; then
		7zr x $1
	else    
		tar -xJf $1
	fi
	if test -d po; then
		cp -a po ..
	else
		cp -a */po ..
	fi
	cd ..
	rm -rf tmp
	patch_cmake_lists_file
}

patch_cmake_lists_file() {
	case $KDE_VERSION in
	  5)
		a=$(grep ki18n_install CMakeLists.txt)
		if test -z "$a"; then
			cat >> CMakeLists.txt <<EOF
find_package(KF5I18n CONFIG REQUIRED)
ki18n_install(po)

find_package(KF5DocTools CONFIG)
if(KF5DocTools_FOUND)
    kdoctools_install(po)
endif()
EOF
		fi
		;;
	  4)
		a=$(grep "macro_optional_add_subdirectory( po )" CMakeLists.txt)
		if test -z "$a"; then
			cat >> CMakeLists.txt <<EOF
include(MacroOptionalAddSubdirectory)
macro_optional_add_subdirectory( po )
EOF
		fi
		;;
	esac
}

case $1 in
--*5)
	KDE_VERSION=5
	;;
esac

case $1 in
--fetch*)
	fetch_translations
	;;
--clean*)
	clean_translations
	;;
--pack*)
	clean_translations
	pack_translations
	;;
--unpack*)
	unpack_translations $2
	;;
esac
openSUSE Build Service is sponsored by