#!/bin/sh
# pacman -S inotify-tools openssh rsync

set -eu

dir=$HOME/share/
user=$(whoami)

if [ ! -d "$dir" ]; then
  echo "First-time sync.."

  cd $HOME

  mkdir $dir
  rsync -avz $user@box:share $HOME

  ln -sf share/txt ~/txt
  ln -sf share/notes ~/notes
  ln -sf share/crypto ~/crypto
  ln -sf share/papers ~/papers
  ln -sf share/docs ~/docs

  echo "Synced."

  exit 0
fi

echo "Backup service started.." | systemd-cat -p info

while inotifywait -t 60 -qqr \
  -e close_write -e moved_to -e moved_from -e delete "$dir";
do
  rsync --del -avz $dir $user@box:share
done
