#!/bin/bash
#
# $Id$

if [ "$2" = "" ]
then
  echo "Parameters missing!"
  echo "Syntax: $0 SRCDIR DESTDIR [Parameters]"
  echo "Common additional parameters are:"
  echo '  -C                   to exclude files that CVS excludes'
  echo '  --delete-before      to delete before transfer instead of after'
  echo '  -exclude="PATTERN"   to exclude certain files'
  echo '  -exclude-from FILE   to exclude certain files'
  echo '  --delete-excluded    to delete dest files matching exclude pattern'
  echo '  -include="PATTERN"   to include certain excluded files'
  echo '  -include-from FILE   to include certain excluded files'
  echo '  -z                   to gzip the transferred data'
  echo '  --compare-dest DIR   to create a new DIR for the new correct target'
  echo '  --progress           to show the progress'
#  echo ' --stats              to show statistic infos'
  exit 2
fi
SRC=$1
DEST=$2
shift
shift
PARAMS=$*

SRCREMOTE=`echo $SRC |grep ':'` 
if [ "$SRCREMOTE" = "" ]
then
  SRCHOSTPART=""
  SRCLOCALPART="$SRC"
else
  SRCHOSTPART=`echo $SRC |cut -f1 -d':'`':'
  SRCLOCALPART=`echo $SRC |cut -f2 -d':'`
fi

DESTREMOTE=`echo $DEST |grep ':'` 
if [ "$DESTREMOTE" = "" ]
then
  DESTHOSTPART=""
  DESTLOCALPART="$DEST"
else
  DESTHOSTPART=`echo $DEST |cut -f1 -d':'`':'
  DESTLOCALPART=`echo $DEST |cut -f2 -d':'`
fi

if [ "$DESTLOCALPART" = "" ]
then
  DESTLOCALPART="$SRCLOCALPART"
fi
if [ "$SRCLOCALPART" = "" ]
then
  SRCLOCALPART="$DESTLOCALPART"
fi
echo "$SRCLOCALPART" |cut -c1 |grep '/' >/dev/null || SRCLOCALPART=$(pwd)"/$SRCLOCALPART"
echo "$DESTLOCALPART" |cut -c1 |grep '/' >/dev/null || DESTLOCALPART=$(pwd)"/$DESTLOCALPART"

if [ "$SRCLOCALPART" = "/" ] ; then
  echo "ERROR: Refusing to sync source path /"
  exit 11
fi
if [ "$DESTLOCALPART" = "/" ] ; then
  echo "ERROR: Refusing to sync destination path /"
  exit 12
fi

REALSRC="$SRCHOSTPART$SRCLOCALPART"
REALDEST="$DESTHOSTPART$DESTLOCALPART"

BASEPARAMS="--delete --delete-after --force -g -l -o -p -v -S -t --timeout=8"
#--stat
#BASEPARAMS="-p -u --delete --force --timeout=180"
if [ "$SRCREMOTE$DESTREMOTE" = "" ]
then
  BASEPARAMS="-W $BASEPARAMS"
  ISLOCAL="locally"
else
  # BASEPARAMS="--compress --progress --rsh ssh $BASEPARAMS"
  if [ -z "$RSYNC_RSH" ] ; then
    BASEPARAMS="--compress --partial --progress --rsh ssh $BASEPARAMS"
  else
    BASEPARAMS="--compress --partial --progress $BASEPARAMS"
  fi
  ISLOCAL="remotely"
fi
if [ -d $SRCLOCALPART ]
then 
  BASEPARAMS="-r $BASEPARAMS"
  REALDEST="$REALDEST/"
  REALSRC="$REALSRC/"
  ISRECURSIVE="recursive"
else
  if [ -d $DESTLOCALPART ]
  then 
    BASEPARAMS="-r $BASEPARAMS"
    REALDEST="$REALDEST/"
    REALSRC="$REALSRC/"
    ISRECURSIVE="recursive"
  else
    BASEPARAMS="-r $BASEPARAMS" # fixes rsync ubuntu 6.10 problem
    ISRECURSIVE="nonrecursive"
  fi
fi

echo "Syncing"
echo "   $REALSRC"
echo "to"
echo "   $REALDEST"
echo "($ISLOCAL, $ISRECURSIVE)."
#echo "Commandline: $BASEPARAMS $PARAMS $REALSRC $REALDEST"
#echo "Press Enter to accept"
#read enter

#echo Calling
echo rsync $BASEPARAMS $PARAMS $REALSRC $REALDEST
rsync $BASEPARAMS $PARAMS $REALSRC $REALDEST
