#! /bin/bash

OPT=""
SOURCE=""
DESTINATION=""
while true ; do
    case "$1" in
    --source)       OPT="source";;
    --destination)  OPT="dest";;
    "")             break ;;
    -*)             echo "Unknown option $1" >&2; break;;
    *)
        if [ "$OPT" = "source" ]; then
            SOURCE="$1"
        elif [ "$OPT" = "dest" ]; then
            DESTINATION="$1"
        else
            echo "Parameter without option. Skip"
        fi
        ;;
    esac
    shift
done

if [ -z "$SOURCE" ] || [ -z "$DESTINATION" ]; then
    echo "Invalid parameters" >&2
    exit 1
fi

if [ ! -f "$SOURCE" ]; then
    echo "No such file: $SOURCE" >&2
    exit 1
fi

DESTDIR=$(dirname "$DESTINATION")
if [ ! -d "$DESTDIR" ]; then
    echo "Destination directory does not exist: $DESTDIR" >&2
    exit 1
fi

if [ -f "$DESTINATION" ]; then
    echo "File already exists: $DESTINATION" >&2
    rm -f "$DESTINATION"
    # returning 1 will instruct postgres to retry
    exit 1
fi

TMPDEST="${DESTINATION}.TMP"
if ! cp --preserve=all "$SOURCE" "$TMPDEST"; then
    echo "Copy command failed" >&2
    [ -e "$TMPDEST" ] && rm -f "$TMPDEST"
    exit 1
fi

SHA1SOURCE=$(sha1sum "$SOURCE" | cut -f1 -d' ')
SHA1DEST=$(sha1sum "$TMPDEST" | cut -f1 -d' ')

if [ "$SHA1SOURCE" != "$SHA1DEST" ]; then
    echo "Checksum error $DESTINATION: $SHA1SOURCE vs. $SHA1DEST" >&2
    # remove broken backup file
    rm -f "$TMPDEST"
    exit 1
fi

mv "$TMPDEST" "$DESTINATION"
exit 0
