clear
#
# Drtic Installer v0.5
#
echo "DRTIC v1.1-en Installation"
echo "=========================="
echo
echo "Plugin instalation started.."

instaluj()
{
    cd /
    gzip -dc /tmp/dti-data.bin | tar xf -  || return 1
		return 0
}
 
if instaluj ; then
  echo "Instalation successful..."
else
  echo "Installation failed."
	return
fi

if [ ! -e /tmp/inc ] ; then
	echo "Chybi cast instalacniho balicku!"
	return
fi

if [ ! -e /var/tit ]; then
	echo " "
	echo "Chose subtitles location:"
	echo "1 - hard disk (/hdd/local/tit)"
	echo "2 - USB (/usb/local/tit)"
	echo "3 - default location (/tmp)"
	echo "0 - already installed, don't modify"
	echo ""
	read C

	DOTMP=1
	if [ "$C" = "1" ] ; then
		echo "Subtitles will be stored on hdd"
		ln -sf /hdd/local/tit /var/tit && DOTMP=0
	elif [ "$C" = "2" ] ; then
		echo "Subtitles will be stored on USB"
		ln -sf /usb/local/tit /var/tit && DOTMP=0
	elif [ "$C" = "0" ] ; then
		DOTMP=0
	fi

	if [ "$DOTMP" = "1" ] ; then
		echo "Subtitles will be stored in /tmp"
		ln -sf /tmp /var/tit
	fi
fi

echo " "
echo "Chose Drtic's font:"
echo "-=-=-=-=-=-=-=-=-=-"

reqF="/share/fonts/md_khmurabi_10.ttf"
defaultF="x"

poc=0
if [ -e "$reqF" ] ; then
	echo "0 - Default font"
	defaultF="$reqF"
fi

if [ -e "/var$reqF" ]; then
	defaultF="$defaultF /var$reqF"
	echo "1 - Alternative default font"
	poc=1
fi

fnts=$(ls /share/fonts/*.ttf /var/share/fonts/*.ttf 2>&1 | grep -v Drtic.ttf | grep -v directory )

for i in $fnts ; do
	poc=$(/tmp/inc $poc)
	echo "$poc - $i" | sed 's!/var/!VAR:  / !' | sed s!/.*/\\\(.*\\\)\\.ttf\$!\\1!
done

vybran="x"
echo " "

while [ "$vybran" = "x" ] ; do

echo -n "Your choice: "
read zvf


poc=0
vybran="x"
for i in $defaultF $fnts ; do
	if [ "$zvf" = "$poc" ]; then
		vybran=$i
		break
	fi 
	poc=$(/tmp/inc $poc)
done
done

echo $vybran

if [ ! -d /var/share/fonts ] ; then
	mkdir -p /var/share/fonts
fi

rm /var/share/fonts/Drtic.ttf 2> /dev/null
ln -sf $vybran /var/share/fonts/Drtic.ttf

rm /tmp/dti-data.bin 2> /dev/null
rm /tmp/install 2> /dev/null
rm /tmp/inc 2> /dev/null

echo "Installation finished."
return

