#!/bin/bash

EXE=$(basename $0)
ORIG_PROG=$(which --skip-tilde $EXE)

mail-sync updateAll > ~/.mail-sync/update_log 2>&1 &
sh -c "watch -n 10 mail-sync updateAll >> ~/.mail-sync/update_log 2>&1" &
PID=$!
echo "Background sync started: $PID"

$ORIG_PROG "$@"

pkill -P $PID
kill $PID
(mail-sync syncOne `cat ~/.mail-sync/mua-current-folder.txt` > /dev/null 2>&1 &&
	mail-sync updateAll >>  ~/.mail-sync/update_log 2>&1 &&
mail-sync syncAllMailBoxes >> ~/.mail-sync/update_log 2>&1 &&
mail-sync updateAll >> ~/.mail-sync/update_log 2>&1
) &
