#!/bin/bash

set -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-11 Andrea Mennucci.
# License: GNU Library General Public License, version 2 or later

# With a change by Torsten Spindler for use with Ubuntu

############################ WARNING ##############
#this script is not completely satisfactory 

# situation,
# baz_2_all.deb is in stable, but then
# baz_2+1_all.deb enters as a "point release" in stable and replaces baz_2_all.deb
# (this needs a delta for stable updates, but it will be managed in a different repo)

# then there is a security update, baz_2+2_all.deb

# now two deltas should be generated for security ,
# 2   -> 2+2
# 2+1 -> 2+2

# the first one is not generated by this simple script.
##########################################

#who I am
b=`basename $0`

DEBUG=''
VERBOSE=''
[ "$1" = '-v'  ] && { VERBOSE='-v' ; shift ; }
[ "$1" = '-d'  ] && { DEBUG='--debug' ; shift; }
[ "$1" = '-v'  ] && { VERBOSE='-v' ; shift ; }
if [ "$1" = ''  ] || test ! -r "$1" ; then 
  echo please provide the configuration file as argument
  exit 1
fi
. "$1"

if [ -n "$TMPDIR" ]; then
    export TMPDIR
fi

# set gpg-agent variables, test it
gpgagentcmd="gpg-agent --homedir \"${GNUPGHOME}\" --daemon --write-env-file \"$GNUPGAGENTINFO\" "
if test "$GNUPGAGENTINFO"  ; then 
 if test ! -r "$GNUPGAGENTINFO"  ; then 
  echo ERROR no agent info, please start the agent with
  echo $gpgagentcmd
  exit 1
 else
  . "$GNUPGAGENTINFO" 
  export GPG_AGENT_INFO
  if test ! "${GPG_AGENT_INFO}" -o  ! -e "${GPG_AGENT_INFO/:*/}" -o ! -O "${GPG_AGENT_INFO/:*/}" ; then
     echo ERROR agent info is not OK, please run the command
     echo $gpgagentcmd
     exit 1
  elif ! echo | gpg-connect-agent --homedir ${GNUPGHOME} ; then
     echo ERROR agent is not responding, please run the command
     echo $gpgagentcmd
     exit 1
  fi
 fi
fi


#test that we can sign, possibly loading the password in the agent
if test  "$GNUPGSEC" ; then
 t=`tempfile`
 echo pippo > $t
 if  ! gpg2 --quiet --batch --homedir "${GNUPGHOME}"  -o /dev/null --default-key $GNUPGSEC --sign $t  ;
 then
  echo signature test FAILED
  rm $t
  exit 1
 fi
 rm $t
fi


#make copy of current stable-security lists of packages
olddists=${TMPDIR:-/tmp}/oldsecdists-`date +'%F_%H-%M-%S'`
mkdir $olddists
cp -a $secdebmir/dists $olddists


#do mirror security
trap "rm  $VERBOSE -f $secdebmirlock ; echo FAILED , please delete $olddists" 0
#this version of 'debmirror' is patched to support the '--trash' option,
# see in /usr/share/debdelta
$DEBMIRROR $DEBUG $VERBOSE $secdebmir $DEBMIRROR_TRASH  \
 --method=$DEBMIRROR_METHOD --nosource -h $sechost \
 -r $release -d ${secstable}  --arch=$ARCHc \
 $DEBMIRROR_OPTIONS

#do create deltas
lockfile -r 1  /tmp/$b.lock || exit 1
trap "rm  $VERBOSE -f /tmp/$b.lock; echo FAILED , please delete $olddists " 0
cd $secdebmir
for arch in $ARCHs ; do
 for sec in $SECTIONS; do
   $debdeltas $VERBOSE -v --test $debdelta_opt  \
     --old $fulldebmir/dists/${origstable}/$sec/binary-$arch/Packages.gz \
     --old $olddists/dists/${secstable}/$sec/binary-$arch/Packages.gz \
     --alt $deltamir/old_debs  \
     --dir $deltamir//   dists/${secstable}/$sec/binary-$arch/Packages.gz
  done
done

#do clean up a bit
trap "" 0
rm  $VERBOSE -f /tmp/$b.lock
rm -r $olddists
find $deltamir/old_debs -type f -mtime +80 | xargs -r rm || true

find $deltamir/pool \
   \( -name '*debdelta-fails' -or -name '*debdelta-too-big' \
      -or -name  '*debdelta' \) -mtime +80 -type f |\
       xargs -r rm || true
