#!/bin/sh



set -eu
umask 0022
export LC_ALL=C
export PATH="$(command -p getconf PATH 2>/dev/null)${PATH+:}${PATH-}"
case $PATH in :*) PATH=${PATH#?} ;; esac
export UNIX_STD=2003 # to make HP-UX conform to POSIX

print_usage_and_exit() {
	cat <<-USAGE 1>&2
		Usage   : ${0##*/} <file> ...
		Args    : <file> ... Text file for URL decoding
		Version : 2020-05-06 22:42:19 JST
		          (POSIX Bourne Shell/POSIX commands)
	USAGE
	exit 1
}


case "${1:-}" in
--help | --version | -h) print_usage_and_exit ;;
esac


(
	cat ${1+"$@"}
	echo ''
) |
	awk '                                                                #
BEGIN {                                                              #
  OFS = "";                                                          #
  ORS = "";                                                          #
  for (i=0; i<256; i++) {                                            #
    l  = sprintf("%c",i);                                            #
    k1 = sprintf("%02x",i);                                          #
    k2 = substr(k1,1,1) toupper(substr(k1,2,1));                     #
    k3 = toupper(substr(k1,1,1)) substr(k1,2,1);                     #
    k4 = toupper(k1);                                                #
    p2c[k1]=l;p2c[k2]=l;p2c[k3]=l;p2c[k4]=l;                         #
  }                                                                  #
  while (getline line) {                                             #
    gsub(/\+/, " ", line);                                           #
    while (length(line)) {                                           #
      if (match(line,/%[0-9A-Fa-f][0-9A-Fa-f]/)) {                   #
        print substr(line,1,RSTART-1), p2c[substr(line,RSTART+1,2)]; #
        line = substr(line,RSTART+RLENGTH);                          #
      } else {                                                       #
        print line;                                                  #
        break;                                                       #
      }                                                              #
    }                                                                #
    print "\n";                                                      #
  }                                                                  #
}' |
	awk '                                                                #
BEGIN{                                                               #
  ORS="";                                                            #
  OFS="";                                                            #
  getline line;                                                      #
  print line;                                                        #
  while (getline line) {                                             #
    print "\n",line;                                                 #
  }                                                                  #
}                                                                    #
'
