#!/bin/bash

# Check if the correct number of arguments is passed
if [ "$#" -ne 3 ]; then
    echo "Usage: $0 <file_to_watch> <process_name> <command_to_run>"
    exit 1
fi

# Assign arguments to variables
FILE_TO_WATCH=$1
PROCESS_NAME=$2
COMMAND_TO_RUN=$3

eval "$COMMAND_TO_RUN" &


# Watch the file for changes
while inotifywait -e modify "$FILE_TO_WATCH"; do
    # Kill the process
    sudo killall "$PROCESS_NAME"
    
    # Run the provided command (like build and run)
    eval "$COMMAND_TO_RUN" &
done
