#!/bin/bash
#
#
# TTF installer 0.1 created by merlwiz79 <linuxmint.com>
# 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.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  
# USA
# dependencies
#	bash
#       zenity
set -x

version="0.1"
#################################################
## File and Extra Values
file=$1
font=$2
WD=440
HT=120
ic=/usr/share/icons/gnome/scalable/apps/fonts.svg
check_dirs="alee arhangai arphic baekmuk beteckna dustin fifthhorseman freefont fontinstaller kiloji kochi konatu latex-xft-fonts mikachan mona openoffice sazanami sjfonts thai ttf-ancient-scripts ttf-arabeyes ttf-atarismall ttf-bengali-fonts ttf-bitstream-vera ttf-bpg-georgian ttf-breip ttf-dejavu ttf-devanagari-fonts ttf-dzongkha ttf-ecolier-court ttf-ecolier-lignes-court ttf-essays ttf-f500 ttf-farsiweb ttf-freefarsi ttf-georgewilliams ttf-gfs-artemisia ttf-gfs-bodoni-classic ttf-gfs-complutum ttf-gfs-didot-classic ttf-gfs-gazis ttf-gfs-neohellenic ttf-gfs-solomos ttf-gfs-theokritos ttf-gujarati-fonts ttf-inconsolata ttf-indic-fonts-core ttf-junicode ttf-kacst ttf-kannada-fonts ttf-khmeros ttf-lao ttf-larabie-deco ttf-larabie-straight ttf-larabie-uncommon ttf-liberation ttf-malayalam-fonts ttf-manchufont ttf-marvosym ttf-mgopen ttf-mph-2b-damase ttf-nafees ttf-ocr-a ttf-oflb-euterpe ttf-oriya-fonts ttf-paktype ttf-punjabi-fonts ttf-radisnoir ttf-sil-abyssinica ttf-sil-andika ttf-sil-charis ttf-sil-doulos ttf-sil-ezra ttf-sil-gentium ttf-sil-padauk ttf-sil-scheherazade ttf-sil-yi ttf-summersby ttf-tamil-fonts ttf-telugu-fonts ttf-tmuni ttf-tuffy ttf-ubuntu-title ttf-xfree86-nonfree ttf-xfree86-nonfree-syriac unfonts uralic vlgothic wqy "

#################################################
#       Translations
        ###### Default = English #####
	title1="How do you want to install the font?"
	text="File name must not have any spaces!   "
	system="System" 
	user="User"
	title2="Warning!                            "
	text1="Installing to the system installs to all users.        "
	title2="User Installation                       "
	text2="Installing to the current user.      "
	question="Do you wish to continue?          "
	done="Installation is complete.            "
	done1="Please restart applications to use the font. "
	error="Error!                                "
	error1="$font is already installed.         "
	error2="Installation of the font was canceled by user!"
	error3="Font was not installed! Please check for spaces in the name."
	error4="No file was selected!                   " 
	select="Select"
	type="Installation type"
case $LANG in
	######## Czech by xxxmerlin ########
	cs* )
		title1="Jak chcete nainstalovat písmo?"
		text="Jméno souboru nesmí obsahovat mezery!   "
		system="Systém" 
		user="Uživatel"
		title2="Varování!                            "
		text1="Instalovat do systému pro všechny uživatele.        "
		title2="Uživatelská instalace                       "
		text2="Instalovat pouze pro současného uživatele.      "
		question="Chcete pokračovat?          "
		done="Instalace dokončena.            "
		done1="Pro používání písma musíte restartovat aplikaci. "
		error="Chyba!                                "
		error1="$font je již nainstalován.         "
		error2="Instalace písma byla zrušena uživatelem!"
		error3="Písmo nebylo nainstalováno! Zkontrolujte, zda jeho jméno neobsahuje žádné mezery."
		error4="Nebyl vybrán soubor!                   " 
		select="Vybrat"
		type="Typ instalace";;
        ###### Slovak by Xgamer #####
	sk* )
		title1="Ako chcete inštalovať písmo?"
		text="Názov súboru nemôže obsahovať medzery!   "
		system="Systém" 
		user="Užívateľ"
		title2="Upozornenie!                            "
		text1="Inštalovať do systému pre všetkých užívateľov.        "
		title2="Užívateľská inštalácia                       "
		text2="Inštalovanie pre terajšieho užívateľa.      "
		question="Prajete si pokračovať?            "
		done="Inštalácia dokončená.            "
		done1="Prosím reštartujte aplikáciu k použitiu nového písma. "
		error="Chyba!                                "
		error1="$font je už inštalované.         "
		error2="Inštalácia písma bola prerušené užívateľom!"
		error3="Písmo nebolo nainštalované.Skontrolujte medzery v názve."
		error4="Žiaden vybratý súbor!                   " 
		select="Vybrať"
		type="Typ inštalácie"
esac

#################################################
## Check if a font file was selected
#################
if [ "$font" = "" ]
then 
zenity --error --title="$error" --text="$error4"
exit
fi

#################################################
## Check for duplicate font file
#################
for test_dir in $check_dirs
do  
if [ -f $HOME/.fonts/$font ] || [ -f /usr/share/fonts/truetype/$test_dir/$font ] || [ -f /usr/share/fonts/truetype/$font ];
then 
zenity --error --title="$error" --text="$error1" 
exit;		
fi done 

#################################################
## Check for or create user font folder
#################
if [ ! -a "~/.fonts" ]; then
	mkdir -p ~/.fonts 
fi
#################################################
## Run the installer
##################
ans=$(zenity --width $WD --height $HT --window-icon=$ic --title="$title1" --list --text="$text" --radiolist --column="$select" --column="$type" TRUE "$user" FALSE $system);
if [ "$ans" = "$user" ] ; then
	zenity --width $WD --question --title="$text2" --text="$question"
 case $? in
                 0)
                        cp $file $HOME/.fonts/ | fc-cache ~/.fonts  ;;
                 1)
                        zenity --width=$WD --error --title="$error" --text="$error2" 
exit ;;
        esac

fi
if [ "$ans" = "$system" ] ; then
	zenity --width $WD --title="$text1" --question --text="$question"
 case $? in
                 0)
if [ ! -a "/usr/share/fonts/truetype/fontinstaller" ]; then
	gksu mkdir /usr/share/fonts/truetype/fontinstaller
fi                     
gksu cp $file /usr/share/fonts/truetype/fontinstaller/ 
 gksu fc-cache /usr/share/fonts/truetype/fontinstaller/ ;;
                 1)
 zenity --width=$WD --title="$error" --error --text="$error2" 
exit ;;
        esac

fi
if [ "$ans" = "" ] ; then
 zenity --width=$WD --title="$error" --error --text="$error2"
exit
fi
#################################################
## Check if the font was installed.
#################
if [ -f $HOME/.fonts/$font ] || [ -f /usr/share/fonts/truetype/fontinstaller/$font ] ;
then 
	zenity --width=$WD --info --title="$done" --text="$done1" 
else
	zenity --width=$WD --info --title="$error" --text="$error3" 
fi
