#! /bin/sh
#
# Copyright (C) 2012 Patrick "P. J." McDermott
#
# Permission is hereby granted, free of charge, to any person obtaining
# a copy of this software and associated documentation files (the
# "Software"), to deal in the Software without restriction, including
# without limitation the rights to use, copy, modify, merge, publish,
# distribute, sublicense, and/or sell copies of the Software, and to
# permit persons to whom the Software is furnished to do so, subject to
# the following conditions:
#
# The above copyright notice and this permission notice shall be included
# in all copies or substantial portions of the Software.
#
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
# EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
# MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
# IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
# CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
# TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
# SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

MIRROR='http://ftp.us.debian.org/debian'
DISTRIBUTION='sid'
COMPONENTS='main'
ARCHITECTURES='i386 armhf'

DIST_BASE="${MIRROR}/dists/${DISTRIBUTION}"

main()
{
	get_orig_indices
	write_new_indices
	generate_patches
	clean
}

get_orig_indices()
{
	for comp in ${COMPONENTS}; do
		for arch in ${ARCHITECTURES}; do
			url="${DIST_BASE}/${comp}/binary-${arch}/Packages.bz2"
			curl -s "${url}" | bzcat \
				>"Packages-${DISTRIBUTION}-${comp}-${arch}.orig"
		done
	done
}

write_new_indices()
{
	for comp in ${COMPONENTS}; do
		for arch in ${ARCHITECTURES}; do
			while IFS= read line; do
				if [ "${line#Source: }" != "${line}" ]; then
					src="${line#Source: }"
					printf '%s\n' "${line}"
					continue
				fi
				if [ "${line#Package: }" != "${line}" ]; then
					pkg="${line#Package: }"
					src="${pkg}"
					printf '%s\n' "${line}"
					continue
				fi
				if [ -f "$(find_deb "${src}" "${pkg}")" ]; then
					if [ "${line#Multi-Arch: }" != \
						"${line}" ]; then
						continue
					fi
					if [ "${line#Description: }" != \
						"${line}" ]; then
						printf '%s\n' "${line}"
						get_field 'Multi-Arch' \
							"${src}" "${pkg}"
						continue
					fi
				fi
				printf '%s\n' "${line}"
			done <"Packages-${DISTRIBUTION}-${comp}-${arch}.orig" \
				>"Packages-${DISTRIBUTION}-${comp}-${arch}"
		done
	done
}

generate_patches()
{
	for comp in ${COMPONENTS}; do
		for arch in ${ARCHITECTURES}; do
		diff -U 6 "Packages-${DISTRIBUTION}-${comp}-${arch}.orig" \
			"Packages-${DISTRIBUTION}-${comp}-${arch}" \
			>"Packages-${DISTRIBUTION}-${comp}-${arch}.patch"
		done
	done
}

clean()
{
	for comp in ${COMPONENTS}; do
		for arch in ${ARCHITECTURES}; do
			rm -f "Packages-${DISTRIBUTION}-${comp}-${arch}.orig" \
				"Packages-${DISTRIBUTION}-${comp}-${arch}"
		done
	done
}

find_deb()
{
	echo "../builds/${1}/${2}_"*.deb
}

get_field()
{
	_field="${1}"
	_src="${2}"
	_pkg="${3}"
	_deb="$(find_deb "${_src}" "${_pkg}")"

	_found=false
	dpkg-deb -I "${_deb}" | while IFS= read _line; do
		if [ "${_line# ${_field}: }" != "${_line}" ]; then
			_found=true
			printf '%s\n' "${_line# }"
			continue
		fi
		if ${_found}; then
			if [ "${_line#  }" != "${_line}" ]; then
				printf '%s\n' "${_line# }"
			else
				_found=false
			fi
			continue
		fi
	done
}

main
