#! /bin/sh
#
# Copyright (C) 1995-1998, 2000, 2001 Free Software Foundation, Inc.
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2, or (at your option)
# any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
#

# This file is meant for authors or maintainers which want to
# internationalize their package with the help of GNU gettext.  For
# further information how to use it consult the GNU gettext manual.

echo=echo
progname=$0
force=0
configstatus=0
origdir=`pwd`
usage="\
Usage: gettextize [OPTION]... [package-dir]
      --help           print this help and exit
      --version        print version information and exit
  -c, --copy           copy files instead of making symlinks
  -f, --force          force writing of new files even if old exist
Report bugs to <bug-gnu-utils@gnu.org>."
package=gettext
version=0.10.38
try_ln_s=:

while test $# -gt 0; do
  case "$1" in
    -c | --copy | --c* )
      shift
      try_ln_s=false ;;
    -f | --force | --f* )
      shift
      force=1 ;;
    -r | --run | --r* )
      shift
      configstatus=1 ;;
    --help | --h* )
      $echo "$usage"; exit 0 ;;
    --version | --v* )
      echo "$progname (GNU $package) $version"
      $echo "Copyright (C) 1995-1998, 2000, 2001 Free Software Foundation, Inc.
This is free software; see the source for copying conditions.  There is NO
warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE."
      $echo "Written by" "Ulrich Drepper"
      exit 0 ;;
    -- )	# Stop option prcessing
      shift; break ;;
    -* )
      $echo "gettextize: unknown option $1"
      $echo "Try \`gettextize --help' for more information."; exit 1 ;;
    * )
      break ;;
  esac
done

if test $# -gt 1; then
  $echo "$usage"
  exit 1
fi

# Fill in the command line options value.
if test $# -eq 1; then
  srcdir=$1
  if cd $srcdir; then
    srcdir=`pwd`
  else
    $echo "Cannot change directory to \`$srcdir'"
    exit 1
  fi
else
  srcdir=$origdir
fi

# Directory where the sources are stored.
prefix=/usr
gettext_dir=${prefix}/share/gettext

test -f configure.in || test -f configure.ac || {
  $echo "Missing configure.in or configure.ac, please cd to your package first."
  exit 1
}

if test -d intl && test $force -eq 0; then
  $echo "\
intl/ subdirectory exists: use option -f if you really want to delete it."
  exit 1
fi

if test -f po/Makefile.in.in && test $force -eq 0; then
  $echo "\
po/Makefile.in.in exists: use option -f if you really want to delete it."
  exit 1
fi

if test -f ABOUT-NLS && test $force -eq 0; then
  $echo "ABOUT-NLS exists: use option -f if you really want to delete it."
  exit 1
fi

if test -d intl; then
  # Remove everything inside intl except for RCS and CVS subdirs and invisible
  # files.
  $echo "Wiping out intl/ subdirectory"
  (cd intl &&
   for f in *; do
     if test CVS != "$f" && test RCS != "$f"; then
       rm -rf "$f"
     fi
   done)
else
  $echo "Creating intl/ subdirectory"
  mkdir intl || {
    $echo "failed to create intl/ subdirectory"
    exit 1;
  }
fi

test -d po || {
  $echo "Creating po/ subdirectory"
  mkdir po || {
    $echo "failed to create po/ subdirectory"
    exit 1
  }
}

# For simplicity we changed to the gettext source directory.
cd $gettext_dir || {
   $echo "gettext source directory '${gettext_dir}' doesn't exist"
   exit 1
}

# Now copy all files.  Take care for the destination directories.
for file in *; do
  case $file in
    intl | po)
      ;;
    *)
      rm -f $srcdir/$file
      ($try_ln_s && ln -s $gettext_dir/$file $srcdir/$file && $echo "Symlinking file $file") 2>/dev/null ||
      { $echo "Copying file $file"; cp $file $srcdir/$file; }
      ;;
  esac
done

# Copy files to intl/ subdirectory.
cd intl
for file in *; do
  rm -f $srcdir/intl/$file
  if test $file != plural.c; then
    ($try_ln_s && ln -s $gettext_dir/intl/$file $srcdir/intl/$file && $echo "Symlinking file intl/$file") 2>/dev/null ||
    { $echo "Copying file intl/$file"; cp $file $srcdir/intl/$file; }
  else
    # plural.c is a generated file; it must be copied and touched.
    $echo "Copying file intl/$file"; cp $file $srcdir/intl/$file
    sleep 2; touch $srcdir/intl/$file
  fi
done

# Copy files to po/ subdirectory.
cd ../po
for file in *; do
  rm -f $srcdir/po/$file
  ($try_ln_s && ln -s $gettext_dir/po/$file $srcdir/po/$file && $echo "Symlinking file po/$file") 2>/dev/null ||
  { $echo "Copying file po/$file"; cp $file $srcdir/po/$file; }
done
DATE=`date +%Y-%m-%d`
cat > $srcdir/po/ChangeLog.tmp <<EOF
$DATE  gettextize  <bug-gnu-utils@gnu.org>

	* Makefile.in.in: Upgrade to gettext-${version}.
EOF
if test -f $srcdir/po/cat-id-tbl.c; then
  $echo "Removing po/cat-id-tbl.c"
  rm -f $srcdir/po/cat-id-tbl.c
  $echo "	* cat-id-tbl.c: Remove file." >> $srcdir/po/ChangeLog.tmp
fi
if test -f $srcdir/po/stamp-cat-id; then
  $echo "Removing po/stamp-cat-id"
  rm -f $srcdir/po/stamp-cat-id
  $echo "	* stamp-cat-id: Remove file." >> $srcdir/po/ChangeLog.tmp
fi
$echo >> $srcdir/po/ChangeLog.tmp
if test -f $srcdir/po/ChangeLog; then
  $echo "Adding an entry to po/ChangeLog (backup is in po/ChangeLog~)"
  cat $srcdir/po/ChangeLog >> $srcdir/po/ChangeLog.tmp
  cp -p $srcdir/po/ChangeLog $srcdir/po/ChangeLog~
else
  $echo "Creating po/ChangeLog"
fi
cp $srcdir/po/ChangeLog.tmp $srcdir/po/ChangeLog
rm -f $srcdir/po/ChangeLog.tmp

echo
echo "Please add the files"
echo "  codeset.m4 gettext.m4 glibc21.m4 iconv.m4 isc-posix.m4 lcmessage.m4"
echo "  progtest.m4"
echo "from the ${prefix}/share/aclocal directory to your autoconf macro directory"
echo "or directly to your aclocal.m4 file."
echo "You will also need config.guess and config.sub, which you can get from"
echo "ftp://ftp.gnu.org/pub/gnu/config/."
echo

exit 0
