#!/bin/sh

# diff_conffiles.sh
# 2016-06-01
# by Gernot Walzl

# "debsums -es" finds packages where the default config files were modified.
# This script creates a file ${PKG}.conffiles.diff containing a diff of changes
# in config files.

set -e

PKG="$1"
if [ "$PKG" = "" ]; then
  echo "Usage: $0 pkg"
  exit 1
fi
if ! dpkg -s "$PKG" | grep -q '^Status: install ok installed$' ; then
  echo "Error: $PKG is not installed."
  exit 1
fi

CWD=$(pwd)
TMP_DIR=$(mktemp -d -t diff_conffiles.$PKG.XXXXXXXX)
cd "$TMP_DIR" || exit 1
apt-get download "$PKG"
ar x $PKG_*.deb
tar xf control.tar.?z ./conffiles
echo -n '' > "${CWD}/${PKG}.conffiles.diff"
mkdir orig
cd orig || exit 1
cat ../conffiles \
  | while read CONFFILE; do
    tar xvf ../data.tar.?z ".${CONFFILE}"
    (
      cd ..
      diff -u "orig${CONFFILE}" "$CONFFILE" >> "${CWD}/${PKG}.conffiles.diff" || true
    )
  done
cd "$CWD"
rm -rf "$TMP_DIR"