#!/bin/bash --
# -*- coding: UTF-8 -*-
#
# Skrypt pokazujący różne sposoby krzaczenia się polskich znaków.
# Przygotowany do pracy z systemem używającym kodowania UTF-8.
#
# encodings.sh (c) Maciej Bliziński 2008
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.

ENCODINGS=(utf-8 iso-8859-2 iso-8859-1 cp1250)
SRC=("Pchnąć w tę łódź jeża i ośm skrzyń fig.")

function interpret_as
{
	local TEXT
	local ENC_ACTUAL="$1"
	shift
	local ENC_ASSUMED="$1"
	shift
	while [[ "$1" ]]
	do
		TEXT="$1"
		echo -n "| ${ENC_ACTUAL} | ${ENC_ASSUMED} | "
		echo -n "$TEXT" \
			| iconv -c -s -f utf-8 -t "${ENC_ACTUAL}" \
			| iconv -c -s -f "${ENC_ASSUMED}" -t utf-8 \
			| sed -e 's/[^[:print:]]/?/g'
		if [[ "${PIPESTATUS[1]}" -ne 0 || "${PIPESTATUS[2]}" -ne 0 ]]
		then
			echo -n "| ERR"
		else
			echo -n "| ok"
		fi
		echo " |"
		shift
	done
}

echo "| Prawdziwe | Wzięte za | Wygląda tak... | ok? |"
for enc1 in "${ENCODINGS[@]}"
do
	if [[ "${enc1}" == "iso-8859-1" ]]; then continue; fi
	for enc2 in "${ENCODINGS[@]}"
	do
		interpret_as "${enc1}" "${enc2}" "${SRC[@]}"
	done
done