#!/usr/bin/env bash
###################################################################
#
#	File    : AutoRun
#	Author  : adb <alex@alexdball.dev>
#	Created : 23-01-02
#
###################################################################
#
clear

RED='\033[0;31m'
GREEN='\033[0;32m'
YELLOW='\033[1;33m'
BLUE='\033[0;34m'
NC='\033[0m'

USAGE="\n${RED}[x]${NC} Usage : AutoRun /directory/to/monitor 'command_to_run'\n\n"
NO_DIR="\n${RED}[x]${NC} Directory ${GREEN}$1${NC} does not exist! Aborting...\n\n"
NOTIFY_BIN="$(which inotifywait)"

if [ -z "$1" ] || [ -z "$2" ]
then
	printf "$USAGE\n"
	exit 0
fi


if [ -z "$NOTIFY_BIN" ]; then
    echo "[x] Cannot proceed inotify-tools is installed"
    exit 1
fi



MONITOR_DIR=$1
COMMAND_TO_RUN=$2

if [ ! -d "$1" ]
then
	printf "$NO_DIR"
	exit 0
fi

while :; do

    printf "\n${BLUE}[*]${NC} Monitoring Directory $1 for changes..\n"

    inotifywait -r -qq -e modify $1

    printf  "\n${YELLOW}[!]${NC} Change Detected.. Running ${GREEN}$2${NC}\n"

    sleep 0.3
    clear
    $2
    printf "\n${YELLOW}[-]${NC} Process exited...\n"

done

