#! /bin/bash

# Functions
function download () {
	local url
	local file
	local checksum dl_checksum
	local tmpfile

	url="$1"
	file="$2"
	checksum="$3"
	tmpfile="${file}.tmp"

	if [ -s "${file}" ]; then
		return 0
	fi

	mkdir -p "$(dirname "${tmpfile}")" 2>/dev/null >/dev/null

	rm -f "${tmpfile}"
	wget -O "${tmpfile}" "${url}" || return 1

	if [ -n "${checksum}" ]; then
		dl_checksum="$(openssl sha1 "${tmpfile}" 2>/dev/null | sed 's@.*= *@@')"

		if [ "${checksum}" != "${dl_checksum}" ]; then
			echo "Checksum mismatch.  Got checksum: ${dl_checksum}; Expected checksum: ${checksum}" >&2

			return 1
		fi
	fi

	mv "${tmpfile}" "${file}"

	return 0
}

function fix_pkgconfig_file () {
	local file

	file="$1"

	sed 's@^prefix=.*@prefix=/usr@' "${file}" > "${file}.new"
	cat "${file}.new" > "${file}"
	rm -f "${file}.new"

	return 0
}

# Determine Linux and uClibc arch name
## alpha arm avr32 bfin cris e1 frv h8300 hppa i386 i960 ia64 m68k microblaze mips nios nios2 powerpc sh sh64 sparc v850 vax x86_64
arch="$(echo "${CCNAME}" | cut -f 1 -d '-')"
case "${arch}" in
	arm*)
		arch=arm
		;;
	mips*)
		arch=mips
		;;
	hppa*)
		arch=hppa
		;;
	sparc*)
		arch=sparc
		;;
	i?86)
		arch=i386
		;;
esac
