#! /bin/sh
#
# sync-bpo.sh: 2 pass mirror script for backports.org
# Copyright (C) 2005 Goswin von Brederlow <brederlo@informatik.uni-tuebingen.de>
#               2004 Mattias Wadenstein <maswan@debian.org>
#
# This program is free software; you can redistribute it and/or
# modify it under the terms of the GNU General Public License as
# published by the Free Software Foundation; either version 2 of the
# License, or (at your option) any later version.
#
# This program is distributed in the hope that it will be useful, but
# WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
# General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
#
# $Id: sync-debian-amd64.sh,v 1.00 2005/05/07 17:14:41 mrvn Exp $

# 2005-12-31: source config file based on script's name.  -- Peter Palfrader

set -e

# Read config
NAME="`basename $0`"
. $HOME/.$NAME.conf

# Provide default options for anything missing
######################################################################

# configuration, no spaces in paths please
# provide these in .sync-debian-amd64.conf
HOSTNAME=${HOSTNAME:-`hostname -f`}
RSYNC_HOST=${RSYNC_HOST:-"syncproxy.backports.org"}
RSYNC_PATH=${RSYNC_PATH:-":backports.org"}
RSYNC_USER=${RSYNC_USER:-""}
RSYNC_PASSWORD=${RSYNC_PASSWORD:-""}
TO=${TO:-"$HOME/$RSYNC_HOST"}
LOG=${LOG:-"$HOME/mirror.log"}
MAILTO=${MAILTO:-"root@localhost"}

# Programs and flags, should need no changes
SAVELOG=${SAVELOG:-"savelog -t"}
LOCKFILE=${LOCKFILE:-"lockfile"}
LOCK=${LOCK:-"Archive-Update-in-Progress-$HOSTNAME"}
TRACE=${TRACE:-"project/trace/$HOSTNAME"}

RSYNC=${RSYNC:-rsync}
RSYNC_OPTIONS=${RSYNC_OPTIONS:-"-rltvHB8192 --timeout 3600 --exclude $LOCK --exclude $TRACE"}
RSYNC_OPTIONS1=${RSYNC_OPTIONS1:-"--exclude Packages* --exclude Sources* --exclude Release*"}
RSYNC_OPTIONS2=${RSYNC_OPTIONS2:-"--delete --delete-after"}

# No user servicable parts below
######################################################################

# Get in the right directory, set the umask to be group writable and
# export RSYNC_PASSWORD
#
cd "$HOME"
umask 022
export RSYNC_PASSWORD

# Check to see if another sync is in progress
if $LOCKFILE >> "$LOG" 2>&1 -! -l 43200 -r 0 "$TO/$LOCK"; then
  echo "`date -u`: sorry, I already run"
  echo >> "$LOG" "### `date -u`: unable to start rsync, lock file exists"
  exit 1
fi
trap 'rm -f $TO/$LOCK; echo -e "Done at \c"; date -u; savelog "$LOG" > /dev/null' EXIT  

# Start log
exec >"$LOG" 2>&1
echo -e "Starting at \c"
date -u

# PASS 1
#
$RSYNC $RSYNC_OPTIONS $RSYNC_OPTIONS1 $EXCLUDE \
     $RSYNC_USER@$RSYNC_HOST:$RSYNC_PATH/. $TO/.

if [ `grep -c '^total size is' "$LOG"` -ne 1 ]; then
  ( echo "Eeek. first rsync to debian broke down... Check logs.."; \
    egrep '^write failed|@ERROR' "$LOG"; \
  ) | mail -s "Debian sync failed" "$MAILTO"
  echo "Eeek. first rsync to debian broke down..."
#  savelog "$LOG"
  exit 1
fi
echo -e "first sync without delete done at \c"
date -u

# PASS 2
#
$RSYNC $RSYNC_OPTIONS $RSYNC_OPTIONS2 $EXCLUDE \
     $RSYNC_USER@$RSYNC_HOST:$RSYNC_PATH/. $TO/.

if [ `grep -c '^total size is' "$LOG" ` -ne 2 ]; then
  ( echo "Eeek. second rsync to debian broke down... Check logs.."; \
    grep '@ERROR' "$LOG"; \
  ) | mail -s "Debian sync failed" "$MAILTO"
  echo "Eeek. second rsync to debian broke down..."
#  savelog "$LOG"
  exit 2
fi
echo -e "second sync with delete done at \c"
date -u

# Cleanup
rm -f "$TO/$LOCK"
date -u > "$TO/$TRACE"
