#!/bin/bash

set -e

mydir=$(dirname "$0")
case "$mydir" in /*);; *) mydir=$(pwd)/"$mydir";; esac

. "$mydir"/include.sh

do_rebuild=""

pubtag="piper"

if [ t"$1" = t"-c" ]; then
    echo "(Building from clean)"
    do_rebuild=yes
    shift
fi

plugindirs="$@"
if [ -z "$plugindirs" ]; then
    plugindirs=$(cat METADATA/repos.txt |
		     grep -v vamp-plugin-sdk |
		     grep -v vamp-plugin-tester |
		     grep -v '^piper' |
		     awk '{ print $1; }')
else 
    for dir in $plugindirs ; do
	if [ ! -d "$dir" ]; then
	    echo "ERROR: Directory $dir not found"
	    usage
	fi
    done
fi

set -u

reportdir="REPORTS/piper-js"
packagedir="PACKAGES/piper-js"

mkdir -p "$reportdir" "$packagedir" || exit 1

built="/tmp/built.$$.txt"
testfailed="/tmp/testfailed.$$.txt"
envcheckfailed="/tmp/envcheckfailed.$$.txt"
notbuilt="/tmp/notbuilt.$$.txt"

trap 'rm -f "$built" "$envcheckfailed" "$testfailed" "$notbuilt"' 0
touch "$built" "$envcheckfailed" "$testfailed" "$notbuilt"

logfile_for() {
    local activity="$1"
    local dir="$2"
    echo "$reportdir/$dir.$activity.txt"
}

build_dir_for() {
    local dir="$1"
    echo "piper-vamp-js-builds/$dir"
}

build() {
    local dir="$1"
    local log=$(logfile_for build "$dir")
    local build_dir=$(build_dir_for "$dir")
    make -C "$build_dir" em 2>&1 | tee "$log"
    return ${PIPESTATUS[0]}
}

rebuild() {
    local dir="$1"
    local log=$(logfile_for build "$dir")
    local build_dir=$(build_dir_for "$dir")
    if make -C "$build_dir" distclean; then
	build "$dir"
    elif make -C "$build_dir" clean; then
	build "$dir"
    else
	echo "*** Failed to 'make clean' in $dir!" | tee "$log"
	return 1
    fi
}

build_or_rebuild() {
    local dir="$1"
    if [ -n "$do_rebuild" ]; then
	rebuild "$dir"
    else
	build "$dir"
    fi
}

package() {
    local dir="$1"
    local id=$(vcs_id "$dir")
    local pstub="$dir-$pubtag-$id"
    local pdir="$packagedir/$pstub"
    local build_dir=piper-vamp-js-builds/"$dir"
    mkdir -p "$pdir"
    ( cd "$dir" ; 
	cp -av \
	    [Rr][Ee][Aa][Dd][Mm][Ee]* \
	    [Cc][Oo][Pp][Yy][Ii][Nn][Gg]* \
	    [Ll][Ii][Cc][Ee][Nn][CcSs][Ee]* \
	    [Cc][Ii][Tt][Aa][Tt][Ii][Oo][Nn]* \
	    [Cc][Hh][Aa][Nn][Gg][Ee][Ll][Oo][Gg]* \
	    ../"$pdir"/ || true
    )
    cp "$build_dir"/*.js "$pdir/"
    ( cd "$packagedir";
      tar cvjf "$pstub".tar.bz2 "$pstub"
      rm -rf "$pstub"
    )
}

for dir in $plugindirs ; do
    dir=${dir%/*}
    build_dir=piper-vamp-js-builds/"$dir"
    echo
    echo "Processing: $dir"
    if [ ! -d "$dir" ]; then
	echo "Directory $dir not found!"
	echo "$dir" >> "$notbuilt"
    elif [ ! -d "$build_dir" ]; then
	echo "Directory $dir lacks Piper build dir $build_dir"
	echo "Skipping"
	echo "$dir" >> "$notbuilt"
    elif build_or_rebuild "$dir"; then
	echo "$dir" >> "$built"
    else
	echo "$dir" >> "$notbuilt"
    fi
    slog=$(logfile_for summary "$dir")
    echo "initialising logfile $slog for $dir"
    cat /dev/null > "$slog"
done
	 
cat "$built" | while read dir; do
    package "$dir"
done
   
echo
echo "** Built and packaged:"
cat "$built" | while read dir; do
    slog=$(logfile_for summary "$dir")
    echo "$dir"
    echo "$dir: OK: Success" >> "$slog"
done | sort

echo
echo "** Failed to build:"	    
cat "$notbuilt" | sort | while read dir; do
    slog=$(logfile_for summary "$dir")
    echo "$dir"
    echo "$dir: BUILD_FAILED: Failed to build" >> "$slog"
done

echo
