#!/usr/bin/env bash
#/ Usage: ghe-rsync-feature-checker <rsync-command>
#/ returns true if the passed rsync command is supported by the current version of rsync
#/ returns false if the passed rsync command is not supported by the current version of rsync
#/

set -o pipefail

# set the variable from the first argument and remove any leading dashes
rsync_command=$1

# extract dashes if present into variable
leading_dashes=$(echo "$rsync_command" | grep -oE "^-+")

# this normalizes the passed command by removing any leading dashes
normalized_command=$(echo "$rsync_command" | sed -E "s/^-+//")

# this checks the rsync command and returns the found command if it is valid
found_command=$(rsync -h | grep -oE "\B-+($normalized_command)\b" | head -n "1")

# this is the normalized found command
normalized_found_command=$(echo "$found_command" | sed -E "s/^-+//")

## Check if $leading_dashes is either - or --
if [ "$leading_dashes" == "-" ]; then
  # check if the passed rsync command is valid and supported or if the normalized command is valid and supported
  if [ "$rsync_command" == "$found_command" ]; then
    echo "true"
  else
    echo "false"
  fi
elif [ "$leading_dashes" == "--" ]; then
    # check if the passed rsync command is valid and supported or if the normalized command is valid and supported
  if [ "$rsync_command" == "$found_command" ]; then
    echo "true"
  else
    echo "false"
  fi
else
  # check if the passed rsync command is valid and supported or if the normalized command is valid and supported
  if [ "$rsync_command" == "$normalized_found_command" ]; then
    echo "true"
  else
    echo "false"
  fi
fi

