#!/bin/sh

translation="${1:-THIS IS THE TRANSLATED LINE.}"
td=/tmp
pd=/usr/share/locale/en/LC_MESSAGES
md=$(readlink -f "$0")
md=${md%-setup.sh}
me=${md##*/}

: Cleanup
rm -f "$td/$me".[mp]o "$pd/$me".mo

if [ -e "$md".md -a -e "$MDVIEW" ]; then

  : Extract message catalog
  "$MDVIEW" --po "$md".md > "$td/$me".po
  [ -s "$td/$me".po ] && msguniq -i "$td/$me".po -o "$td/$me".po &&

  : Translate catalog
  (
    cd "$td" &&
    sed -i '$s/""/"'"$translation"'"/' "$me".po &&
    msgfmt -o "$me".mo "$me".po
  ) &&

  : Publish catalog
  ln -st "$pd" "$td/$me".mo &&

  echo "Setup OK" || {
    echo "Setup failed"
    exit 1
  }

fi

