#!/bin/sh -e

FROM=rstync://hanzubon.jp/debian/
TO=/mirrors/Debian/debian/
LOGFILE=rsync.log

#-----------------------------------------------------------------------------#

HOSTNAME=`hostname -f`

# Get in the right directory and set the umask to be group writable
cd $HOME/log
umask 002

LOCK="${TO}/Archive-Update-in-Progress-${HOSTNAME}"

# Check to see if another sync is in progress
if [ -f "$LOCK" ]; then
  if [ "`find $LOCK -maxdepth 1 -amin -360`" = "" ]; then
    echo "stale lock found (not accessed in the last 6 hours), forcing update!"
    rm -f $LOCK
  else
    echo "current lock file exists, unable to start rsync!"
    exit 1
  fi
fi

touch $LOCK

rsync --recursive --times --links --hard-links \
      --exclude "Packages*" --exclude "Sources*" \
      --exclude "Release*" --exclude "ls-lR*" \
      --verbose --stats \
      $FROM $TO > $LOGFILE 2>&1

rsync --recursive --times --links --hard-links --max-delete=400000 \
      --delete --delete-after \
      --exclude "Archive-Update-in-Progress-${HOSTNAME}" \
      --exclude "project/trace/${HOSTNAME}" \
      --verbose --stats \
      $FROM $TO > $LOGFILE.2nd 2>&1
date -u > "${TO}/project/trace/`hostname -f`"

cat $LOGFILE.2nd >> $LOGFILE
rm -f $LOGFILE.2nd

rm -f $LOCK

#./push
/home/ftpadm/bin/signal.sh rsync.example.jp ftp-mirror >> $LOGFILE || true

savelog $LOGFILE > /dev/null 2>&1

