From a78ad0d38d03d25a32eaf78337e1a8d0d11ae60e Mon Sep 17 00:00:00 2001
From: Andrew Fyfe <andrew@neptune-one.net>
Date: Sun, 3 Jun 2007 19:14:14 +0100
Subject: contrib/wget-xdelta.sh: Replaced with an improved version.

Signed-off-by: Andrew Fyfe <andrew@neptune-one.net>
---
 contrib/wget-xdelta.sh | 111 +++++++++++++++++++++++++++++--------------------
 1 file changed, 67 insertions(+), 44 deletions(-)

(limited to 'contrib')

diff --git a/contrib/wget-xdelta.sh b/contrib/wget-xdelta.sh
index d99dd3cc..4656f4dd 100755
--- a/contrib/wget-xdelta.sh
+++ b/contrib/wget-xdelta.sh
@@ -1,47 +1,70 @@
 #!/bin/bash
-o=$(basename $1)
-u=$2
-CARCH="i686" # Hmmm where to get this from? /etc/makepkg.conf?
-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
-  pkgname=${o%-*-[0-9]-${CARCH}.pkg.tar.gz.part}   # Parse out the package name
-  newend=${o##$pkgname-}                  # Parse out everything following pkgname
-  new_version=${newend%-${CARCH}.pkg.tar.gz.part}  # Strip off .pkg.tar.gz.part leaving version
-  url=${u%/*}
-  for cached_file in $(ls -r /var/cache/pacman/pkg/${pkgname}-*-${CARCH}.pkg.tar.gz 2>/dev/null); do
-    # just take the first one, by name. I suppose we could take the latest by date...
-    oldend=${cached_file##*/$pkgname-}
-    old_version=${oldend%-${CARCH}.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
-  delta_name=$pkgname-${old_version}_to_${new_version}-${CARCH}.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
-    echo Applying delta...
-    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 
+
+if [ -r "/etc/makepkg.conf" ]; then
+	source /etc/makepkg.conf
 else
-  # just download the file
-  wget --passive-ftp -c -O $o $u
+	echo "wget-xdelta: Unable to find makepkg.conf"
+	exit 1
+fi
+
+if [ -r ~/.makepkg.conf ]; then
+	source ~/.makepkg.conf
+fi
+
+out_file=$(basename $1)
+file_url=$2
+
+if ! [[ "$out_file" =~ "pkg.tar.gz" ]]; then
+	# If it's not a package file download as normal and exit.
+	#wget --passive-ftp -c -O "$out_file" "$file_url"
+	exit $?
 fi
+
+
+# Get the package name and version
+[[ "$out_file" =~ "$CARCH" ]] && arch="-$CARCH" || arch=""
+pkg_data=$(echo $out_file | \
+	sed "s|^\(.*\)-\([[:alnum:]_\.]*-[[:alnum:]_\.]*\)${arch}${PKGEXT}.part|\1 \2|")
+pkgname=$(echo $pkg_data | cut -d ' ' -f 1)
+new_version=$(echo $pkg_data | cut -d ' ' -f 2)
+base_url=${file_url%/*}
+
+# Look for the last version
+for file in $(ls -r /var/cache/pacman/pkg/${pkgname}-*-*{,-$CARCH}$PKGEXT 2>/dev/null); do
+	[[ "$file" =~ "$CARCH" ]] && arch="-$CARCH" || arch=""
+	check_version=$(echo $file | \
+		sed "s|^.*/${pkgname}-\([[:alnum:]_\.]*-[[:alnum:]_\.]*\)${arch}$PKGEXT$|\1|" | \
+		grep -v "^/var/cache/pacman/pkg")
+
+	[ "$check_version" = "" ] && continue
+
+	vercmp=$(vercmp "$check_version" "$old_version")
+	if [ "$check_version" != "$new_version" -a $vercmp -gt 0 ]; then
+		old_version=$check_version
+		old_file=$file
+	fi
+done
+
+if [ "$old_version" != "" -a "$old_version" != "$new_version" ]; then
+	# Great, we have a cached file, now calculate a patch name from it
+	delta_name="$pkgname-${old_version}_to_${new_version}-${CARCH}.delta"
+
+	echo "wget-xdelta: Attempting to download delta $delta_name..." >&2
+	if wget --passive-ftp -c "$base_url/$delta_name"; then
+		echo "wget-xdelta: Applying delta..."
+		if xdelta patch "$delta_name" "$old_file" "$out_file"; then
+			echo "wget-xdelta: Delta applied successfully!"
+			rm "$delta_name"
+			exit 0
+		else
+			echo "wget-xdelta: Failed to apply delta!"
+			rm $delta_name
+		fi
+	fi
+ fi
+
+echo "wget-xdelta: Downloading new package..."
+wget --passive-ftp -c -O "$out_file" "$file_url"
+exit $?
+
+# vim:set ts=4 sw=4 noet:
-- 
cgit v1.2.3-70-g09d2