#!/bin/bash
o=$1
u=$2
pkgname=${o%%-*}
newname=${o##$pkgname-}
new_version=${newname%.pkg.tar.gz.part}
url=${u%/*}
cached_file=""
# Only check for pkg.tar.gz files in the cache, we download db.tar.gz as well
if [[ "$o" =~ "pkg.tar.gz" ]] # if $o contains pkg.tar.gz
then
  for cached_file in $(ls -r /var/cache/pacman/pkg/${pkgname}-*.pkg.tar.gz); do
    # just take the first one, by name. I suppose we could take the latest by date...
    oldname=${cached_file##*/$pkgname-}
    old_version=${oldname%.pkg.tar.gz}
    if [ "$old_version" = "$new_version" ]; then
      # We already have the new version in the cache! Just continue the download.
      cached_file="" 
    fi
    break
  done
fi
if [ "$cached_file" != "" ]; then
  # Great, we have a cached file, now calculate a patch name from it
  oldname=${cached_file##*/$pkgname-}
  old_version=${oldname%.pkg.tar.gz}
  delta_name=$pkgname-${old_version}_to_${new_version}.delta
  # try to download the delta
  if wget --passive-ftp -c $url/$delta_name; then
    # Now apply the delta to the cached file to produce the new file
    if xdelta patch $delta_name $cached_file $o; then
      # Remove the delta now that we are finished with it
      rm $delta_name      
    else
      # Hmmm. xdelta failed for some reason      
      rm $delta_name
      # just download the file
      wget --passive-ftp -c -O $o $u
    fi
  else
    # just download the file
    wget --passive-ftp -c -O $o $u
  fi  
else
  # just download the file
  wget --passive-ftp -c -O $o $u  
fi
