#! /bin/sh
# Transfer a file from the master to the secondary DNS server as a "new" file
# and then check the file to see if there are any differences between the old
# and the new. If there are then replace the old file with the new file,
# otherwise, just delete the new file.

REMOTE_USER="$1"
REMOTE_SERVER="$2"

LOCAL_USER="$3"
FILE="$4"

if [ $# -lt 4 ]; then
  echo "Usage: $0 REMOTE_USER REMOTE_SERVER LOCAL_USER FILE"
  exit 1
fi

su - "$LOCAL_USER" -c "scp $REMOTE_USER@$REMOTE_SERVER:/etc/dns/public/root/$FILE /home/$LOCAL_USER/JAIL/$FILE.new"

if diff /home/$LOCAL_USER/JAIL/$FILE /home/$LOCAL_USER/JAIL/$FILE.new > /dev/null 2>&1; then
  rm /home/$LOCAL_USER/JAIL/$FILE.new
else
  mv /home/$LOCAL_USER/JAIL/$FILE.new /home/$LOCAL_USER/JAIL/$FILE
fi
