#!/bin/sh
#
# Search book title via Gutendex API.
#
# Usage: script.sh TITLE
#

[ -z "$1" ] && {
	printf "Usage: %s <book title>\n" "$0"
	exit 1
}

fetch_book_by_title() {
	title="$1"
	search_title=$(printf '%s' "$title" | jq -sRr @uri)

	curl -s "https://gutendex.com/books/?search=$search_title" |
		jq -r '
        .results[0]
        | (.authors[0].name // "Unknown")
          + ". "
          + .title
          + ". Gutenberg ID: "
          + (.id|tostring)
      '
}

fetch_book_by_title "$1"
