gnulib-sync 9.08 KB
#! /bin/sh
#  Copyright (C) 2005 Free Software Foundation, Inc.
#
#  GNU Mailutils 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

LC_ALL=C
export LC_ALL
CVSFILES=cvsfiles.$$
CHANGELOG=changelog.$$

usage() {
 cat <<-EOF
	usage: $0 GNULIB-DIR
	$0 synchronizes mailutils library with gnulib. 

	GNULIB-DIR is the directory where gnulib sources reside. The program
	reads file gnulib.modules, populates the mailutils tree with the
	necessary files from GNULIB-DIR and updates Makefile.am-s.

	Options:

	--verbose         Display every operation
	--dry-run         Do not do anything, just print what would have been done
	--help            Display this help list

EOF
} 

error() {
  echo $* >&2
  echo "Try $0 --help for more information" >&2
  exit 1
}	

DRY_RUN=
VERBOSE=
while [ $# -ne 0 ]
do
  case $1 in
  --help)
	usage
	exit 0;;
  --dry-run)
	DRY_RUN=1; shift;;
  --verbose)
	VERBOSE=1; shift;;	
  --*)
        echo >&2 "$0: $1: unknown option"
        exit 1;;
  *)
        break;;
  esac
done

if [ $# -ne 1 ]; then
  error "Wrong number of arguments"	
fi

GNULIB_SRCDIR=$1


# Check the correctness of the invocation

if [ -r configure.ac ]; then
  if grep -q 'AC_INIT(\[GNU Mailutils' configure.ac; then
    :
  else
    error "This does not seem a mailutils package."
  fi
else
  error "Cannot find configure.ac"
fi	

if [ -r gnulib.modules ]; then
  :
else
  error "gnulib.modules does not exist."
  exit 1
fi

if [ -d $GNULIB_SRCDIR ]; then
  :
else
  echo "$GNULIB_SRCDIR: not found or is not a directory" >&2
  exit 1
fi

<$GNULIB_SRCDIR/gnulib-tool || error "gnulib-tool is not present?!?"

#####################
## Functions
#####################

get_modules() {
  while read NAME
  do
    case $NAME in
    "")     ;;
    \#*)	;;
    :*)	variable=`echo $NAME | cut -c2-`
	case $variable in
	muaux|mailutils)	;;
	*) echo "Unknown variable $variable" >&2
	   exit 1;;
	esac;;
     *)	eval ${variable}_modules=\"\$${variable}_modules $NAME\"
    esac
  done < $1
}

expand_modules() {
  eval gnulib_modules="\$${1}"
  gnulib_modules="`echo $gnulib_modules | sort -u -`"

  # gnulib-tool currently does not support --avoid with --extract-dependencies
  # Hence this mess.
  varname=$1   
  filter=''
  shift
  for mod 
  do
	filter="${filter}s/$mod//;"
  done
  if [ -z "$filter" ]; then
    filter="b"
  fi
  
  previous_gnulib_modules=
  while [ "$gnulib_modules" != "$previous_gnulib_modules" ]
  do
    previous_gnulib_modules=$gnulib_modules
    gnulib_modules=`(echo "$gnulib_modules"
		     for module in $gnulib_modules; do
	  	       $GNULIB_SRCDIR/gnulib-tool --extract-dependencies $module 
		     done) | sed "$filter" | sort -u
    `
  done
  eval ${varname}=\"$gnulib_modules\"
}

verbose() {
  [ -n "$VERBOSE" ] && echo $*
}	

do_copy() {
  case $1 in
  *.m4) verbose "${3:-Copying} $1 to $2"
        if [ -z "$DRY_RUN" ]; then
          if [ -n "$3" ]; then
            sed "$3" $1 > $2 || exit
          else
            cp $1 $2 || exit
          fi
        fi;;
  *)    verbose "Copying $1 to $2"
        if [ -z "$DRY_RUN" ]; then
          cp $1 $2 || exit
        fi;;
  esac
  echo $2 >> $CVSFILES
}			

