#!/bin/sh
# Script to modify font.desc file in user's mplayer directory
# to handle both ISO8859-2 and WINDOWS-1250 encodings.
# Piotr Wiejak <piotr@wiejak.priv.pl>              2001-11.19

cat << EOF

Skrypt modyfikuje plik font.desc w katalogu fontów mplayer-a.
Plik ten określa mapowanie znaków o określonych kodach na
ich odpowiedniki bitmapowe. Większość systemów UNIX-owych
używa kodowania polskich znaków ISO8859-2.
Systemy Micro\$oftu nie trzymają się tego standardu i używają
strony kodowej WINDOWS-1250.
Aby zapewnić poprawne wyświetlanie napisów kodowanych w obydwu
systemach, można lekko zmodyfikować mapowania znaków, tak, aby
kody zarówno ISO jak i WIN wskazywały na właściwe bitmapy 
zestawu fontów ISO8859-2.
Twój oryginalny plik "font.desc" zostanie zachowany pod nazwą
"font.desc.orig".

EOF

read -n 1 -p "Czy chcesz konynuować? [t/n]" dalej
echo ""

# If don't want to continue - exit
if [ "$dalej" != "t" ]
then
 exit
fi


# Directory where the font.desc file is placed
filedir=~/.mplayer/font

# If there is a $1, substitute it to filedir
if [ "$1" != "" ]
then
 filedir=$1
fi

# Check if file exists
if [ ! -e $filedir/font.desc ]
then
 echo "Plik $filedir/font.desc nie istnieje!!!"
 echo "Podaj katalog z fontami jako parametr skryptu."
 exit
fi

# Check if the file font.desc isn't modified
tst=`cat $filedir/font.desc | grep Wiejak`
if [ "$tst" != "" ]
then
 echo "Plik $filedir/font.desc jest już zmodyfikowany!!!"
 exit
fi

# Array to store original lines
declare -a chars_to_subst
first=0

# ISO & WIN different mappings
chars_iso=("0x00b1" "0x00b6" "0x00bc" "0x00a1" "0x00a6" "0x00ac")
chars_win=("0x00b9" "0x009c" "0x009f" "0x00a5" "0x008c" "0x008f")

# Find ISO8859-2 mappings and do remapping
for (( i=0 ; i < 6 ; i=$i+1 ))  
  do
   chars_to_subst[$i]=`cat $filedir/font.desc | grep ${chars_iso[$i]} | sed s/${chars_iso[$i]}/${chars_win[$i]}/`
  done

# Delete ISO8859-2 mappings, which has the same codes as WIN-1250
# This codes are unused and nobody really needs them, I suppose
cat $filedir/font.desc 	| grep -v ${chars_win[0]} | grep -v ${chars_win[1]} \
			| grep -v ${chars_win[2]} | grep -v ${chars_win[3]} \
			| grep -v ${chars_win[4]} | grep -v ${chars_win[5]}  > /tmp/font.$$

echo "# This file was modified to handle WINDOWS-1250 and ISO8859-2" >> /tmp/font2.$$
echo "# codepages using ISO8859-2 font set." >> /tmp/font2.$$
echo "# Comments: Piotr Wiejak <piotr@wiejak.priv.pl>" >> /tmp/font2.$$
echo "#" >> /tmp/font2.$$

# Process font.desc file 
cat /tmp/font.$$ | while read -r linia 
    do
	echo "$linia" >> /tmp/font2.$$
	if [ "$linia" == "[characters]" ]
	    then
	    # Modify only first [characters] section, leaving OSD section untouched
	    if [ $first -eq 0 ]
		then
		first=1
		# Create WINDOWS-1250 remapping section
		echo "# BEGIN - WINDOWS-1250 remappings" >> /tmp/font2.$$
		for (( i=0 ; i < 6 ; i=$i+1 ))  
		do 
		    echo ${chars_to_subst[$i]} >> /tmp/font2.$$
		done
		echo "# END - WINDOWS-1250 remappings" >> /tmp/font2.$$
	    fi
	fi
    done

# Do cleanup and backup original file
mv $filedir/font.desc $filedir/font.desc.orig
mv /tmp/font2.$$ $filedir/font.desc
rm /tmp/font.$$

echo "Gotowe."