#!/bin/bash

# Lock file path
lockfile=/tmp/sync-txoo-local.lock

# Open the lock file for writing, and get file descriptor 200 for it.
exec 200>"$lockfile"
# Try to acquire an exclusive lock on the file descriptor 200.
if ! flock -n 200 ; then
    echo "Script is already running. Exiting."
    exit 1
fi

# Sync with a local web server and render an index page
args="$@"
for net in bitcoin testnet; do
  if [ -e ~/.txoo/$net/public ]; then
    changes=$(rsync -ai ~/.txoo/$net/public/ /var/www/html/txoo/$net/)
    if [ ! -z "$changes" ]; then
      /usr/local/bin/render-txoo /var/www/html/txoo/$net/
    fi
  fi
done

# The lock is automatically released when the script ends.
