#!/bin/bash -eu

#
# Kézikönyv oldalak kipofozása. Nem tömörítjük őket, sőt, kitömörítjük, ha
# esetleg tömörítve lennének. Átalakítjuk őket UTF-8 kódolásúra.
#

. /usr/lib/uhubuild/uhubuild-common

language_to_charset() {
	local lang="$1"
	if [ "$lang" = "ca" ]; then
		echo "ISO-8859-1"
		return
	fi
	if [ "$lang" = "ru" ]; then
		echo "KOI8-R"
		return
	fi
	if [ "$lang" = "sr" ]; then
		echo "ISO-8859-5"
		return
	fi
	local locales
	local reply
	local charset
	locales="$(locale -a | grep -E "^$lang(_|\$)" | grep -F -v . | grep -F -v @ | cat)"
	reply="unknown"
	for locale in $locales; do
		charset="$(LC_ALL="$locale" locale charmap)"
		if [ "$reply" = "unknown" ]; then
			reply="$charset"
			continue
		fi
		if [ "$charset" != "$reply" ]; then
			reply="ambiguous"
			break
		fi
	done
	echo "$reply"
}

convert_from() {
	local from="$1"
	local file="$2"
	if iconv -f UTF-8 -t UTF-8 <"$file" >/dev/null 2>&1; then
		return
	fi
	if [ "$from" = "unknown" -o "$from" = "ambiguous" ]; then
		error "Source charset of ${file##*/man//} is $from."
		error "Please convert it to UTF-8 manually in the install script."
		return 1
	fi
	echo "  Converting ${file##*/man//} from $from to UTF-8"
	iconv -f "$from" -t UTF-8 <"$file" >"$file.tmp"
	touch -r "$file" "$file.tmp"
	pretendroot mv "$file.tmp" "$file"
}

for pkg in $UB_PACKAGES; do
	cd "$UB_PACKAGEDIR/$pkg"
	for dir in usr/share; do
		if [ ! -d "$dir" ]; then
			continue
		fi
		for d in "$dir"/man[0-9]; do
			error "$d könyvtár nem lesz jó!"
		done
		dir="$dir/man/"
		if [ ! -d "$dir" ]; then
			continue
		fi

		# helyrepofozzuk a jogosultsagokat
		find "$dir"         | xargs -r pretendroot chown -h 0:0
		find "$dir" -type f | xargs -r pretendroot chmod 644
		find "$dir" -type d | xargs -r pretendroot chmod 755
		# kibontjuk a tomoritett man oldalakat
		find "$dir" -type f -name '*.gz'  -print0 | xargs -r0 gunzip  -f --
		find "$dir" -type f -name '*.bz2' -print0 | xargs -r0 bunzip2 -f --
		# átalakítás UTF-8-ra
		for d in "$dir"/*; do
			if [ -d "$d" ]; then
				d="${d##*/}"
				case "$d" in
				  man*)
					charset="ISO-8859-1"
					;;
				  *)
					charset="$(language_to_charset "$d")"
					;;
				esac
				for file in $(find "$dir/$d" -type f); do
					convert_from "$charset" "$file"
				done
			fi
		done
	done
done
