#!/bin/sh

# parse args
BUILD_LOG=$1

# error handling
set -e
set -o pipefail

# write the header
echo "##"
echo "## (!!!) This is an auto-generated file."
echo "## (!!!) Please do not modify manually."
echo "## (!!!) Instead please follow the instructions in Makefile on how to update the port."
echo "##"
echo ""

# write MASTER_SITES_DEPS
echo "MASTER_SITES_DEPS= \\"
cat ${BUILD_LOG} | \
	grep -a "^; Fetching #<URL " | \
	sed -E 's|^; Fetching #<URL "|| ; s|">$$||' | \
	sort | \
	sed -E 's|^(http://beta.quicklisp.org/.*/)([^/]+)$$|\1:\2|' | \
	sed -E 's|^(.+):([^:-\+\.]*)[-\+\.]([^:]*)$|\1:\2_\3|' | \
	sed -E 's|^(.+):([^:-\+\.]*)[-\+\.]([^:]*)$|\1:\2_\3|' | \
	sed -E 's|^(.+):([^:-\+\.]*)[-\+\.]([^:]*)$|\1:\2_\3|' | \
	sed -E 's|^(.+):([^:-\+\.]*)[-\+\.]([^:]*)$|\1:\2_\3|' | \
	sed -E 's|^(.+):([^:-\+\.]*)[-\+\.]([^:]*)$|\1:\2_\3|' | \
	sed -E 's|^(.+):([^:-\+\.]*)[-\+\.]([^:]*)$|\1:\2_\3|' | \
	sed -E 's|.*|& \\|'
echo ""

# write DISTFILES_DEPS
echo "DISTFILES_DEPS= \\"
cat ${BUILD_LOG} | \
	grep -a "^; Fetching #<URL " | \
	sed -E 's|^; Fetching #<URL "|| ; s|">$$||' | \
	sort | \
	sed -E 's|^(http://beta.quicklisp.org/.*/)([^/]+)$$|\2:\2|' | \
	sed -E 's|^(.+):([^:-\+\.]*)[-\+\.]([^:]*)$|\1:\2_\3|' | \
	sed -E 's|^(.+):([^:-\+\.]*)[-\+\.]([^:]*)$|\1:\2_\3|' | \
	sed -E 's|^(.+):([^:-\+\.]*)[-\+\.]([^:]*)$|\1:\2_\3|' | \
	sed -E 's|^(.+):([^:-\+\.]*)[-\+\.]([^:]*)$|\1:\2_\3|' | \
	sed -E 's|^(.+):([^:-\+\.]*)[-\+\.]([^:]*)$|\1:\2_\3|' | \
	sed -E 's|^(.+):([^:-\+\.]*)[-\+\.]([^:]*)$|\1:\2_\3|' | \
	sed -E 's|.*|& \\|'
echo ""
