#!/bin/sh -e

# A simple script that will create a repository of deltas, that may be
# used by debdelta-upgrade for upgrading package in stable and
# stable-security

# Copyright (C) 2006-09 Andrea Mennucci.
# License: GNU Library General Public License, version 2 or later

#who I am
b=`basename $0`

TMPDIR=/mirror/tmp
export TMPDIR

DEBUG=''
VERBOSE=''
[ "$1" = '-v'  ] && { VERBOSE='-v' ; shift ; }
[ "$1" = '-d'  ] && { DEBUG='--debug' ; }

#where to download the full mirror of debian stable security
secdebmir=/mirror/debian-security
#the lock used by debmirror
secdebmirlock=$secdebmir/Archive-Update-in-Progress-`hostname  -f`

#where is the full mirror of debian stable
fulldebmir=/mirror/debian
#the name by which "stable" is known in that mirror
stable="lenny"

#where to create the repository of deltas
deltamir=/mirror/debian-security-deltas

#where is the debdeltas program
debdeltas=/usr/bin/debdeltas
#options to your taste
debdelta_opt=" -n 3 --delta-algo xdelta3 --sign my_favorite_key"

#do mirror security
trap "rm  $VERBOSE -f $secdebmirlock" 0
#this version of 'debmirror' is patched to support the '--trash' option,
#  --trash option, see in /usr/share/debdelta
~/bin/debmirror.mine $DEBUG $VERBOSE $secdebmir --trash $deltamir/old_debs  \
 --nosource -h security.debian.org \
 -r debian-security -d stable/updates  --arch=i386,amd64

#do create deltas
lockfile -r 1  /tmp/$b.lock || exit 1
trap "rm  $VERBOSE -f /tmp/$b.lock" 0
cd $secdebmir
for p in `find dists -name Packages.gz` ; do
 q=`echo $p | sed "s:/stable/updates/:/$stable/:"`
 $debdeltas $VERBOSE -vd $debdelta_opt  \
  --alt $fulldebmir/$q   --alt $deltamir/old_debs  \
  --dir $deltamir//   $p
done

#do clean up a bit
find $deltamir/old_debs -type f -mtime +80 | xargs -r rm
