#!/bin/sh
# convert meson wrap files into makefiles for compatibility reasons
F="$1"
if [ -z "$F" ]; then
	echo "Usage: acr-wrap foo.wrap > foo.mk"
	exit 1
fi
(
echo "# This file is autogenerated by acr-wrap"
echo
NAME=""
while : ; do
	read LINE
	[ $? != 0 ] && break
	echo "$LINE" | grep -q =
	if [ $? = 0 ]; then
		if [ -z "${NAME}" ]; then
			echo "malformed"
			break
		fi
		echo ${LINE} |grep '^#' && continue
		LINE=`echo "$LINE"|sed -e 's, ,,g'`
		echo "WRAP_${NAME}_${LINE}" | sed -e 's/=/:=/g'
		eval "export WRAP_${LINE}"
	else
		echo "$LINE" | grep -q '^\['
		if [ $? = 0 ]; then
			NAME=$(echo "$LINE" |sed -e 's,-,_,g' -e 's,\[,,g' -e 's,\],,g')
		fi
	fi
done

WRAP_name=`echo ${WRAP_directory}|sed -e 's/-/-/g'`

cat <<EOF

${WRAP_name}_all: ${WRAP_directory}
	@echo "Nothing to do"

${WRAP_directory}:
EOF

GIT_FLAGS=""
if [ -n "${WRAP_depth}" ]; then
	GIT_FLAGS="--depth=${WRAP_depth}"
fi
echo "${NAME}" | grep -q -- '_git'
if [ $? != 0 ]; then
	echo "	echo Not supported && exit 1"
fi
if [ -n "$WRAP_revision" ]; then
	echo "	git clone --no-checkout ${GIT_FLAGS} ${WRAP_url} ${WRAP_directory}"
	echo "	cd ${WRAP_directory} && git fetch ${GIT_FLAGS} origin ${WRAP_revision}"
else
	echo "	git clone ${GIT_FLAGS} ${WRAP_url} ${WRAP_directory}"
fi
	echo "	cd ${WRAP_directory} && git checkout FETCH_HEAD"
if [ -n "$WRAP_patch_directory" ]; then
	echo "	cp -f packagefiles/${WRAP_patch_directory}/* ${WRAP_directory}"
fi

cat <<EOF

${WRAP_name}_clean:
	rm -rf ${WRAP_directory}
EOF

) < $F
