#!/bin/bash
#
#
# TTF installer 0.2 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
#
# 
# version 0.4 2011-12-28
# modified for salix OS and removed system wide installation
#
# Ingemar Karlsson <ingemar@ingk.se>
# salixos.ingk.se
#
set -x

version="0.4"
#################################################
## 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 #####
		text="The file must have at least read only Permissions!   "
		user="User"
		title2="Warning!                            "
		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="The font was not installed! Please check the file Permissions!!"
		error4="No file was selected!                   " 
		select="Select"
		type="Installation type"
case $LANG in
	######## Swedish by Zwopper ########
	sv* )
		text="Filen måste som minst ha läsrättigheter! "
		user="Användarnivå"
		title2="Varning!                            "
		title2="Installation på användarnivå                       "
		text2="Installerar till nuvarande användare.      "
		question="Vill du fortsätta?                 "
		done="Installationen är klar.            "
		done1="Du måste starta om öppna program för att använda de nya typsnitten. "
		error="Fel!                                "
		error1="$font är redan installerat.         "
		error2="Installationen av typsnitt avbröts av användaren!"
		error3="Typsnittet installerades inte! Kontrollera att namnet inte innehåller något mellanslag."
		error4="Ingen fil valdes!                   " 
		select="Välj"
		type="Installationstyp";;
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/TTF/$test_dir/$font" ] || [ -f "/usr/share/fonts/TTF/$font" ]; then 
		zenity --error --title="$error" --text="$error1" 
		exit;
	fi
done 

#################################################
## Check for or create user font folder
#################################################
if [ ! -d "~/.fonts" ]; then
	mkdir -p ~/.fonts 
fi
#################################################
## Run the installer
#################################################
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

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