#! /bin/sh

# Test checking of Lisp format strings.

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

tmpfiles="$tmpfiles f-l-2.data"
cat <<\EOF > f-l-2.data
# Valid: ~~ doesn't count
msgid  "abc~~def"
msgstr "xyz"
# Invalid: invalid msgstr
msgid  "abc~~def"
msgstr "xyz~"
# Valid: same argument
msgid  "abc~Ddef"
msgstr "xyz~D"
# Valid: same arguments
msgid  "abc~1000000@*~Ddef"
msgstr "~1000000@*xyz~D"
# Valid: permutation
msgid  "abc~D~C~Fdef"
msgstr "xyz~2@*~F~2:*~C~2:*~D"
# Invalid: too few arguments
msgid  "abc~1@*~Sdef~0@*~S"
msgstr "xyz~S"
# Invalid: too many arguments
msgid  "abc~Ddef"
msgstr "xyz~Duvw~C"
# Invalid: missing non-final argument
msgid  "abc~1@*~Sdef~0@*~F"
msgstr "xyz~1@*~S"
# Invalid: added non-final argument
msgid  "abc~1@*~Ddef"
msgstr "xyz~F~D"
# Valid: formatting variations
msgid  "abc~Sdef"
msgstr "xyz~:S"
# Valid: formatting variations
msgid  "abc~4Sdef"
msgstr "xyz~7S"
# Valid: type compatibility
msgid  "abc~Sdef"
msgstr "xyz~W"
# Valid: type compatibility
msgid  "abc~Ddef"
msgstr "xyz~B"
# Valid: type compatibility
msgid  "abc~Ddef"
msgstr "xyz~O"
# Valid: type compatibility
msgid  "abc~Ddef"
msgstr "xyz~X"
# Valid: type compatibility
msgid  "abc~Ddef"
msgstr "xyz~R"
# Valid: optional plural
msgid  "~D egg~:P"
msgstr "~D Eier"
# Valid: type compatibility
msgid  "abc~Fdef"
msgstr "xyz~E"
# Valid: type compatibility
msgid  "abc~Fdef"
msgstr "xyz~G"
# Valid: type compatibility
msgid  "abc~Fdef"
msgstr "xyz~$"
# Invalid: type incompatibility
msgid  "abc~Fdef"
msgstr "xyz~D"
# Invalid: type incompatibility
msgid  "abc~Ddef"
msgstr "xyz~C"
# Invalid: type incompatibility
msgid  "abc~Fdef"
msgstr "xyz~C"
# Invalid: type incompatibility
msgid  "abc~Sdef"
msgstr "xyz~D"
# Invalid: type incompatibility
msgid  "abc~Sdef"
msgstr "xyz~F"
# Invalid: type incompatibility
msgid  "abc~Sdef"
msgstr "xyz~C"
# Valid: extra formatting
msgid  "abc~Ddef~S"
msgstr "xyz~D~_def~%~S"
# Valid: FORMAT-GOTO doesn't consume an argument by itself
msgid  "abc~5@*~Ddef~C"
msgstr "xyz~6@*~C~2@*uvw~5@*~R"
# Invalid: @ matters
msgid  "abc~?"
msgstr "xyz~@?"
# Valid: case conversion is optional
msgid  "abc~(~S~)"
msgstr "xyz~S"
# Valid: case conversion is optional
msgid  "abc~(~D~^ ~D~)"
msgstr "xyz~D~^ ~D"
# Invalid: ~^ matters
msgid  "abc~(~D~^ ~D~)"
msgstr "xyz~(~D ~D~)"
# Valid: case conversion is optional, and it doesn't catch ~^
msgid  "abc~(~D~^ ~D~)def~C"
msgstr "xyz~D~^ ~Duvw~C"
# Valid: synoymous conditionals
msgid  "abc~@[~D~]def~C"
msgstr "xyz~:[~;~:*~D~]~C"
# Valid: conditionals are optional and don't catch ~^
msgid  "abc~:[~;~]~^~D~C"
msgstr "xyz~:[uvw~^~R~;~:*~S~^~D~]~C"
# Invalid: ~:[ requires an argument, ~1@* doesn't
msgid  "abc~1@*~^~D~C"
msgstr "xyz~:[uvw~^~R~;~:*~S~^~D~]~C"
# Invalid: ~{~} is special
msgid  "abc~{ ~}"
msgstr "xyz~{~}"
# Invalid: @ matters
msgid  "abc~{~S~}"
msgstr "xyz~@{~S~}"
# Invalid: @ matters
msgid  "abc~:{~S~}"
msgstr "xyz~@:{~S~}"
# Invalid: : matters
msgid  "abc~{~S~}"
msgstr "xyz~:{~S~}"
# Invalid: : matters
msgid  "abc~@{~S~}"
msgstr "xyz~@:{~S~}"
# Invalid: @: matters
msgid  "abc~{~S~}"
msgstr "xyz~@:{~S~}"
# Valid: permutation inside iteration
msgid  "abc~{~D ~C~}"
msgstr "xyz~{~1@*~C~2:*~D~1*~}"
# Invalid: permutation inside iteration, but wrong position at iteration end
msgid  "abc~{~D ~C~}"
msgstr "xyz~{~1@*~C~2:*~D~}"
# Invalid: ~^ inside iteration matters
msgid  "abc~{~D ~^~C~}"
msgstr "xyz~{~D ~C~}"
# Invalid: ~^ inside iteration depends on position
msgid  "abc~{~D ~^~C~}"
msgstr "xyz~{~1@*~C~^~2:*~D~1*~}"
# Invalid: ~^ inside iteration depends on position
msgid  "abc~{~D ~^~C~}"
msgstr "xyz~{~1@*~C~2:*~^~D~1*~}"
# Invalid: ~^ inside iteration depends on position
msgid  "abc~{~D ~^~C~}"
msgstr "xyz~{~1@*~C~2:*~D~^~1*~}"
# Invalid: ~^ inside iteration depends on position
msgid  "abc~{~D ~^~C~}"
msgstr "xyz~{~1@*~C~2:*~D~1*~^~}"
# Valid: justification is optional
msgid  "abc~<~D~;~C~>"
msgstr "xyz~D~C"
# Valid: justification with different segmentation
msgid  "abc~<~D ~D~;~C~;~S~S~>"
msgstr "xyz~<~D~;~D~C~S~;~S~>"
# Invalid: justification catches ~^
msgid  "abc~<~D~;~^~D~>~C"
msgstr "abc~D~^~D~C"
EOF

: ${MSGFMT=msgfmt}
n=0
while read comment; do
  read msgid_line
  read msgstr_line
  n=`expr $n + 1`
  tmpfiles="$tmpfiles f-l-2-$n.po f-l-2-$n.mo"
  cat <<EOF > f-l-2-$n.po
#, lisp-format
${msgid_line}
${msgstr_line}
EOF
  fail=
  if echo "$comment" | grep 'Valid:' > /dev/null; then
    if ${MSGFMT} --check-format -o f-l-2-$n.mo f-l-2-$n.po; then
      :
    else
      fail=yes
    fi
  else
    ${MSGFMT} --check-format -o f-l-2-$n.mo f-l-2-$n.po 2> /dev/null
    if test $? = 1; then
      :
    else
      fail=yes
    fi
  fi
  if test -n "$fail"; then
    echo "Format string checking error:" 1>&2
    cat f-l-2-$n.po 1>&2
    exit 1
  fi
  rm -f f-l-2-$n.po f-l-2-$n.mo
done < f-l-2.data

rm -fr $tmpfiles

exit 0
