#!/usr/bin/env bash
set -o pipefail

function show_help {
cat << EOF
Usage: s3sync [options] AWS_ACCESS_KEY_ID AWS_SECRET_KEY_ID SOURCE_DIR DEST_BUCKET

Sync files in SOURCE_DIR to the s3 bucket at DEST_BUCKET, using specified key 
pair. 

Required Arguments:
  AWS_ACCCESS_KEY_ID   AWS Access Key ID to use when communicating with AWS.
                       Needs to have permissions in the destination bucket.
  AWS_SECRET_KEY       AWS Secret Key to use when communicating with AWS.
  SOURCE_DIR           Local directory to sync with S3
  DEST_BUCKET          Remote bucket to sync into. Specify like this: 
                         s3://<bucket>/<path>
                       for example "s3://ids-netflows/data"

Optional Arguments:
  --delete-removed     Delete files that exist remotely but are no longer 
                       present locally
  --exclude            Using standard shell wildcards, exclude files matching 
                       a pattern.
  -F, --flow-symlinks  Follow symbolic links as if they are regular files
  --include            Using standard shell wildcards, only include files 
                       matching a pattern.
  -n, --dry-run        No-op - just show what would be uploaded/deleted
  --no-check-md5       Don't check md5sum of files to know whether to 
                       overwrite; just check file size. May be much faster for
                       large local files, but more likely to erroneously 
                       believe files are already synced.
  -q, --quiet          Silence output on stdout
  -r, --recursive      Upload contents of subdirectories, too.
  --skip-existing      Don't overwrite remote files, even if checksums/file 
                       sizes have changesd
  -v, --verbose        Enable verbose output
EOF
}
if [[ $# -lt 4 ]] ; then
    show_help
    exit 0
fi

noptions=$(($#-4))
options="${@:0:$noptions}"
shift $noptions
if [[ $# -lt 4 ]] ; then
    show_help
    exit 0
fi
access="$1"
secret="$2"
local="$3"
remote="$4"
shift 4


here=$( cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd ) # directory of script

$here/s3env/bin/s3cmd sync $options --access_key=$access --secret_key=$secret "$local" "$remote"