# copy_files dir libname
copy_files() {
  if [ $# -eq 1 ]; then
    libname=$1
  else
    libname=$2
  fi
  eval list="\$${libname}_modules"
  libname=lib$libname
  files=`$GNULIB_SRCDIR/gnulib-tool --extract-filelist $list | sort -u`
	
  echo "$files" | sed 's,/[^/]*$,,' | sort -u |
  sed "s/lib/$1/;s/config/scripts/" |
  while read DIR
  do
     if [ -d $DIR ]; then
       :
     else
       echo "Directory $DIR does not exist!" >&2
       echo "Update gnulib-sync script!" >&2
       exit 1
     fi
   done
	 	
   for file in $files
   do
     dest=`echo $file | sed "s/lib/$1/;s/config\//scripts\//"`
		
     case $file in
     m4/codeset.m4) continue;;
     m4/intdiv0.m4) continue;;
     m4/inttypes-pri.m4) continue;;
     m4/isc-posix.m4) continue;;
     m4/lcmessage.m4) continue;;
     m4/onceonly_2_57.m4) dest=m4/onceonly.m4;;
     # These will be overwritten by autopoint, which still uses
     # old jm_.* macro names, so we have to keep both copies.
     m4/gettext.m4 | m4/glibc21.m4 | m4/inttypes_h.m4 | m4/lib-ld.m4 | \
     m4/lib-prefix.m4 | m4/po.m4 | m4/stdint_h.m4 | m4/uintmax_t.m4 | \
     m4/longlong.m4 | m4/ulonglong.m4)
       dest=`expr $file : '\(.*\).m4'`_gl.m4;;
     esac

     rm -f $dest &&
       do_copy $GNULIB_SRCDIR/$file $dest $3
     done
}	

create_m4_wrapper() {
  (eval list="\$${1}_modules"
   libname=lib$1
   echo "AC_DEFUN([${libname}_GNULIB],["
   for module in $list
   do
     echo "# $module"
     $GNULIB_SRCDIR/gnulib-tool --extract-autoconf-snippet $module
   done
   echo "])") | sed '/AM_GNU_GETTEXT/d'
}				

