#! /bin/sh
#
# 	Part of kde-service-menu-pdf Version 1.0
# 	Copyright (C) 2007-2018 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/>.
#

if [ ${#} -eq 0 ] || [ "${1}" = "-h" ] || [ "${1}" = "--help" ]; then
	echo
	echo "USE:\t\t${0##*/} files"
	echo
	echo "files:\t\tfile1 file2 ..."
	echo
	echo "example:\t${0##*/} en /home/user/file1.pdf /home/user/file2.pdf"
	echo
	exit 0
fi

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

load_language_de () {
	msg_alert_gs_not_found_title="Komponente wurde nicht gefunden"
	msg_alert_gs_not_found_text="Die gs Software wurde nicht gefunden! Bitte installieren Sie Ghostscript."

	msg_alert_pdfinfo_not_found_title="Komponente wurde nicht gefunden"
	msg_alert_pdfinfo_not_found_text="Die pdfinfo Software wurde nicht gefunden! Bitte installieren Sie pdfinfo."

	msg_insert_password_title="Passwort eingeben"
	msg_insert_password_usr="Bitte das Benutzerpasswort für \"${file_name}\" eingeben:"
	msg_insert_password_own="Bitte das Masterpasswort für \"${file_name}\" eingeben:"

	msg_info_title="Informationen"
	msg_file_not_found="Datei nicht gefunden."
}

load_language_en () {
	msg_alert_gs_not_found_title="Component not found"
	msg_alert_gs_not_found_text="Software gs not found! Please, install Ghostscript."

	msg_alert_pdfinfo_not_found_title="Component not found"
	msg_alert_pdfinfo_not_found_text="Software pdfinfo not found! Please, install it."

	msg_insert_password_title="Insert password"
	msg_insert_password_usr="Please insert user password of document \"${file_name}\":"
	msg_insert_password_own="Please insert main password of document \"${file_name}\":"

	msg_info_title="Information"
	msg_file_not_found="File not found."
}

load_language_fr () {
	msg_alert_gs_not_found_title="Composant non trouvé"
	msg_alert_gs_not_found_text="Logiciel Ghostscript non trouvé! S'il vous plaît, installez-le."

	msg_alert_pdfinfo_not_found_title="Composant non trouvé"
	msg_alert_pdfinfo_not_found_text="Logiciel pdfinfo non trouvé! S'il vous plaît, installez-le."

	msg_insert_password_title="Saisissez le mot de passe"
	msg_insert_password_usr="Saisissez le mot de passe utilisateur pour \"${file_name}\":"
	msg_insert_password_own="Saisissez le mot de passe Administrateur pour \"${file_name}\":"

	msg_info_title="Informations"
	msg_file_not_found="Fichier non trouvé."
}

load_language_it () {
	msg_alert_gs_not_found_title="Componente non trovato"
	msg_alert_gs_not_found_text="Il software gs non è stato trovato! Per favore installa Ghostscript."

	msg_alert_pdfinfo_not_found_title="Componente non trovato"
	msg_alert_pdfinfo_not_found_text="Il software pdfinfo non è stato trovato! Per favore, installalo."

	msg_insert_password_title="Inserisci password"
	msg_insert_password_usr="Per favore digita la password utente per \"${file_name}\":"
	msg_insert_password_own="Per favore digita la password principale \"${file_name}\":"

	msg_info_title="Informazioni"
	msg_file_not_found="File non trovato."
}

load_language_ru () {
	msg_alert_gs_not_found_title="Компонент не найден"
	msg_alert_gs_not_found_text="Программное обеспечение gs не найдено! Установите Ghostscript."

	msg_alert_pdfinfo_not_found_title="Компонент не найден"
	msg_alert_pdfinfo_not_found_text="Программное обеспечение pdfinfo не найдено! Установите pdfinfo."

	msg_insert_password_title="Вставить пароль"
	msg_insert_password_usr="Пожалуйста, вставьте пароль пользователя для документа \"${file_name}\":"
	msg_insert_password_own="Пожалуйста, вставьте основной пароль для документа \"${file_name}\":"

	msg_info_title="Информация"
	msg_file_not_found="Файл не найден."
}

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

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

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

gs_bin=$(which gs)
[ -z "${gs_bin}" ] && "${kdialog_bin}" --title "${msg_alert_gs_not_found_title}" --icon configure --error "${msg_alert_gs_not_found_text}" && exit 2

pdfinfo_bin=$(which pdfinfo)
[ -z "${pdfinfo_bin}" ] && "${kdialog_bin}" --title "${msg_alert_pdfinfo_not_found_title}" --icon configure --error "${msg_alert_pdfinfo_not_found_text}" && exit 2

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

checkIfProtected () {
	output=$("${gs_bin}" -dBATCH -sNODISPLAY "${1}" 2>&1)
	gsexit=$?

	if [ "$gsexit" = "0" ]; then
		is_protected=0;
	else
		found=$(echo "$output" | grep -o "This file requires a password")
		if [ -n "$found" ]; then
			is_protected=1;
		else
			"${kdialog_bin}" --title "${msg_finish_title}" --passivepopup "${msg_finish_error}" 5
			exit $gsexit
		fi
	fi
}

info () {
	checkIfProtected $1

	if [ ${is_protected} = 1 ]; then
		password=$("${kdialog_bin}" --title "${msg_insert_password_title}" --password "${msg_insert_password_usr}") || exit 0
		password=$(echo ${password} |grep '^.' |sed 's/^/-upw /')

		if [ -z "${password}" ]; then
			password=$("${kdialog_bin}" --title "${msg_insert_password_title}" --password "${msg_insert_password_usr}") || exit 0
			password=$(echo ${password} |grep '^.' |sed 's/^/-opw /')
		fi

		[ -z "${password}" ] && password=''
	fi

	newline="\n"
	msg=""
	while [ $# -ne 0 ]; do
		work_dir="${1%/*}"; [ ! -d "${work_dir}" ] && work_dir="$(pwd)"
		file_name="${1##*/}"

		[ ! -f "${work_dir}/${file_name}" ] && shift && "${kdialog_bin}" --title "${msg_finish_title}" --passivepopup "${msg_file_not_found}" 5 && continue
		[ -n "${msg}" ] && msg="${msg}${newline}"
		msg="${msg}File: ${file_name}${newline}"
 		msg="${msg}$("${pdfinfo_bin}" "${work_dir}/${file_name}")${newline}"

 		shift
	done

	"${kdialog_bin}" --title "${msg_info_title}" --icon help-about --msgbox "${msg}"
}

#### main

info "${@}" && exit 0
