#!/bin/zsh -feu

# make a copy of the system configuration which I made:
# * all files in /etc/ which differ from debian defaults
#     including those orphaned.
# * all new files create atop of debian config.
#

usage()
{
    cat <<EOF
usage: ${0##*/} [-l] [+-h] <backup root>

-l  list the user-MODIFIED conffiles of debian.
copy to backup-root

(better to invoke as root -- needs read access)
EOF
}

list_only=n
verbose=n

while getopts :hlv OPT; do
    case $OPT in
        l)
            list_only=y
            ;;
        h|+h)
            usage
            exit 0
            ;;
        v)
            verbose=y
            ;;
        *)
            usage >&2
            exit 2
    esac
done
shift OPTIND-1
OPTIND=1

if [[ $list_only == n ]]
then
    if [[ $(id --user) != 0 ]]; then
        usage >&2
        exit 1
    fi

    if [[ $# -lt 1 ]]
    then
        usage >&2
        exit 1
    else
        backup_root=$1
        shift
    fi
fi


copy_file()
{
    # cp --archive
    rsync --relative $1 $backup_root
}

STAGE()
{
    cecho blue $@
}

### Find the modified ones:
STAGE "Find the modified Conffiles"

dpkg-query --showformat='${Conffiles}\n' --show |\
    grep -v -e '^$'|
    while read file hash; do
        if [[ ! -f $file ]]
        then
            cecho blue "missing $file"
        elif [[ $hash != $(md5sum $file| cut -f 1 -d ' ') ]]
        then
            ls -l $file
            cecho red "$hash\n"
            cecho yellow $(md5sum $file| cut -f 1 -d ' ')
            if [[ $list_only = n ]]; then
                copy_file $file
            fi
        else
            # cecho yellow "ok $file"
        fi
    done


### Find the backup files -- still a bit interesting.
STAGE "Find the backup files (dpkg)"
# ll -R system-configuration
#.dpkg-dist
#.dpkg-new
find /etc/ -name '*.dpkg-old' | \
    while read filename; do
        ls -l $filename
        if [[ $list_only = n ]]; then
            copy_file $filename
        fi
    done


if [[ $list_only = n && $verbose = y ]]; then
    STAGE "show the backup store"
    ls -lR $backup_root
    # tar -xapf system-configuration.tar.bz2 system-configuration/
fi

set -x
# fixme for root it does not exist
: ${TMPDIR:=/run/user/$(id --real)/}
echo "*** using $TMPDIR for temp files" >&2


set +x
# can I find /etc/ld.so.conf.d/zzz_pkgsrc.conf.no
STAGE "make a list of all DEBIAN files in /etc"
dlocate  --filename-only  -w '/etc/.*'|\
    while read file; do
        if [[ -f $file ]]; then echo $file;
        fi;
    done |\
        sort  > $TMPDIR/from-debs

sudo find /etc/ -type f -o -type l |sort    > $TMPDIR/from-fs

# removed from debians:
# -3 common
# -1

STAGE "List files in /etc/ not in DEBIAN packages"
cecho red "generated"

comm -3 -2 $TMPDIR/from-fs $TMPDIR/from-debs |\
    while read filename; do
        if [[ $list_only = n ]]; then
            # this skips symlinks, so I don't want empty directories either!
            copy_file $filename
        fi
    done

if [[ $list_only != y ]];
then
    # backup_root=$backup_root/generated/

    # delete empty directories?
    ( cd $backup_root && find -depth -type d -exec rmdir {} \; )
fi


# disappeared:
cecho red "disappeared:"
# maybe symlinks:
comm -3 -1 $TMPDIR/from-fs $TMPDIR/from-debs

cecho blue -e  "suggestion\n\tsudo tar --use-compress-program  bzip2 -cpf system-configuration.tar.bz2 system-configuration"


# sudo rsync var/tmp/pkgsrc  root@e7240:/mnt/big/