# update_makefile_am dir [libname]
update_makefile_am() {
  if [ $# -eq 1 ]; then
    libname=$1
  else
    libname=$2
  fi
  eval list="\$${libname}_modules"
  libname=lib$libname
  cat $1/Makefile.am | sed '/##:##.*##:##/,$d'
  echo "##:## EOF marker for gnulib-sync script. Please, do not remove ##:##"
  echo "## Do not change anything below this line ##"
  $GNULIB_SRCDIR/gnulib-tool --extract-automake-snippet $list |
   sed "s/lib_/${libname}_la_/g"
}

# Update EXTRA_DIST variables based on MU_LIBSOURCES and MU_LIBOBJ
# FIXME: This needs cleaning up. First, the output list should be
# filtered through `sort -u'. Secondly, the actual value of EXTRA_DIST
# must be considered too.
update_extra_dist() {
  file=$1/Makefile.am
  shift

  # The following sed program does not work: s/[\[\]]//g. A bug in sed?
  # 
  find m4 -name '*.m4' -not -name mu_libobj.m4 |
   xargs sed -n \
     '/MU_LIBSOURCES/{s/,//g;s/.*MU_LIBSOURCES *( *\[\(.*\)\]).*/\1/p;};/MU_LIBOBJ/{s/\[//g;s/\]//g;s/.*MU_LIBOBJ *(\(.*\)).*/\1.c/p;};/MU_REPLACE_FUNCS/{s/\[//g;s/\]//g;s/.*MU_REPLACE_FUNCS *(\(.*\)).*/\1.c/p;}' |
   (EXTRA_DIST=
   while read NAME; do EXTRA_DIST="$EXTRA_DIST $NAME"; done
   if [ -n "$EXTRA_DIST" ]; then 
     echo "EXTRA_DIST +=$EXTRA_DIST"
   fi)
}
  
cvs_commands() {
  echo "<<<<<<	Update by gnulib-sync. Please edit before applying" > $CHANGELOG
  echo "" >> $CHANGELOG

  echo "# CVS commands generated by gnulib-sync at `date`"
  echo "# Please edit before applying"
  
  sort $CVSFILES |
   while read NAME
   do
     dir=`expr $NAME : '\(.*\)/.*'`
     name=`expr $NAME : '.*/\(.*\)'`
     if [ -r $dir/CVS/Entries ]; then
       if grep -q $name $dir/CVS/Entries; then
	 comment="Updated"
       else
         comment="Added to the repository"
         echo "cvs add $NAME"
       fi
       echo "cvs commit -m '$comment by gnulib-sync' $NAME"
       echo "	* $NAME: $comment" >> $CHANGELOG
     fi
   done
  echo ">>>>>>" >> $CHANGELOG
}

cleanup() {
  rm -f $CVSFILES $CHANGELOG
}

#####################
## OK, let's start!
#####################

cat /dev/null > $CVSFILES
trap cleanup 1 2 13 15

get_modules gnulib.modules

expand_modules muaux_modules
expand_modules mailutils_modules dirname

MODLIST=
for mod in $muaux_modules
do
  if echo "$mailutils_modules" | grep -q " $mod " -; then
    :
  else
    MODLIST="$MODLIST $mod"
  fi
done      

muaux_modules=`echo $MODLIST | sort -u`
mailutils_modules=`echo $mailutils_modules | sort -u`

copy_files lib muaux
copy_files mailbox mailutils 's/AC_LIBSOURCES/MU_LIBSOURCES/;s/AC_LIBSOURCE/MU_LIBSOURCE/;s/AC_REPLACE_FUNCS/MU_REPLACE_FUNCS/;s/AC_LIBOBJ/MU_LIBOBJ/'

if [ -n "$DRY_RUN" ]; then
  echo "-----BEGIN gnulib.m4 FILE-----"
  create_m4_wrapper muaux
  create_m4_wrapper mailutils
  echo "-----END gnulib.m4 FILE-----"
  echo ""
  echo "-----BEGIN lib/Makefile.am FILE-----"
  update_makefile_am lib muaux
  echo "-----END lib/Makefile.am FILE-----"
  echo ""
  echo "-----BEGIN mailbox/Makefile.am FILE-----"
  update_makefile_am mailbox mailutils
  update_extra_dist mailbox m4/*.m4
  echo "-----END mailbox/Makefile.am FILE-----"
  echo "-----BEGIN CVS COMMANDS-----"
  cvs_commands
  echo "-----END CVS COMMANDS-----"
  echo "-----BEGIN ChangeLog ENTRY-----"
  cat $CHANGELOG 
  echo "-----END ChangeLog ENTRY-----"
else	
  verbose "Creating gnulib.m4"
  (echo "# This file is generated automatically. Please, do not edit."
   echo "#"
   create_m4_wrapper muaux
   create_m4_wrapper mailutils) > m4/gnulib.m4

  verbose "Creating lib/Makefile.am"
  update_makefile_am lib muaux > lib/Makefile.am.$$
  mv lib/Makefile.am.$$ lib/Makefile.am
    
  verbose "Creating mailbox/Makefile.am"
  (update_makefile_am mailbox mailutils
   update_extra_dist mailbox m4/*.m4) > mailbox/Makefile.am.$$
  mv mailbox/Makefile.am.$$ mailbox/Makefile.am

  cvs_commands > gnulib.cvs
  mv $CHANGELOG gnulib.changelog
  echo "See gnulib.cvs and gnulib.changelog"  
fi	

cleanup

exit 0