#! /bin/sh

unset MAKEFLAGS
MAKE=/usr/bin/make

package_merge()
{
catalogs=$POFILES
for cat in $catalogs; do
  msgmerge -o $cat.new $cat $PACKAGE.pot
  if test -s $cat.new; then
    grep -v "\"POT-Creation" $cat.new > $cat.new.2
    grep -v "\"POT-Creation" $cat >> $cat.new.1
    if diff $cat.new.1 $cat.new.2; then
	rm $cat.new
    else
	mv $cat.new $cat
    fi
    rm -f $cat.new.1 $cat.new.2
  fi
done
}

extract_messages()
{
podir=${podir:-$PWD/po}
files=`find . -name '*.po' -type f`
dirs=`for i in $files; do echo \`dirname $i\`; done`
tmpname="$PWD/messages.log"
if test -z "$EXTRACTRC"; then EXTRACTRC=extractrc ; fi
if test -z "$PREPARETIPS"; then PREPARETIPS=preparetips ; fi
export EXTRACTRC PREPARETIPS

for subdir in $dirs; do
  test -z "$VERBOSE" || echo "Making messages in $subdir"
  (cd $subdir
   candidates=`ls -1 *.rc *.ui *.kcfg 2>/dev/null`
   if test -n "$candidates"; then
   	echo "$subdir has *.rc, *.ui or *.kcfg files, but not correct messages line"
   fi
   if find . -name \*.c\* -o -name \*.h\* | fgrep -v ".svn" | xargs fgrep -s -q KAboutData ; then
	echo -e 'i18n("_: NAME OF TRANSLATORS\\n"\n"Your names")\ni18n("_: EMAIL OF TRANSLATORS\\n"\n"Your emails")' > _translatorinfo.cpp
   else echo " " > _translatorinfo.cpp
   fi
  
   kdepotpath=${includedir:-`kde-config --expandvars --install include`}/kde.pot
   if ! test -f $kdepotpath; then
	kdepotpath=`kde-config --expandvars --prefix`/include/kde.pot
   fi

   ${XGETTEXT:-xgettext} --foreign-user -C -ci18n -ki18n -ktr2i18n -kI18N_NOOP -kI18N_NOOP2 -kaliasLocale -x $kdepotpath messages
   exit_code=$?
   if test "$exit_code" != 0; then
       echo "make exit code: $exit_code"
   fi
   ) 2>&1 | grep -v '^make\[1\]' > $tmpname
   test -s $tmpname && { echo $subdir ; cat "$tmpname"; }
   test -f $subdir/rc.cpp && rm -f $subdir/rc.cpp
   rm -f $subdir/_translatorinfo.cpp
   rm -f $subdir/_transMakefile
done
rm -f $tmpname
}

package_messages()
{
rm -rf po.backup
mkdir po.backup

for i in `ls -1 po/*.pot 2>/dev/null | sed -e "s#po/##"`; do
  egrep -v '^#[^,]' po/$i | egrep '^.*[^ ]+.*$' | grep -v "\"POT-Creation" > po.backup/$i
  cat po/$i > po.backup/backup_$i
  touch -r po/$i po.backup/backup_$i
  rm po/$i
done

extract_messages

for i in `ls -1 po.backup/*.pot 2>/dev/null | sed -e "s#po.backup/##" | egrep -v '^backup_'`; do
  test -f po/$i || echo "disappeared: $i"
done
for i in `ls -1 po/*.pot 2>/dev/null | sed -e "s#po/##"`; do
  sed -e 's,^"Content-Type: text/plain; charset=CHARSET\\n"$,"Content-Type: text/plain; charset=UTF-8\\n",' po/$i > po/$i.new && mv po/$i.new po/$i
  #msgmerge -q -o po/$i po/$i po/$i
  egrep -v '^#[^,]' po/$i | egrep '^.*[^ ]+.*$' | grep -v "\"POT-Creation" > temp.pot
  if test -f po.backup/$i && ! cmp -s temp.pot po.backup/$i; then
    echo "will update $i"
  else
    if test -f po.backup/backup_$i; then
      test -z "$VERBOSE" || echo "I'm restoring $i"
      mv po.backup/backup_$i po/$i
      rm po.backup/$i
    else
      echo "will add $i"
    fi
  fi
done
rm -f temp.pot
rm -rf po.backup
}

# Make sure that sorting is always done the same way
LC_ALL=C
export LC_ALL
unset LANG || :
unset LC_CTYPE || :
unset LANGUAGE || :

unset CDPATH || :
admindir=`echo "$0" | sed 's%[\\/][^\\/][^\\/]*$%%'`
test "x$admindir" = "x$0" && admindir=.

###
### Main
###

arg=`echo $1 | tr .- __`
case $arg in
  package_merge | package_messages | extract_messages ) $arg ;;
  * ) echo "Usage: $0 <target>"
      echo "Target can be one of:"
      echo "    package-merge package-messages extract_messages"
      echo ""
      echo "Usage: anything but $1"
      exit 1 ;;
esac


exit 0
