3 # etcdiff (deb-etcdiff?) shows how your current /etc dir
 
   4 # diverges from the debian distribution standard one.
 
   6 # Copyright (C) 2008,2009 Antonio Ospite <ospite@studenti.unina.it>
 
   7 # License: GPLv2 or later
 
  11 #   Add per package etcdiff, using dpkg -L
 
  12 #   Add per distro etcdiff using dpkg --get-selections
 
  14 # Note that the first method can't consider files not in original packages,
 
  15 # unless some smart trick is used (new files could be in same /etc subdir of
 
  16 # the provided configuration files)
 
  18 # The second one could even speedup the whole etcdiffing by recreating an etc
 
  19 # dir as per packages defaults, and do one big diff.
 
  25 DEBIANMIRROR="http://ftp.it.debian.org/debian"
 
  27 rm $PROMPT_RM -rf temp     && mkdir temp
 
  28 rm $PROMPT_RM -rf archives && mkdir archives
 
  29 rm $PROMPT_RM -rf conf     && mkdir conf
 
  31 # Examples of file query
 
  34 #FILES="/etc/sysctl.conf /etc/init.d/procps /etc/passwd-"
 
  37 #FILES=$(find /etc/apache2 -type f $USERMODE | grep -v '.dpkg-')
 
  39 # by command, public files, limited
 
  40 #USERMODE="-perm /o+r"
 
  41 #MAXDEPTH="-maxdepth 1"
 
  42 #FILES=$(find /etc/ $MAXDEPTH -type f $USERMODE | grep -v '.dpkg-')
 
  45 FILES=$(dpkg -L apache2-doc | grep '^/etc')
 
  47 FILES+=$(dpkg -L apache2.2-common | grep '^/etc')
 
  49 FILES+=$(dpkg -L libapache2-mod-php5 | grep '^/etc')
 
  55   # skip dirs, they will be put in the list of files when using 'dpkg -L'
 
  56   [ -d $file ] && continue
 
  60     echo "ERROR, file $file does not exist."
 
  65   # Find out which installed package provides the config file
 
  66   PACKAGE=$(dpkg-query -S "$file" | cut -d ':' -f 1 | uniq 2> /dev/null)
 
  68   # Copy the whole file if it is not provided by default
 
  69   if [ "x$PACKAGE" == "x" ];
 
  71     DESTDIR=`dirname $file`
 
  72     # strip trailing slash
 
  73     [ ${DESTDIR:0:1} == '/' ] && DESTDIR=${DESTDIR:1}
 
  75     # RECREATE the destination dir 
 
  76     [ -d conf/$DESTDIR ] || mkdir -p conf/$DESTDIR
 
  77     cp "$file" conf/$DESTDIR
 
  82   # Get the package from the repository and diff
 
  84   FILENAME=$(apt-cache show $PACKAGE | grep ^Filename | cut -d ' ' -f 2-)
 
  85   ARCHIVE=$(basename $FILENAME)
 
  87   if [ ! -f archives/$ARCHIVE ];
 
  90         wget -q -nc -c $DEBIANMIRROR/$FILENAME &&
 
  91         mkdir ../temp/$PACKAGE
 
  92         dpkg -x $ARCHIVE ../temp/$PACKAGE
 
  96   # Check for file existence before diffing, the file is not provided in
 
  97   # package archive, but it could have been generated by package installation
 
  99   if [ ! -f temp/$PACKAGE/$file ];
 
 101     echo "Warning: '$file' can't be found in package!"
 
 105   # TODO: diff only once and check filesize
 
 106   TMPDIFF_FILE=`mktemp /tmp/etcdiff.XXXXXXXXXX`
 
 107   diff -u temp/$PACKAGE/$file $file > $TMPDIFF_FILE
 
 108   if [ `stat -c '%s' $TMPDIFF_FILE` -eq 0 ];
 
 110     echo "$file: not changed"
 
 113     DESTDIR=`dirname $file`
 
 114     # strip trailing slash
 
 115     [ ${DESTDIR:0:1} == '/' ] && DESTDIR=${DESTDIR:1}
 
 117     # RECREATE the destination dir 
 
 118     [ -d conf/$DESTDIR ] || mkdir -p conf/$DESTDIR
 
 119     cp $TMPDIFF_FILE conf/$DESTDIR/$(basename $file).patch