#!/usr/bin/sh
#
# 	Part of kde-service-menu-pdf Version 2.3
# 	Copyright (C) 2007-2019 Giuseppe Benigno <giuseppe.benigno(at)gmail.com>
#
# 	This program 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.
#
# 	This program 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 this program.  If not, see <http://www.gnu.org/licenses/>.
#

new_line="
"

#### languages strings messages #################
# Syntax for strings name is: msg_[$action]_$window_[$section]
# For languages as sr@Latn use srLatn

load_language_de () {
	msg_common_software_not_found_title="Komponente wurde nicht gefunden"
	msg_common_pdfbook_not_found_text="Die pdfbook Software wurde nicht gefunden! Bitte installieren Sie texlive-extra-utils"
	msg_common_file_not_found="Datei nicht gefunden."

	msg_booklet_suffix="Broschüre"
	msg_booklet_finish_title="Neugestaltung des Dokuments \"${base_file_name}\""
	msg_booklet_finish_ok="Fertig."
	msg_booklet_finish_error="Ein Fehler ist aufgetreten."
}

load_language_en () {
	msg_common_software_not_found_title="Component not found"
	msg_common_pdfbook_not_found_text="Software pdfbook not found! Please, install texlive-extra-utils."
	msg_common_file_not_found="File not found."

	msg_booklet_suffix="Booklet"
	msg_booklet_finish_title="Repagination of the \"${base_file_name}\" document"
	msg_booklet_finish_ok="Done."
	msg_booklet_finish_error="An error has occurred."
}

load_language_fr () {
	msg_common_software_not_found_title="Composant non trouvé"
	msg_common_pdfbook_not_found_text="Logiciel pdfbook non trouvé! S'il vous plaît, installez texlive-extra-utils."
	msg_common_file_not_found="Fichier non trouvé."

	msg_booklet_suffix="Livret"
	msg_booklet_finish_title="Repagination du document \"${base_file_name}\""
	msg_booklet_finish_ok="Terminé."
	msg_booklet_finish_error="Une erreur est survenue."
}

load_language_it () {
	msg_common_software_not_found_title="Componente non trovato"
	msg_common_pdfbook_not_found_text="Il software pdfbook non è stato trovato! Per favore, installa texlive-extra-utils."
	msg_common_file_not_found="File non trovato."

	msg_booklet_suffix="Opuscolo"
	msg_booklet_finish_title="Rimpaginazione del documento \"${base_file_name}\""
	msg_booklet_finish_ok="Completata."
	msg_booklet_finish_error="Si è verificato un errore."
}

load_language_ru () {
	msg_common_software_not_found_title="Компонент не найден"
	msg_common_pdfbook_not_found_text="Программное обеспечение pdfbook не найдено! Установите texlive-extra-utils."
	msg_common_file_not_found="Файл не найден."

	msg_booklet_suffix="буклет"
	msg_booklet_finish_title="Перепечатка документа \"${base_file_name}\""
	msg_booklet_finish_ok="Готово."
	msg_booklet_finish_error="Произошла ошибка."
}

load_language () {
	## Load localized strings AFTER english strings
	## - if localized strings not found use english for default
	## - if localized strings are incomplete use english only fot missing strings :-)
	load_language_en && [ "${lang}" != "en" ] && load_language_${lang}
}

lang=${LANGUAGE%%:*}
type load_language_${lang} > /dev/null || lang='en'
load_language

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

help () {
	echo
	echo "USE:\t\t${0##*/} action files"
	echo
	echo "files:\t\tfile1 file2 ..."
	echo
	echo "example:\t${0##*/} /home/user/file1.pdf /home/user/file2.pdf"
	echo
	exit 0
}

## Transform one or many.
booklet () {
	for input in "${@}"; do
		work_dir="${input%/*}" && [ ! -d "${work_dir}" ] && work_dir="$(pwd)"
		file_name="${input##*/}"
		base_file_name="${file_name%.*}"

		## Reload language strings with current variables
		load_language

		suffix="${msg_booklet_suffix}"

		[ ! -f "${work_dir}/${file_name}" ] && shift && "${kdialog_bin}" --icon viewpdf --title "${msg_booklet_finish_title}" --passivepopup "${msg_common_file_not_found}" 5 && continue

		msg=$("${pdfbook_bin}" "${input}" 2>&1) && \
		"${kdialog_bin}" --icon viewpdf --title "${msg_booklet_finish_title}" --passivepopup "${msg_booklet_finish_ok}" 5 || \
		{ "${kdialog_bin}" --title "${msg_booklet_finish_title}" --detailederror "${msg_booklet_finish_error}" "${msg}"; exit 2; }
	done
}

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

[ "${1}" = "-h" ] || [ "${1}" = "--help" ] || [ ${#} -lt 1 ] && help

kdialog_bin=$(which kdialog)
[ -z "${kdialog_bin}" ] && echo "kdialog not found!" && exit 1

pdfbook_bin=$(which pdfbook2)
[ -z "${pdfbook_bin}" ] && "${kdialog_bin}" --title "${msg_common_software_not_found_title}" --error "${msg_common_pdfbook_not_found_text}" && exit 2

booklet "${@}"

exit 0
