#!/bin/sh
# DocumentId:	$Id: dpsyco-skel 2318 2006-07-19 17:58:05Z ola $
# Author:	$Author: ola $
# Date:		$Date: 2006-07-19 19:58:05 +0200 (ons, 19 jul 2006) $
# Summary:
#	Handle the syncronization of the skels.
# Arguments:	$1	Source.
#		$2	Destination.
#    (optional)	$3	Rsync options.
#
#
# Copyright (C) 2001-2006 Ola Lundqvist <opal@debian.org>
#
# This program 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 of the License, 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., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
#

SOURCE="$1"
DESTR="$2"
RSYNCOPT="$3"

if [ ! -d "$SOURCE" ] ; then
    echo "Source $SOURCE is not a directory, exiting."
    exit 0
fi

if [ ! -d "$DESTR" ] ; then
    echo "Destination $DEST is not a directory, exiting."
    exit 0
fi
DESTAPP=$(echo "$DESTR" | sed -e "s|^/||;")
DEST=$(echo "$DESTR" | sed -e "s|/$||;")

SKELBDB="/var/lib/dpsyco-skel/skel"
SKELTBDFORMAT=
#SKELTBDFORMAT="%Y-%m-%d"

if [ -r /etc/dpsyco/defaults.conf ] ; then
    . /etc/dpsyco/defaults.conf
fi

CDIR=$(echo "$SKELBDB/$DESTAPP" | sed -e "s|/$||;")
mkdir -p $CDIR
OLDLISTF=$CDIR/skel.list
LISTF=$CDIR/tmpskel.list
BDIR=$CDIR/skel
TBDIR=$CDIR/back
if [ -n "$SKELTBDFORMAT" ] ; then
    TBDIR=$CDIR/back-$(date +"$SKELTBDFORMAT")
fi
touch $OLDLISTF

find $SOURCE -depth | sed -e "s|^$SOURCE||;" | sed -e "s|^/||;" | grep -v "^$" | sort > $LISTF

SEPREM="< "
SEPADD="> "
# Compare the two skeleton files and see what files that have been removed.
diff $OLDLISTF $LISTF | grep "$SEPREM" | sed -e "s|^$SEPREM||;" | {
    while read FILE ; do
	if [ -e "$DEST/$FILE" -a ! -d "$DEST/$FILE" -a ! -L "$DEST/$FILE" ] ; then
	    # Should it be restored or should it be deleted?
	    if [ -e "$BDIR/$FILE" ] ; then
		echo "Restore $DEST/$FILE from backup."
		mv "$BDIR/$FILE" "$DEST/$FILE"
	    else
		if [ -e "$DEST/$FILE" ] ; then
		    echo "Searching for $DEST/$FILE in the installed packages."
		    if dpkg -S "$DEST/$FILE" > /dev/null 2>&1 ; then
			echo "$DEST/$FILE found, not removed. Nothing to restore from though."
		    else
			echo "Remove $DEST/$FILE. Removed from dpsyco skel and nothing to restore."
			rm -f "$DEST/$FILE"
		    fi
		else
		    echo "File $DEST/$FILE removed from skel and but was already deleted."
		fi
	    fi
	elif [ -d "$DEST/$FILE" ] ; then
	    if rmdir "$DEST/$FILE" > /dev/null 2>&1 ; then
		echo "Directory $DEST/$FILE successfully removed."
	    fi
	fi
    done
}
# Compare the two skeleton files and see what directories that have been
# removed.
diff $OLDLISTF $LISTF | grep "$SEPREM" | sed -e "s|^$SEPREM||;" | {
    while read FILE ; do
	if [ -d "$DEST/$FILE" ] ; then
	    if rmdir "$DEST/$FILE" > /dev/null 2>&1 ; then
		echo "Directory $DEST/$FILE successfully removed."
	    fi
	fi
    done
}

# Sync in a new version.
mkdir -p $TBDIR
rsync -rltD -b --backup-dir=$TBDIR -I $RSYNCOPT "$SOURCE/" "$DESTR"

find $TBDIR -type f | sed -e "s|^$TBDIR||;" | {
    while read FILE ; do
	#echo diff -q "$TBDIR$FILE" "$SOURCE$FILE"
	if ! diff -q "$TBDIR$FILE" "$SOURCE$FILE" > /dev/null ; then
	    # If they differ back it up.
	    if [ ! -e "$TBDIR$FILE" ] ; then
		echo "Backing up $DEST$FILE."
	    else
		echo "Backing (destroy old backup) up $DEST$FILE (was modified)."
	    fi
	    TMPC=$(echo "$BDIR$FILE" | sed -e "s|/[^/]*$||;")
	    mkdir -p $TMPC
	    #echo "Copy $TBDIR$FILE to $BDIR$FILE"
	    cp -a "$TBDIR$FILE" "$BDIR$FILE"
	else
	    #echo "No need to back up $DEST$FILE (rm $TBDIR$FILE)."
	    rm "$TBDIR$FILE"
	fi
    done
}
if [ -z "$SKELTBDFORMAT" ] ; then
    #echo "Clean $TBDIR."
    rm -Rf $TBDIR
else
    #echo "rmdir $TBDIR recursively"
    find $TBDIR -type d -depth | xargs rmdir > /dev/null 2>&1
fi
# Store the new skel as the old one.
mv $LISTF $OLDLISTF
