#!/usr/bin/bash
set -euo pipefail

SCRIPT_NAME=$(basename "$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="$(dirname "$(readlink -f "$0")")/../src/compiletools/README.${SCRIPT_NAME}.rst"
fi

show_help() {
    echo "Usage: ${SCRIPT_NAME} SOURCE [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

startsource=$1
shift
source=$(ct-filelist --filter=source --style flat "$startsource"|tr '\n' ' ')
ct-create-makefile --dynamic "$startsource" ${source} "$@"

make -j$(ct-jobs)

# These are the headers that have to be included
#headers=$(ct-headertree --style Flat "$1" | tr '\n' ' ')
