giuliomoro@516: [ -z "$BBB_USER" ] && BBB_USER="root"
giuliomoro@516: [ -z "$BBB_HOSTNAME" ] && BBB_HOSTNAME="192.168.7.2"
giuliomoro@516: [ -z "$BBB_ADDRESS" ] && BBB_ADDRESS="$BBB_USER@$BBB_HOSTNAME"
giuliomoro@445: [ -z "$BBB_BELA_HOME" ] && BBB_BELA_HOME="~/Bela/"
giuliomoro@445: [ -z "$BBB_SCREEN_NAME" ] && BBB_SCREEN_NAME="Bela"
giuliomoro@445: [ -z "$RUN_PROJECT" ] && RUN_PROJECT=1
giuliomoro@445: [ -z "$COMMAND_ARGS" ] && COMMAND_ARGS=
giuliomoro@445: [ -z "$RUN_IN_FOREGROUND" ] && RUN_IN_FOREGROUND=1
giuliomoro@445: [ -z "$RUN_WITHOUT_SCREEN" ] && RUN_WITHOUT_SCREEN=0
giuliomoro@445: [ -z "$BBB_PROJECT_HOME" ] && BBB_PROJECT_HOME="${BBB_BELA_HOME}/projects/"
giuliomoro@445: [ -z "$BBB_DEFAULT_PROJECT_NAME" ] && BBB_DEFAULT_PROJECT_NAME="scriptUploadedProject"
giuliomoro@445: [ -z "$BBB_PROJECT_NAME" ] && BBB_PROJECT_NAME=$BBB_DEFAULT_PROJECT_NAME
giuliomoro@445: 
giuliomoro@445: trap "echo; exit 0;" 2 9
giuliomoro@445: folder_has_changed(){
giuliomoro@447: 	[ -z "$2" ] && { echo "Error: folder_has_changed(folder, reference, [filter])"; return 1; }
giuliomoro@445: 	[ -z "$3" ] && FILTER="." || FILTER="$3"
giuliomoro@523: 	# Do not watch hidden files or folders 
giuliomoro@523: 	find "$1" -type f -not -path '*/\.*' -newer "$2" | grep "$FILTER"
giuliomoro@445: 	return $?
giuliomoro@445: }
giuliomoro@445: 
giuliomoro@445: wait_for_change(){
giuliomoro@447: 	[ -z "$2" ] && { echo "Error: folder_has_changed(folder, reference, [filter])"; return 1; }
giuliomoro@445: 	[ -z "$4" ] && SLEEP=0.5 || SLEEP="$4"
giuliomoro@445: 	while ! folder_has_changed "$1" "$2" "$3"
giuliomoro@445: 	do
giuliomoro@445: 		sleep $SLEEP;	
giuliomoro@445: 	done
giuliomoro@445: }
giuliomoro@445: 
giuliomoro@445: set_date(){
giuliomoro@445: # an optional parameter will be executed as part of the same ssh session
giuliomoro@518: 	ssh $BBB_ADDRESS "date -s \"`date '+%Y%m%d %T %z'`\" > /dev/null; $1"
giuliomoro@445: }
giuliomoro@515: 
giuliomoro@515: check_board_alive(){
giuliomoro@515: 	printf "Checking the board is up and running at $BBB_ADDRESS..."
giuliomoro@515: 	ssh -o ConnectTimeout=10 $BBB_ADDRESS exit && printf "done\n" || {
giuliomoro@515: 		printf "\nERROR: the board does not respond at $BBB_ADDRESS, check that the address is correct and the board is connected.\n";
giuliomoro@515: 		exit 1;
giuliomoro@515: 	}
giuliomoro@515: }
giuliomoro@523: 
giuliomoro@525: build_script_usage_brief(){
giuliomoro@525: 	printf "Usage: $THIS_SCRIPT [-c command-line-args] [-nbfF] [--watch] [--clean]%s <directory-with-source-files>" "$1"
giuliomoro@525: }
giuliomoro@525: 
giuliomoro@523: build_script_usage(){
giuliomoro@523: 	echo "	The program can be run in one of the following modes:
giuliomoro@523: 		-f arg : runs in the foreground (default).
giuliomoro@523: 		-b arg : runs in the background (no output is shown)
giuliomoro@523: 
giuliomoro@523: 	When running in the foreground you can stop the currently running program 
giuliomoro@523: 	with ctrl-C.
giuliomoro@523: 	When running in the background, you can use \`./stop_running.sh' to stop
giuliomoro@523: 	the current process and \`./connect_to_project.sh' to see the program's output.
giuliomoro@523: 
giuliomoro@523: 	Other options:
giuliomoro@525: 	--clean : cleans the pre-compiled object files on the board (same as \`-m projectclean').
giuliomoro@523: 	          If the linker issues warnings during a build, or you see that your latest changes are not
giuliomoro@523: 	          being applied to the running program, try to add the --clean flag for the next build and see 
giuliomoro@523: 	          if that fixes it. 
giuliomoro@523: 	
giuliomoro@523: 	--watch: the script will wait for changes in the source folder and when it detects them it
giuliomoro@523: 	         triggers a new build. If you do not run the program with -b, then you will have to
giuliomoro@523: 			 either kill the process or detach from the screen in order for the watchdog to be active.
giuliomoro@523: 	-n : the output will not be run after compiling.
giuliomoro@523: 	-c \"args\" : passes command-line arguments to the Bela program; e
giuliomoro@523: 	              close the argument string in quotes.
giuliomoro@523: 	-p arg : sets the name of the project to in the remote folder (default: $BBB_PROJECT_NAME ) 
giuliomoro@523: 	         do not use spaces in your project names, please
giuliomoro@523: 	-m \"args\" allows to pass arguments to the Makefile before the run target. For instance,
giuliomoro@523: 	           pass -m \`"projectclean"\` or \`-m "distclean"\` to clean project-specific
giuliomoro@523: 	           pre-built objects, or all the pre-built objects, respectively.
giuliomoro@523: 	"
giuliomoro@523: }