Surf Monitor

1. Purpose

This script opens the surf web browser associated with a .html file. After opening it will monitor for changes in the received path files and update the current page when any change is detected.

2. Example Usage

surf_monitor :file.html: :path_to_monitor:

3. Dependencies

You should have surf installed - download. And also the inotifywait from the inotify-tools.

4. General Code Layout

First we will check the html file argument, then start the surf browser the first time, after that we start monitoring the received path files so we can update surf when any change occurs.

{surf_monitor.sh 4}
#!/bin/sh

{Validate Arguments, 5}
{Launch Surf, 5}

{Monitor File Changes, 5}
	{Update Surf, 5}
done

exit 0

5. Specific implementation

First we will start the surf browser associated with the received file. The process is put in background so we can continue to the next commands.

{Launch Surf 5}
surf $1 &

Used in section 4

We need to make sure that we are really receiving some html file or the browser will not display anything, and we need to tell which files we want to monitor. So we check if at least two arguments were received, and if it wasn't we finish the program by printing the expected usage.

{Validate Arguments 5}
if [ $# -lt 2 ]; then
	echo "Usage: $0 :file.html: :path_to_monitor:"
	exit 1
fi

Used in section 4

We know we received some file and the surf browser is running, so now we will start monitoring for changes in the path to monitor. Since the path already needs to exist we can monitor only the modification event.

{Monitor File Changes 5}
while inotifywait -e modify $2; do

Used in section 4

After the event is detected we want to update the currenly displayed web page. Surf allows us to tell it to refresh the open webpage using the SIGHUP signal. To be able to send that signal to the surf browser we need to have it's pid.

{Launch Surf 5} +=
SURF_PID=$!

Used in section 4

And then we can send the signal to the surf proccess.

{Update Surf 5}
kill -HUP $SURF_PID

Used in section 4