#!/bin/sh -e

if [ -f "$__object/parameter/destination" ]
then
    dst="$( cat "$__object/parameter/destination" )"
else
    dst="/$__object_id"
fi

if [ ! -f "$dst" ]
then
    echo 'absent'
    exit 0
fi

if [ ! -f "$__object/parameter/sum" ]
then
    echo 'present'
    exit 0
fi

sum_should="$( cat "$__object/parameter/sum" )"

if echo "$sum_should" | grep -Fq ':'
then
    sum_should="$( echo "$sum_should" | cut -d : -f 2 )"
fi

sum_cmd="$( "$__type_explorer/remote_cmd_sum" )"

# shellcheck disable=SC2059
sum_is="$( eval "$( printf "$sum_cmd" "'$dst'" )" )"

if [ -z "$sum_is" ]
then
    echo 'existing destination checksum failed' >&2
    exit 1
fi

if [ "$sum_is" = "$sum_should" ]
then
    echo 'present'
else
    echo 'mismatch'
fi
