#!/usr/bin/env zsh
#
# WebNomad, your slick and static website publisher
#
# Copyright (C) 2012-2015 Denis Roio <jaromil@dyne.org>
#
# This source  code is free  software; you can redistribute  it and/or
# modify it under the terms of  the GNU Public License as published by
# the Free  Software Foundation; either  version 3 of the  License, or
# (at your option) any later version.
#
# This source code is distributed in  the hope that it will be useful,
# but  WITHOUT ANY  WARRANTY;  without even  the  implied warranty  of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
# Please refer to the GNU Public License for more details.
#
# You should have received a copy of the GNU Public License along with
# this source code; if not, write to:
# Free Software Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.

# full path to webnomad's system
R="`pwd`"
SYS="$R/webnomad"
source $SYS/zuper/zuper
vars=(BROWSER TIME_FORMAT OUTPUT_FORMAT SYS)
vars=(views BROWSER_WINDOW_ID)
source $SYS/zuper/zuper.init

source $R/config.zsh

BROWSER=${1:-firefox}
TIME_FORMAT='%F %H:%M'
OUTPUT_FORMAT='%T Event: %e fired for file: %w. Refreshing.'

$SYS/render test
$BROWSER $R/test/index.html &

views="
$R/test
$R/views
$R/tmpl
$R/config.zsh
"
while print - $views | inotifywait --fromfile - -q -r \
    -e modify -e create -e move -e delete \
    --timefmt "${TIME_FORMAT}" --format "${OUTPUT_FORMAT}"; do
    $SYS/render test
    BROWSER_WINDOW_ID=$(xdotool search --onlyvisible --class $BROWSER)
    xdotool key --window $BROWSER_WINDOW_ID 'CTRL+r'
done
