#!/bin/bash
# synchronize a directory in a remote machine by sending only (one-way)
#   CFL - 20060222
#   CFL - 20070901 (adapted to rsync  version 2.6.8)

#syntax help
if [[ -z $1 || -n $1 && ${1:0:1} = '-' ]]
   then
   echo "Syntax: syncsend [DIR DEST | -help]"
   echo "        Synchronize directory DIR sending"
   echo "        files to remote location DEST,"
   echo "        where DEST is machine:[path]."
   echo "        (one-way only)"
   echo "Example: syncsend Mail roma:"
   echo "        For subdirectories script needs"
   echo "        to be called from parent directory,"
   echo "        DIR is just the subdirectory and "
   echo "        DEST requires path to parent directory."
   echo "Example: cd sounds; syncsend ogg roma:sounds"
   exit
fi

# read additional options
echo ""
read -p " - Additional options? (--dry-run --backup --checksum --exclude=big.file)  " OPTIONS

# set directory
DIR=$1
DIR=${DIR%'/'}     # delete eventual trailing '/'

# set destination
if [[ -n $2 ]]
   then
     DEST=$2
   else   # default destination
     DEST="roma:"
fi

#set list of exclusions
EXCLUSIONS=" --exclude=*~ --exclude=*% --exclude=*.bak --exclude=*.bck --exclude=*.backup --exclude=.xvpics --exclude=.xsession-errors "

#send files
echo "Sending "$DIR" to "$DEST
rsync --archive\
      --compress\
      --update\
      --partial --delay-updates\
      --progress\
      $EXCLUSIONS\
      $OPTIONS\
      $DIR $DEST

#check files to delete
echo "__________________________"
echo "Reporting files to delete:"
rsync --dry-run\
      --archive\
      --update\
      $EXCLUSIONS\
      --delete\
      --itemize-changes\
      $OPTIONS\
      $DIR $DEST | grep deleting


echo ""
read -p " - Delete?(y/n) " ANSWER
#actually delete files
if [[ -n $ANSWER &&  $ANSWER = 'y' ]]
   then
     echo "Deleting files..."
     rsync \
        --archive\
        --update\
        $EXCLUSIONS\
        --delete\
        $OPTIONS\
        $DIR $DEST
fi

