#!/bin/bash
NORESTART=false
POSITIONAL=()
PARAMS=()
thisfile=$(basename $0)

command -v fswatch &> /dev/null || { echo "The fswatch command is not available.  You need to install fswatch."; exit 1; }

while [[ $# -gt 0 ]]; do
    key="$1"
    case $key in
	--playground|--noconfig|-h|--help|--add|--noconfig)
	    PARAMS+=("$1")
	    shift
	    ;;
	--apikey|--apiurl|--server|--project)
	    PARAMS+=("$1")
	    shift
	    PARAMS+=("$1")
	    shift
	    ;;
	--norestart)
	    NORESTART=true
	    shift
	    ;;
	*)
	    POSITIONAL+=("$1")
	    shift
	;;
    esac
done

if [ ${#POSITIONAL[@]} -eq 0 ]; then
    echo -e "usage: ${thisfile} [--apiurl APIURL] [--apikey APIKEY] [--server SERVER]\n                      [--playground] [--project PROJECT]\n                      [--add] [--noconfig] [--norestart]\n                      [directory]"
    exit 1;
fi

echo "Watching ${POSITIONAL[0]}"

while true; do
    changedfiles=$(fswatch -1 -r -E --exclude '.*\#.*' --exclude '.*~' --exclude '.*/\.git$' --exclude '.*/\.git/.*' --exclude '.*__pycache__.*' --event Updated --event Created ${POSITIONAL[0]} | uniq)
    echo -e "${changedfiles}"
    sleep 0.2
    CURRENTPARAMS=("${PARAMS[@]}")
    if [ "$NORESTART" = true ]; then
	CURRENTPARAMS+=("--norestart")
    else
	if ! echo "${changedfiles}" | grep -q '\.py'; then
	    CURRENTPARAMS+=("--norestart")
	fi
    fi
    dainstall "${CURRENTPARAMS[@]}" "${POSITIONAL[@]}"
    # Depending on your system, you may need to comment out the above line and uncomment the following line.
    # python -m docassemble.cli.dainstall "${CURRENTPARAMS[@]}" "${POSITIONAL[@]}"
done
