#!/bin/sh

HELPDIR=$OOINSTDIR/help

if (! test -d $HELPDIR); then
    echo "Cannot find $HELPDIR; make sure you installed OOo first"
    exit 1;
fi

for LANG in $OOO_LANGS ; do
  test "$LANG" = "en-US" -o -d $HELPDIR/$LANG && continue;
  LANGNUM=`$TOOLSDIR/bin/openoffice-xlate-lang -p $LANG`
  if test -f $SRCDIR/helpcontent_${LANGNUM}_unix.tar.bz2 -o -f $SRCDIR/helpcontent_${LANGNUM}_unix.tgz ; then
    install -m 755 -d $HELPDIR/$LANG || exit 1;
    echo "Unpacking $LANG help content..."
    if test -f $SRCDIR/helpcontent_${LANGNUM}_unix.tar.bz2 ; then
      tar -xjf $SRCDIR/helpcontent_${LANGNUM}_unix.tar.bz2 -C $HELPDIR/$LANG || exit 1;
    else
      tar -xzf $SRCDIR/helpcontent_${LANGNUM}_unix.tgz -C $HELPDIR/$LANG || exit 1;
    fi
    cd $HELPDIR/$LANG
    for arch in *.zip ; do
      if test "z$arch" != "zhelpxsl.zip" ; then
        unzip -q -o $arch
      fi
      rm $arch
    done
    # remove potential CVS directories (used for example in helpcontent_50_unix)
    find . -name CVS -type d -exec rm -rf {} \;
    cd - >/dev/null

    # fix or add highcontrast*.css
    for config in custom.css default.css \
	    highcontrastblack.css highcontrastwhite.css \
    	    highcontrast1.css highcontrast2.css ; do
      if test -f $HELPDIR/$LANG/$config ; then
        $TOOLSDIR/bin/help-font-munge $HELPDIR/$LANG/$config || exit 1;
	rm $HELPDIR/$LANG/$config.bak
      else
        cp -a $HELPDIR/en/$config $HELPDIR/$LANG/$config || exit 1;
      fi
    done

    # fix permissions and create list of files and directories
    echo "%defattr(644,root,root,755)" >$BUILDDIR/help_${LANG}_list.txt || exit 1;
    find $HELPDIR/$LANG -type d -exec chmod 755 {} \; -exec echo %dir {}/ \; >>$BUILDDIR/help_${LANG}_list.txt || exit 1;
    find $HELPDIR/$LANG -type f -exec chmod 644 {} \; -print >>$BUILDDIR/help_${LANG}_list.txt || exit 1;
    # remove DESTDIR
    sed "s|$OODESTDIR||" $BUILDDIR/help_${LANG}_list.txt >$BUILDDIR/help_${LANG}_list.txt.new
    mv -f $BUILDDIR/help_${LANG}_list.txt.new $BUILDDIR/help_${LANG}_list.txt
  else
    echo "Warning: $LANG help content is not available..."
    rm -f $BUILDDIR/help_${LANG}_list.txt
  fi
done
