#!/bin/bash

if [[ "$DISPLAY" && "$(which Xdialog 2>&1 | grep -v "which: no")" ]]; then
	dialog="Xdialog"
else
	dialog="dialog"
fi

if [ ! -e "$1" ]; then
	$dialog --title "Not Found" --msgbox "\n$1\nFile not found\n" 0 0
	exit
fi

filename=$(basename "$1")
fname=`echo "$filename" | tr "[:upper:]" "[:lower:]" | tr ' ' '_'`
fext=${fname##*.}

if [ $fext != "ttf" ]; then
	$dialog --title "Not a TTF?" --msgbox "\n$1\nDoes not appear to be a TrueType Font\n" 0 0
	exit
fi

# Stick font in global font dir if root.
# in $HOME/.fonts if a user

if [ `id -u` = 0 ]; then 
	cp "$1" /usr/share/fonts/TTF/$fname
	fc-cache /usr/share/fonts/TTF
else
	if [ ! -d "$HOME/.fonts" ]; then
    	mkdir $HOME/.fonts
	fi
	cp $1 $HOME/.fonts/$fname
	fc-cache $HOME/.fonts
fi

if [ $? = 1 ]; then
	clear
	$dialog --title "Error..." \
        	--msgbox "$1\nwas not properly installed.\n\n" 0 0
else
	$dialog --title "Complete" \
        	--infobox "$1\nhas been installed.\n" 0 0 4000
fi
exit


