#!/bin/sh

# Every time a file relevant to this project changes, rebuild.
# Requires: inotify-tools
# Example Usage: ct-watch-build --variant=release

SCRIPT_NAME=$(basename "$0")
SCRIPT_DIR=$(dirname "$(readlink -f "$0" 2>/dev/null || echo "$0")")

# Find README: check installed location first, then development location
if [ -f "/usr/share/doc/compiletools/README.${SCRIPT_NAME}.rst" ]; then
    README_PATH="/usr/share/doc/compiletools/README.${SCRIPT_NAME}.rst"
else
    README_PATH="${SCRIPT_DIR}/../src/compiletools/README.${SCRIPT_NAME}.rst"
fi

show_help() {
    echo "Usage: ${SCRIPT_NAME} [OPTIONS]"
    echo ""
    if [ -f "$README_PATH" ]; then
        cat "$README_PATH"
    else
        echo "No documentation available for ${SCRIPT_NAME}"
        echo "See: https://github.com/drgeoffathome/compiletools"
    fi
    exit 0
}

for arg in "$@"; do
    case "$arg" in
        --help|-h) show_help ;;
    esac
done

while true; do
    inotifywait -e modify,close_write,move,delete $(ct-cake --file-list)
    ct-cake "$@"
done
