#!/bin/sh
set -eu


usage() {
	cat <<-'EOF'
		Usage:
		  slugify < STDIN
		  slugify -h
	EOF
}

help() {
	cat <<-'EOF'

		Options:
		  -h, --help    show this message


		"slugify" the input string, removing diacritics and punctuation
		from the string, on a best-effort basis.


		Examples:

		  Slugify the input string:

		    $ echo 'Saçi-pererê, tomando açaí!!' | slugify
		    saci-perere-tomando-acai
	EOF
}


for flag in "$@"; do
	case "$flag" in
		--)
			break
			;;
		--help)
			usage
			help
			exit
			;;
		*)
			;;
	esac
done

while getopts 'h' flag; do
	case "$flag" in
		h)
			usage
			help
			exit
			;;
		*)
			usage >&2
			exit 2
	esac
done
shift $((OPTIND - 1))

iconv -ct ASCII//TRANSLIT |
	tr '[:upper:]' '[:lower:]' |
	sed \
		-e 's/[^a-z0-9]/-/g' \
		-e 's/--*/-/g'       \
		-e 's/^-//'          \
		-e 's/-$//'