#!/bin/sh

set -e

from=$1
to=$2
output=$3
case "$from" in /*) ;; *) from=`pwd`/$from ;; esac
case "$to" in /*) ;; *) to=`pwd`/$to ;; esac
case "$output" in /*) ;; *) output=`pwd`/$output ;; esac

excludes="
	Xonotic/data/xonotic-*-data.pk3
	Xonotic/data/xonotic-*-maps.pk3
	Xonotic/data/xonotic-*-music.pk3
	Xonotic/data/xonotic-*-nexcompat.pk3
	Xonotic/data/xonotic-*-data-low.pk3
	Xonotic/data/xonotic-*-maps-low.pk3
	Xonotic/data/xonotic-*-music-low.pk3
	Xonotic/data/font-nimbussansl-*.pk3
	Xonotic/data/font-unifont-*.pk3
	Xonotic/data/font-xolonium-*.pk3
"

makepatchname()
{
	wildcard=$1
	fromname=$2
	toname=$3
	prefix=${wildcard%%\**}
	suffix=${wildcard#*\*}
	fromversion=${fromname#$prefix}
	fromversion=${fromversion%$suffix}
	toversion=${toname#$prefix}
	toversion=${toversion%$suffix}
	echo "$prefix$fromversion"patch"$toversion$suffix"
}

zipdiff -f "$from" -t "$to" -o "$output" -x "$excludes"
# or maybe just include ALL not excluded files from $to in $output?

tempdir=`mktemp -d -t zipdiff.XXXXXX`
cd "$tempdir"

for x in $excludes; do
	mkdir a b c
	(cd a && unzip "$from" "$x")
	fromname=`find a/ -type f`; fromname=${fromname#a/}
	(cd b && unzip "$to" "$x")
	toname=`find b/ -type f`; toname=${toname#b/}
	patchname=`makepatchname "$x" "$fromname" "$toname"`
	patchdir="c/$patchname"; patchdir=${patchdir%/*}
	mkdir -p "$patchdir"
	zipdiff -f a/"$fromname" -t b/"$toname" -o c/"$patchname"
	if [ -f c/"$patchname" ]; then
		(cd c && zip -0r "$output" "$patchname")
	fi
	rm -rf a b c
done

rm -rf "$tempdir"
