#!/bin/sh
# Upstream thinks it's a good idea to create a whole new github repo plus an
# npm package for every generic-but-solved-in-a-specific-way sub-component of
# their project.
#
# They are wrong, group them back together here. We don't have time to package
# 6 Debian packages (that will never be used by anyone else) just for node-ws.

TMPDIR=debian/get-orig-source.tmp
VERFILE=dependency.versions

set -e

if [ -n "$1" ]; then
	version="$2"
	filename="$3"
	test -d "$TMPDIR" || exit 147
	if [ "$(basename "$filename")" = "ws-$version.tar.gz" ]; then
		echo "$version" >> "$TMPDIR/version"
	else
		echo "$filename=$version" | sed -e "s,$TMPDIR/ws-\(.*\)-$version.tar.gz,\1,g" >> "$TMPDIR/$VERFILE"
	fi
	tar -C "$TMPDIR" -xf "$filename"
	exit
fi

rm -rf "$TMPDIR"
mkdir -p "$TMPDIR"

cur_version="$(dpkg-parsechangelog -n1 -S Version | sed -e 's,\(.*\)-.*,\1,g' | sed -re 's,([^+]+)(\+ds([0-9]+)\.(.*))?,\1-\3-\4,g')"
cur_upstream="$(echo "$cur_version" | cut -d- -f1)"
cur_depiter="$(echo "$cur_version" | cut -d- -f2)"
cur_depiter="${cur_depiter:-0}"
cur_dephash="$(echo "$cur_version" | cut -d- -f3)"
cur_dephash="${cur_dephash:-0}"

git_clone_head() {
	local repo="$1"
	local dep="$2"
	local oldpwd="$(pwd -P)"
	cd "$TMPDIR"
	git clone "https://github.com/$repo/$dep"
	cd "$dep"
	git tag 0 "$(git rev-list HEAD | tail -n 1)"
	local version="$(git describe --tags | sed -e 's,-,.,g')"
	cd ..
	mv "$dep" "$dep-$version"
	cd "$oldpwd"
	echo "$dep=$version" >> "$TMPDIR/$VERFILE"
}

get_dep() {
	local repo="$1"
	local dep="$2"
	uscan --destdir="$TMPDIR" --no-symlink --upstream-version 0 --download --package "ws-$dep" --watchfile "debian/watch.$dep" || git_clone_head "$1" "$2"
}

get_dep websockets bufferutil
get_dep websockets utf-8-validate
get_dep unshiftio ultron
get_dep einaros options.js
get_dep websockets wscat

new_dephash="$(sha256sum "$TMPDIR/$VERFILE" | cut -b1-8)"

if [ "$new_dephash" = "$cur_dephash" ]; then
	if ! uscan --destdir "$TMPDIR" --no-symlink --upstream-version "${cur_upstream}"; then
		rm -rf "$TMPDIR"
		echo >&2 "no newer version, exiting"
		exit 1
	fi
else
	uscan --destdir "$TMPDIR" --no-symlink --upstream-version "${cur_upstream}~~"
fi

origpwd="$(pwd -P)"
cd "$TMPDIR"
new_upstream="$(cat version)"
test "$cur_upstream" = "$new_upstream" || cur_depiter=0
cd "ws-$new_upstream"
mkdir -p bin
mkdir -p src
mkdir -p node_modules/options
mkdir -p node_modules/ultron
cp ../bufferutil-*/src/*.cc src
cp ../utf-8-validate-*/src/*.cc src
cp ../wscat-*/bin/* bin
cp ../ultron-*/index.js node_modules/ultron/index.js
cp ../options.js-*/lib/options.js node_modules/options/index.js
python - ../bufferutil-*/binding.gyp ../utf-8-validate-*/binding.gyp >binding.gyp <<EOF
import ast, itertools, pprint, sys
o = [ast.literal_eval(open(i).read()) for i in sys.argv[1:]]
pprint.pprint({"targets": list(itertools.chain(*(x["targets"] for x in o)))})
EOF
cp ../"$VERFILE" .
cd ..

new_depiter=$((cur_depiter + 1))
new_version="${new_upstream}+ds${new_depiter}.${new_dephash}"
mv "ws-${new_upstream}" "node-ws_${new_version}"
tar -cJf "node-ws_${new_version}.orig.tar.xz" "node-ws_${new_version}"

cd "$origpwd"
mv "$TMPDIR/node-ws_${new_version}.orig.tar.xz" ..
rm -rf "$TMPDIR"
