#! /bin/sh

# Test 'bulk' mode of Desktop Entry support.

tmpfiles=""
trap 'rm -fr $tmpfiles' 1 2 3 15

tmpfiles="$tmpfiles mf-desk2-po mf-desk2.desktop mf-desk2-fr.po mf-2.desktop.desired.ok mf-2.desktop.desired.out mf-2.desktop.ok mf-2.desktop.out"

cat <<\EOF > mf-desk2.desktop
[Desktop Entry]
Type=Application
Name =Foo

Comment[foo]=Already translated comment
Comment= \sThis is a \nmultiline comment; for testing
# This is a comment and must be preserved
Keywords=Keyword1;Keyword2;Key\;word3;
EOF

test -d mf-desk2-po || mkdir mf-desk2-po

cat <<\EOF > mf-desk2-po/fr.po
# SOME DESCRIPTIVE TITLE.
# Copyright (C) YEAR THE PACKAGE'S COPYRIGHT HOLDER
# This file is distributed under the same license as the PACKAGE package.
# FIRST AUTHOR <EMAIL@ADDRESS>, YEAR.
#
#, fuzzy
msgid ""
msgstr ""
"Project-Id-Version: PACKAGE VERSION\n"
"Report-Msgid-Bugs-To: \n"
"POT-Creation-Date: 2014-03-17 07:36+0900\n"
"PO-Revision-Date: 2014-03-17 08:40+0900\n"
"Last-Translator: FULL NAME <EMAIL@ADDRESS>\n"
"Language-Team: LANGUAGE <LL@li.org>\n"
"Language: \n"
"MIME-Version: 1.0\n"
"Content-Type: text/plain; charset=UTF-8\n"
"Content-Transfer-Encoding: 8bit\n"

#: xg.desktop:4
msgid "Foo"
msgstr ""
"French\n"
"foo"

#: xg.desktop:5
msgid ""
" This is a \n"
"multiline comment; for testing"
msgstr ""
"French \n"
"comment"

#: xg.desktop:7
msgid "Keyword1;Keyword2;Key\\;word3;"
msgstr "one;two;thr\\;ee;"
EOF

cat <<\EOF > mf-desk2-po/de.po
# SOME DESCRIPTIVE TITLE.
# Copyright (C) YEAR THE PACKAGE'S COPYRIGHT HOLDER
# This file is distributed under the same license as the PACKAGE package.
# FIRST AUTHOR <EMAIL@ADDRESS>, YEAR.
#
#, fuzzy
msgid ""
msgstr ""
"Project-Id-Version: PACKAGE VERSION\n"
"Report-Msgid-Bugs-To: \n"
"POT-Creation-Date: 2014-03-17 07:36+0900\n"
"PO-Revision-Date: 2014-03-17 08:40+0900\n"
"Last-Translator: FULL NAME <EMAIL@ADDRESS>\n"
"Language-Team: LANGUAGE <LL@li.org>\n"
"Language: \n"
"MIME-Version: 1.0\n"
"Content-Type: text/plain; charset=UTF-8\n"
"Content-Transfer-Encoding: 8bit\n"

#: xg.desktop:4
msgid "Foo"
msgstr ""
"German\n"
"foo"

#: xg.desktop:5
msgid ""
" This is a \n"
"multiline comment; for testing"
msgstr ""
"German \n"
"comment"

#: xg.desktop:7
msgid "Keyword1;Keyword2;Key\\;word3;"
msgstr "one;two;thr\\;ee;"
EOF

cat <<\EOF > mf-2.desktop.ok
[Desktop Entry]
Type=Application
Name[de]=German\nfoo
Name[fr]=French\nfoo
Name=Foo

Comment[foo]=Already translated comment
Comment[de]=German \ncomment
Comment[fr]=French \ncomment
Comment=\sThis is a \nmultiline comment; for testing
# This is a comment and must be preserved
Keywords[de]=one;two;thr\;ee;
Keywords[fr]=one;two;thr\;ee;
Keywords=Keyword1;Keyword2;Key\;word3;
EOF

cat <<\EOF > mf-2.desktop.desired.ok
[Desktop Entry]
Type=Application
Name[fr]=French\nfoo
Name=Foo

Comment[foo]=Already translated comment
Comment[fr]=French \ncomment
Comment=\sThis is a \nmultiline comment; for testing
# This is a comment and must be preserved
Keywords[fr]=one;two;thr\;ee;
Keywords=Keyword1;Keyword2;Key\;word3;
EOF

unset LINGUAS

# Sanity checks for contradicting options.

${MSGFMT} --desktop --template=mf-desk2.desktop -d mf-desk2-po -o mf-2.desktop.out \
          >/dev/null 2>/dev/null \
  exit 1

test -d mf-desk2-po/LINGUAS || mkdir mf-desk2-po/LINGUAS

${MSGFMT} --desktop --template=mf-desk2.desktop -d mf-desk2-po -o mf-2.desktop.out \
          >/dev/null 2>/dev/null \
  exit 1

rm -fr mf-desk2-po/LINGUAS

cat <<\EOF > mf-desk2-po/LINGUAS
de
fr
EOF

${MSGFMT} --desktop --template=mf-desk2.desktop -d mf-desk2-po \
          >/dev/null 2>/dev/null \
  && exit 1

${MSGFMT} --desktop --template=mf-desk2.desktop -d mf-desk2-po -o mf-2.desktop.out -l fr \
          >/dev/null 2>/dev/null \
  && exit 1

${MSGFMT} --desktop --template=mf-desk2.desktop -d mf-desk2-po -o mf-2.desktop.out mf-desk2-po/fr.po \
          >/dev/null 2>/dev/null \
  && exit 1

# Proceed to the .desktop file generation.

${MSGFMT} --desktop --template=mf-desk2.desktop -d mf-desk2-po -o mf-2.desktop.out || exit 1

: ${DIFF=diff -b}
${DIFF} mf-2.desktop.ok mf-2.desktop.out
test $? = 0 || exit 1

# Restrict the desired languages with the LINGUAS envvar.

LINGUAS="fr ja" ${MSGFMT} --desktop --template=mf-desk2.desktop -d mf-desk2-po -o mf-2.desktop.desired.out || exit 1

: ${DIFF=diff -b}
${DIFF} mf-2.desktop.desired.ok mf-2.desktop.desired.out
test $? = 0 || exit 1

rm -fr $tmpfiles

exit 0
