Reimplementation.
[etcdiff.git] / etcdiff.sh
index 3a022b6..3014c4b 100755 (executable)
-#!/bin/bash
+#!/bin/sh
 #
 # etcdiff (deb-etcdiff?) shows how your current /etc dir
 # diverges from the debian distribution standard one.
 #
-# Copyright (C) 2008,2009 Antonio Ospite <ospite@studenti.unina.it>
+# Copyright (C) 2008,2009,2010 Antonio Ospite <ospite@studenti.unina.it>
 # License: GPLv2 or later
-
-
-# TODO:
-#   Add per package etcdiff, using dpkg -L
-#   Add per distro etcdiff using dpkg --get-selections
-#
-# Note that the first method can't consider files not in original packages,
-# unless some smart trick is used (new files could be in same /etc subdir of
-# the provided configuration files)
 #
-# The second one could even speedup the whole etcdiffing by recreating an etc
-# dir as per packages defaults, and do one big diff.
+#set -x
+#set -e
 
-set -x
-
-#PROMPT_RM=-i
+PROMPT_RM=-i
 
 DEBIANMIRROR="http://ftp.it.debian.org/debian"
 
-rm $PROMPT_RM -rf temp     && mkdir temp
-rm $PROMPT_RM -rf archives && mkdir archives
-rm $PROMPT_RM -rf conf     && mkdir conf
-
-# Examples of file query
-
-# by list
-#FILES="/etc/sysctl.conf /etc/init.d/procps /etc/passwd-"
-
-# by command
-#FILES=$(find /etc/apache2 -type f $USERMODE | grep -v '.dpkg-')
-
-# by command, public files, limited
-#USERMODE="-perm /o+r"
-#MAXDEPTH="-maxdepth 1"
-#FILES=$(find /etc/ $MAXDEPTH -type f $USERMODE | grep -v '.dpkg-')
-
-# by package name
-FILES=$(dpkg -L apache2-doc | grep '^/etc')
-FILES+=" "
-FILES+=$(dpkg -L apache2.2-common | grep '^/etc')
-FILES+=" "
-FILES+=$(dpkg -L libapache2-mod-php5 | grep '^/etc')
-
+BASEDIR=$(dirname $0)
+TEMPDIR=${BASEDIR}/temp
+CACHEDIR=${BASEDIR}/cache
+REPORTDIR=${BASEDIR}/reports
 
-for file in ${FILES};
-do
+rm $PROMPT_RM -rf $TEMPDIR   && mkdir $TEMPDIR
+rm $PROMPT_RM -rf $CACHEDIR  && mkdir $CACHEDIR
+rm $PROMPT_RM -rf $REPORTDIR && mkdir $REPORTDIR
 
-  # skip dirs, they will be put in the list of files when using 'dpkg -L'
-  [ -d $file ] && continue
+. $BASEDIR/etcdiff.include
 
-  if [ ! -e $file ];
-  then
-    echo "ERROR, file $file does not exist."
-    exit 1;
-  fi
-  echo "-> $file"
-
-  # Find out which installed package provides the config file
-  PACKAGE=$(dpkg-query -S "$file" | cut -d ':' -f 1 | uniq 2> /dev/null)
-
-  # Copy the whole file if it is not provided by default
-  if [ "x$PACKAGE" == "x" ];
-  then
-    DESTDIR=`dirname $file`
-    # strip trailing slash
-    [ ${DESTDIR:0:1} == '/' ] && DESTDIR=${DESTDIR:1}
-
-    # RECREATE the destination dir 
-    [ -d conf/$DESTDIR ] || mkdir -p conf/$DESTDIR
-    cp "$file" conf/$DESTDIR
-
-    continue
-  fi
-
-  # Get the package from the repository and diff
-
-  FILENAME=$(apt-cache show $PACKAGE | grep ^Filename | cut -d ' ' -f 2-)
-  ARCHIVE=$(basename $FILENAME)
-
-  if [ ! -f archives/$ARCHIVE ];
-  then
-    ( cd archives &&
-        wget -q -nc -c $DEBIANMIRROR/$FILENAME &&
-        mkdir ../temp/$PACKAGE
-        dpkg -x $ARCHIVE ../temp/$PACKAGE
-    )
-  fi
-
-  # Check for file existence before diffing, the file is not provided in
-  # package archive, but it could have been generated by package installation
-  # scripts
-  if [ ! -f temp/$PACKAGE/$file ];
-  then
-    echo "Warning: '$file' can't be found in package!"
-  fi
-
-
-  # TODO: diff only once and check filesize
-  TMPDIFF_FILE=`mktemp /tmp/etcdiff.XXXXXXXXXX`
-  diff -u temp/$PACKAGE/$file $file > $TMPDIFF_FILE
-  if [ `stat -c '%s' $TMPDIFF_FILE` -eq 0 ];
-  then
-    echo "$file: not changed"
-    rm -f $TMPDIFF_FILE
-  else
-    DESTDIR=`dirname $file`
-    # strip trailing slash
-    [ ${DESTDIR:0:1} == '/' ] && DESTDIR=${DESTDIR:1}
+# Examples of file query
 
-    # RECREATE the destination dir 
-    [ -d conf/$DESTDIR ] || mkdir -p conf/$DESTDIR
-    cp $TMPDIFF_FILE conf/$DESTDIR/$(basename $file).patch
+# by explicit file list
+#FILES="/etc/sysctl.conf /etc/updatedb.conf"
+#for file in $FILES;
+#do
+#  etcdiff_by_file $file
+#done
 
-    rm -f $TMPDIFF_FILE
-  fi
+# by file list generated by a command
+#FILES=$(find /etc/apache2 -type f -perm /o+r | grep -v '.dpkg-')
+#for file in $FILES;
+#do
+#  etcdiff_by_file $file
+#done
 
-done
+# by package name
+#etcdiff_by_package cherokee
+#etcdiff_by_package mlocate
+#etcdiff_by_package apache2-doc
+#etcdiff_by_package apache2.2-common
+#etcdiff_by_package libapache2-mod-php5
+#etcdiff_by_package hostapd
+
+#etchdiff_system