#!/bin/sh
## syncing sputnik zu energija

#echo "Irgendwie ist hier was schief gelaufen!!!"
#echo "Lösung: bilder/pcd/island und debian-maintain/packages/pcd2html per Hand!!!!"
#exit
. ~/.rsync/DIRS2SYNC

ONEWAYFLAG=0
MAILFLAG=0
XPLANET=/var/lib/xplanet

## OTHERHOST: Anpingen des von OTHERHOST zum Test, ob alles OK ist
## THISHOST:  in ~/.hosts/$THISHOST wird z.B. bin gesichert

if [ `basename $0` = "s2e" ] ; then
  TARGET=${ECH}@energija:/home/${ECH}
  TARGETUSER=${ECH}
  SOURCE=${MYHOME}
#  ZTARGET=${ECH}@energija:/var/lib/zope/
#  ZSOURCE=/var/lib/zope/
  OTHERHOST=energija
  THISHOST=sputnik
  ONEWAYFLAG=1
  ${RSYNC} ${HOME}/bin ${HOME}/.hosts/sputnik ${FILTER} || exit
##  zope/zback
  XPLANETSOURCE="/var/lib/xplanet"
  XPLANETTARGET="${ECH}@energija:/var/lib"
  echo "Synchronisiere:  Sputnik      ----->>        Energija"
elif [ `basename $0` = "s2w" ] ; then
  TARGET=${ICH}@wr-linux02:/home/${ICH}
  TARGETUSER=${ICH}
  SOURCE=${MYHOME}
#  ZTARGET=${ICH}@wr-linux02:/var/lib/zope/
#  ZSOURCE=/var/lib/zope/
  OTHERHOST=wr-linux02
  THISHOST=sputnik
  ${RSYNC} ${HOME}/bin ${HOME}/.hosts/sputnik ${FILTER} || exit
##  zope/zback
  echo "Synchronisiere:  Sputnik      ----->>        Wr-Linux02"
elif [ `basename $0` = "w2s" ] ; then
  TARGET=${MYHOME}
  TARGETUSER=${ICH}
  SOURCE=${ICH}@wr-linux02:/home/${ICH} 
#  ZTARGET=/var/lib/zope/
#  ZSOURCE=${ICH}@wr-linux02:/var/lib/zope/
  ONEWAYFLAG=1
  OTHERHOST=wr-linux02
  THISHOST=wr-linux02
  MAILFLAG=1
  ssh ${OTHERHOST} bin/sync-bin || exit
#  ssh ${OTHERHOST} zope/zback
  XPLANETSOURCE="${TARGETUSER}@${OTHERHOST}:/var/lib/xplanet"
  XPLANETTARGET="/var/lib"
  echo "Synchronisiere:  Wr-Linux02   ----->>        Sputnik"
elif [ `basename $0` = "e2s" ] ; then
  TARGET=${MYHOME}
  TARGETUSER=${ICH}
  SOURCE=${ECH}@energija:/home/${ECH}
#  ZTARGET=/var/lib/zope/
#  ZSOURCE=${ECH}@energija:/var/lib/zope/
  OTHERHOST=energija
  ssh ${ECH}@${OTHERHOST} bin/sync-bin || exit
#  ssh ${ECH}@${OTHERHOST} zope/zback
  echo "Synchronisiere:  Energija     ----->>        Sputnik"
else
  echo Falscher Aufruf: `basename $0`
  exit 1
fi

### Das funktioniert nicht ganz, weil hier ein anderer Rechner die
### Energija-IP hat
RET=`fping ${OTHERHOST} 2> /dev/null`
if [ ! `echo $RET | grep -c "alive"` -gt 0 ] ; then
  echo $OTHERHOST not reachable.
fi

read INPUT

# RSYNC="echo $RSYNC"
# RSYNC="$RSYNC -n"

# synchronisiere ~/bin
# ${RSYNC} ${SOURCE}/.hosts ${TARGET}/.hosts/${THISHOST} ${FILTER}  || exit
echo "rsync -azult --delete --temp-dir=/tmp ~/.hosts/* andreas@energija:.hosts"

# synchronisiere Zope dirs
#echo "Syncronisiere Zope"
#for z in ${ZOPE} ; do
#  echo "   $z"
#  set -x
#  ${RSYNC} --exclude zshell.py \
#           --exclude *.pyc \
#	   --exclude *Examples.zexp* \
#	   --exclude zpt_examples.zexp \
#	   --exclude ZopeTutorialExamples \
#	   --exclude myZMStheme* \
#	   --exclude lcms.zexp \
#	   --exclude bland.gif \
#	   ${ZSOURCE}/$z ${ZTARGET} ${FILTER} || true ## eigentlich exit !!!
# set +x
#done

echo "Syncronisiere home"
for dir in $SYNCDIRS ; do
   echo "   $dir"
   if echo $dir | grep -q "/" ; then
      echo "Synchronisiere subdir!"
      ${RSYNC} -v ${SOURCE}/${dir} ${TARGET}/`dirname $dir` ${FILTER} || exit
   else
      ${RSYNC} ${SOURCE}/${dir} ${TARGET} ${FILTER} || exit
   fi
done
# set -x
if [ $ONEWAYFLAG = 1 ] ; then
  if [ -d ${XPLANETSOURCE} ] ; then
    echo "Syncronisiere nur in eine Richtung"
    ${RSYNC} --exclude markers ${XPLANETSOURCE} ${XPLANETTARGET} ${FILTER} # || exit
    echo "CHECK XPLANET DIR!!!!!!!!"
    for dir in $ONEWAY ; do
      echo "   $dir"
      ${RSYNC} ${SOURCE}/${dir} ${TARGET} ${FILTER} || exit
    done
  fi
fi

echo "Syncronisiere Mail"
if [ $MAILFLAG = 1 ] ; then
    rsync wr-linux02:/var/mail/tillea /home/tillea/.pine/wr-linux02 || exit
fi

echo "Syncronisiere spezielle Dateien"
for file in $SINGLEFILES ; do
   echo $file
   ${RSYNC} ${SOURCE}/${file} ${TARGET}/${file} ${FILTER}  || exit
### wie läßt sich das verbessern????
#echo "Spezielle Dateien ..."
#echo ${RSYNC} "in <datei> aufgelistete dateien auf HOST in DIR" ${TARGET}/${file} ${FILTER}  || exit
done

echo "Fertig!"
if [ -f ~/.rsync/TODO ] ; then
   cat ~/.rsync/TODO
fi
