#!/bin/sh

test_nonexisting() {
	echo "Getting a configuration value that shouldn't be there"
	if [ "$(snapctl get non-existing 2>&1)" != "" ]; then
		echo "Expected getting a non-existing value to be empty"
		exit 1
	fi
}

test_snapctl_set_foo() {
	echo "Setting foo"
	if ! snapctl set foo=bar; then
		echo "snapctl set unexpectedly failed"
		exit 1
	fi
}

test_snapctl_set_bar_doc() {
  echo "Setting bar document"
  if ! snapctl set bar="{\"a\":{\"aa\":1,\"ab\":2},\"b\":3}"; then
    echo "snapctl set unexpectedly failed"
    exit 1
  fi
}

test_snapctl_get_foo() {
	echo "Getting foo"
	if ! output="$(snapctl get foo)"; then
		echo "Expected snapctl get to be able to retrieve value just set"
		exit 1
	fi

	expected_output="bar"
	if [ "$output" != "$expected_output" ]; then
		echo "Expected output to be '$expected_output', but it was '$output'"
		exit 1
	fi
}

test_get_int_number() {
  echo "Getting int number"
  if ! output="$(snapctl get intnumber)"; then
		echo "Expected snapctl get to be able to retrieve value just set"
		exit 1
	fi
	expected_output="1234567890"
	if [ "$output" != "$expected_output" ]; then
		  echo "Expected output to be '$expected_output', but it was '$output'"
		  exit 1
	fi
  if ! output="$(snapctl get intnumber2)"; then
		  echo "Expected snapctl get to be able to retrieve value just set"
		  exit 1
	fi
	expected_output="\"a\": 9876543210"
	if ! echo "$output" | grep -q -e "$expected_output"; then
		  echo "Expected output to be '$expected_output', but it was '$output'"
		  exit 1
	fi
}

test_get_nested() {
  echo "Getting a map"
  if ! output="$(snapctl get root)"; then
      echo "Expected snapctl get to be able to retrieve value just set"
      exit 1
  fi
  expected_output='{\n\t"key1": "a",\n\t"key2": "b"\n}'
  # note: "echo" is a built-in of sh and doesn't support -e flag, use /bin/echo.
  # shellcheck disable=SC2039
  if [ "$output" != "$(/bin/echo -e "$expected_output")" ]; then
      echo "Expected output to be '$expected_output' but it was '$output'"
      exit 1
  fi
}

test_snapctl_foo_null() {
	  echo "Getting foo"
	  # note, snapctl doesn't fail on non-existing keys, this check is for other unexpected errors
	  if ! output="$(snapctl get foo)"; then
		    echo "Did not expect snapctl get to fail"
		    exit 1
	  fi

	  expected_output=""
	  if [ "$output" != "$expected_output" ]; then
		    echo "Expected output to be '$expected_output', but it was '$output'"
		    exit 1
	  fi
}

test_snapctl_unset() {
	  echo "Unsetting an option"
	  if ! snapctl set root.key2! ; then
	            echo "snapctl set unexpectedly failed when un-setting root.key2"
		    exit 1
	  fi
}

test_snapctl_unset_with_unset() {
	echo "Unsetting an option"
	if ! snapctl unset root.key2 ; then
	    echo "snapctl set unexpectedly failed when un-setting root.key2"
		exit 1
	fi
}

test_exit_one() {
	echo "Failing as requested."
	exit 1
}

command=$(snapctl get command)
case $command in
	"")
		;;
	"test-nonexisting")
		test_nonexisting
		;;
  "test-snapctl-set-bar-doc")
    test_snapctl_set_bar_doc
    ;;
	"test-snapctl-set-foo")
		test_snapctl_set_foo
		;;
	"test-snapctl-get-foo")
		test_snapctl_get_foo
		;;
	"test-snapctl-foo-null")
    test_snapctl_foo_null
    ;;
	"test-exit-one")
		test_exit_one
		;;
  "test-get-int")
    test_get_int_number
    ;;
  "test-get-nested")
    test_get_nested
    ;;
  "test-unset-with-unset")
    test_snapctl_unset_with_unset
	;;
  "noop")
	;;
  "test-unset")
    test_snapctl_unset
	;;
	*)
		echo "Invalid command: '$command'"
		exit 1
		;;
esac
