#!/bin/bash
# synchronize a directory in a remote machine by getting 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: syncget [DIR ORIG | -help]"
   echo "        Synchronize directory DIR getting"
   echo "        files from remote location ORIG,"
   echo "        where ORIG is machine:[path]."
   echo "        (one-way only)"
   echo "Example: syncget Mail roma:"
   echo "        DIR is always relative to the home"
   echo "        directory and for subdirectories"
   echo "        the script needs to be called from"
   echo "        the parent directory."
   echo "Example: cd ~/sounds; syncget sounds/ogg roma:"
   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
     ORIG=$2
   else   # default destination
     ORIG="roma:"
fi

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

#send files
echo "Getting "$DIR" from "$ORIG
rsync --archive\
      --compress\
      --update\
      --partial --delay-updates\
      --progress\
      $EXCLUSIONS\
      $OPTIONS\
      $ORIG$DIR .

#check files to delete
echo "__________________________"
echo "Reporting files to delete:"
rsync --dry-run\
      --archive\
      --update\
      $EXCLUSIONS\
      --delete\
      --itemize-changes\
      $OPTIONS\
      $ORIG$DIR . | 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\
        $ORIG$DIR .
fi

