#!/bin/sh

# Update this path if necessary
KDEL10N=$PWD/../kde-l10n

export LC_ALL=C
export CDPATH=

# Fix up the script
if ! grep -q Language-Team admin/cvs.sh; then
  patch admin/cvs.sh < cvs.sh.diff
fi

test -L po || ( rm -f po ; ln -s ../kde-l10n/templates/messages/tdepim po )

# I tried setting podir to get the output directly into kde-l10n but then
# we run old code in cvs.sh; scripty doesn't use that anymore, but the scripts in l10n.
# However those are harder to reduce to tdepim only, so let's keep the old way
# of doing it: with a po subdir, like a 3rd-party app and not like a real kde module.
#export podir="$KDEL10N/templates/messages/tdepim"
if ! test -d "po"; then
  echo "po doesn't exist"
  exit 1
fi

if test -z "$XGETTEXT"; then
  if test -f /usr/bin/kde-xgettext; then
    xgettext=kde-xgettext
    export XGETTEXT=kde-xgettext
  elif test -f /opt/trinity/bin/kde-xgettext; then
    xgettext=/opt/trinity/bin/kde-xgettext
    export XGETTEXT=$xgettext
  else
    xgettext=xgettext
  fi
else
  xgettext="$XGETTEXT"
fi
gettext_version=`$xgettext --version | grep 0.10.35`
if test -z "$gettext_version"; then
    echo "No xgettext installed or wrong xgettext version: "`$xgettext --version | head -n1`
    exit 1
fi

tdepim="$PWD"

# I assume tdepim is uptodate, but kde-l10n is probably not
cd "$KDEL10N" || exit 1
svn update

cd "$tdepim"
make -f admin/Makefile.common package-messages || exit 1
# ? make -f admin/Makefile.common package-merge || exit 1

cd "$KDEL10N" || exit 1

scripts/merge_all.sh

svn diff

for i in `find -name tdepim`; do
  cd $i || exit 1

  for t in `svn status 2>&1 | grep '^M' | gawk '{print $2}'`; do
    diff=`diff -u -I'^#' -I'^"POT-Creation-Date:' .svn/text-base/$t.svn-base $t`
    if test -n "$diff"; then
      echo "$t: changed"
    else
      #echo "$t: no change"
      svn revert $t
    fi
  done
  # New files are not added automatically; do those by hand.

  # To see what changed in a .pot file:  svn di kmail.pot | grep '^[-+][^#]'

  cd "$KDEL10N"
done

scripts/check_po_files

echo "Now go to $KDEL10N and commit templates/ and de/"