#!/bin/bash

# Creates a Makefile that runs "port livecheck" for each port I maintain,
# then runs "make" with multiple simultaneous jobs, then deletes the Makefile.
# The results don't always display in the same order each time, but they do
# come in much faster.

HANDLE=ryandesign
JOBS="$(sysctl -n hw.ncpu)"

PORTS=$(port echo '(' \
'maintainer:(\s|^)'$HANDLE'(\s|$)' or \
'maintainer:(\s|^)'$HANDLE'@macports.org(\s|$)' or \
'maintainer:(\s|^)macports.org:'$HANDLE'(\s|$)' ')' \
| sed -E 's/ +//g' \
| tr '\n' ' ')

MAKEFILE="$(mktemp "${TMPDIR:=/tmp}/portmylivecheck.XXXXXXXXXX")"
MAKEFILE_FD=3

eval exec "$MAKEFILE_FD>$MAKEFILE"

echo "all: $PORTS" >&$MAKEFILE_FD
echo >&$MAKEFILE_FD
echo ".PHONY: $PORTS" >&$MAKEFILE_FD

for PORT in $PORTS; do
    echo >&$MAKEFILE_FD
    echo "$PORT:" >&$MAKEFILE_FD
    echo $'\t'"port livecheck $PORT" >&$MAKEFILE_FD
done

eval exec "$MAKEFILE_FD>&-"

make -f "$MAKEFILE" -j "$JOBS" -s || exit $?

rm -f "$MAKEFILE"
