#! /bin/bash

if [[ ! -f $HOME/etc/debian-cd-mirror.conf ]]; then
	echo "No $HOME/etc/debian-cd-mirror.conf !"
	exit 4
fi

. $HOME/etc/debian-cd-mirror.conf

if [[ ! -f ${jigdoConf}.in ]]; then
	echo "No ${jigdoConf}.in !"
	exit 5
fi

export RSYNC_PASSWORD
PATH=${HOME}/bin:/usr/gnu/bin:/usr/local/bin:/usr/bin:/bin
export PATH

HOST=`hostname -f`
LOCK="${TO}/Archive-Update-in-Progress-${HOST}"

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

# Check to see if another sync is in progress
if lockfile -! -l 43200 -r 0 "$LOCK"; then
  echo ftp is unable to start rsync, lock file exists
  exit 1
fi
trap 'rm -f $LOCK > /dev/null 2>&1; echo -n "Done at " >> debian-cd-mirror.log; date >> debian-cd-mirror.log; savelog debian-cd-mirror.log > /dev/null' EXIT  

let NRSYNCS=0


# Deleting the images before the run if DELETEFIRST is set.

if [ $DELETEFIRST ]; then
	echo "Removing images not on the other host:" >> debian-cd-mirror.log
	let NRSYNCS++
	rsync -av --delete --ignore-existing --existing --exclude "$EXCLUDE" \
	$RSYNC_USER@$RSYNC_HOST::${MODULE}/images/ ${TO}/images/ >>debian-cd-mirror.log 2>&1
	if [ `grep -c '^total size is' debian-cd-mirror.log ` -ne $NRSYNCS ]; then
		( echo  "Eeek. Debian jigdo rsync broke down... Check logs.."; \
		  egrep '^write failed|@ERROR' debian-cd-mirror.log ) | mail -s "Debian-cd sync failed" $ERRORMAIL
		echo  "Eeek. Debian jigdo rsync broke down..." >> debian-cd-mirror.log
		exit 1
	fi
fi

let NRSYNCS++
rsync -avH --delete --delete-after \
     --timeout 7200 \
     --exclude "Archive-Update-in-Progress-${HOST}" \
     --exclude "project/trace/${HOST}" \
     --exclude "project/trace/cdimage.debian.org" \
     --exclude "$EXCLUDE" \
     $RSYNC_USER@$RSYNC_HOST::${MODULE}/jigdo/ ${TO}/jigdo/>> debian-cd-mirror.log 2>&1

if [ `grep -c '^total size is' debian-cd-mirror.log ` -ne $NRSYNCS ]; then
	( echo  "Eeek. Debian jigdo rsync broke down... Check logs.."; \
	  egrep '^write failed|@ERROR' debian-cd-mirror.log ) | mail -s "Debian-cd sync failed" $ERRORMAIL
	echo  "Eeek. Debian jigdo rsync broke down..." >> debian-cd-mirror.log
	exit 1
fi
echo -n "jigdo rsync done, now generating images. at " >> debian-cd-mirror.log
date >> debian-cd-mirror.log

typeset currentVersion=`ls -l ${TO}/jigdo/current`
currentVersion="${currentVersion##* -> }"

versionDir="${TO}/jigdo/${currentVersion}"

for a in ${versionDir}/*; do
	if [[ ! -d $a ]]; then
		continue
	fi
	b=`basename $a`
	typeset jigdoDir=${TO}/jigdo/${currentVersion}/${b}
	typeset imageDir=${TO}/images/${currentVersion}/${b}
	if [[ ! -d $imageDir ]]; then
		echo "Creating $imageDir" >> debian-cd-mirror.log
		mkdir -p $imageDir
	fi
	cp -f ${jigdoConf}.in $jigdoConf
      	echo "jigdoDir=$jigdoDir" >> $jigdoConf
        echo "imageDir=$imageDir" >> $jigdoConf
	echo "logfile=${JIGDOLOGBASE}.${b}.log" >> $jigdoConf
	echo -n "jigdoing the files in ${b}, starting at ">>debian-cd-mirror.log
	date  >> debian-cd-mirror.log
	jigdo-mirror $jigdoConf >>debian-cd-mirror.log
	savelog ${JIGDOLOGBASE}.${b}.log
done

echo -n "jigdo generation done, now doing final rsync. at " >> debian-cd-mirror.log
date >> debian-cd-mirror.log

let NRSYNCS++
rsync -avH --delete --delete-after \
     --timeout 7200 --size-only\
     --exclude "Archive-Update-in-Progress-${HOST}" \
     --exclude "project/trace/${HOST}" \
     --exclude "$EXCLUDE" \
     $RSYNC_USER@$RSYNC_HOST::$MODULE/. $TO/. >> debian-cd-mirror.log 2>&1

if [ `grep -c '^total size is' debian-cd-mirror.log ` -ne $NRSYNCS ]; then
	( echo "Eeek. Debian cdimage rsync broke down... Check logs.."; \
	  egrep '^write failed|@ERROR' debian-cd-mirror.log ) | mail -s "Debian-cd sync failed" $ERRORMAIL
	echo "Eeek. Debian cdimage rsync broke down..." >> debian-cd-mirror.log
	exit 2
fi
echo -n "second sync with delete done at " >> debian-cd-mirror.log
date >> debian-cd-mirror.log


rm -f $LOCK > /dev/null 2>&1

date -u > "${TO}/project/trace/${HOST}"
