#!/usr/bin/env sh
# Decode urlencoded stdin
#
# Usage:
#
#   urldecode 'foo%20bar'
#   printf 'foo%%20bar' | urldecode

while getopts h opt; do
    case $opt in
    h) awk 'NR == 1 { next } /^$/ { exit } { print substr($0, 3) }' "$0"
       exit ;;
    esac
done
shift $(( OPTIND - 1 ))

{
    printf '<foo><![CDATA['
    if [ $# -eq 0 ]; then
        cat
    else
        printf '%s' "$1"
    fi
    printf ']]></foo>\n';
} \
    | xmlstarlet sel -T -t -v 'str:decode-uri(//foo)'
