#!/bin/sh

[ -z $GTKDIALOG ] && GTKDIALOG=gtkdialog

# Pfontview (c) Thunor 2012
# GNU GENERAL PUBLIC LICENSE Version 2, June 1991
# See http://www.gnu.org/licenses/gpl-2.0.txt

## Strip comments from the entire project using:
## for f in func* main; do sed -i -e '/## /d' -e '/ ##/d' $f; done

## ----------------------------------------------------------------------------
## Constants
## ----------------------------------------------------------------------------
## These are application defaults which may be updated from rcfiles.

TARGET=pfontview
TITLE=Pfontview
VERSION=0.1.3
LOCAL_DATA_DIR=$HOME/.$TARGET
PACKAGE_DATA_DIR=`pwd`
WINDOW_TITLE="$TITLE - ${1##*/}"
## Setting these to non-zero will generate lots of useful debugging info.
DEBUG_CONTENT=0
DEBUG_TRANSITS=0

## These don't require exporting.
DebugSpacer=
#"[]"
LocalFontDir=$HOME/.fonts
FontSymlink=$LocalFontDir/${TARGET}.XXXXXXXX
WindowIcon=mini-font.xpm
FamilyTextState=true
StyleTextState=true
FontButtonState=1
PreviewFontSize=20
PreviewText=
WindowWidth=300
WindowHeight=300

## ----------------------------------------------------------------------------
## Includes
## ----------------------------------------------------------------------------
## These files are sourced from the PATH.

PATH=$PACKAGE_DATA_DIR:$PATH

. funcFontQuery
. funcgtkrcCreate
. funci18nApply
. funcrcCreate
. funcrcGet
. funcrcSave
. funcrcSet
. functmpGet
. functmpSet

## ----------------------------------------------------------------------------
## Local Functions
## ----------------------------------------------------------------------------

## Enable the embedding of comments within the XML.
Comment() { :; }

## Convert 1 to true and 0 to false.
## On entry: $1 = 1 or 0
##  On exit: echoes true or false
funcTorF() {
	if [ $1 -eq 1 ]; then
		echo true
	else
		echo false
	fi
}

## Get major/minor/micro version of an application.
## On entry: $1 = command
##           $2 = index into string to locate version
##  On exit: Initialises major/minor/micro_version
funcAppVersionGet() {
	local field
	local index=0

	for field in `$1`; do
		if [ $index -eq $2 ]; then break; fi; index=$((index + 1))
	done
	major_version=${field%%.*}; field=${field#*.}
	minor_version=${field%%.*}
	micro_version=${field#*.}
}

## Create the XML for the Tools menu.
## On entry: $1 = the font filename from the command-line
funcToolsMenuCreate() {
	local pfont2=`command -v pfont2`
	local ttf=`command -v ttf`
	local sfontview=`command -v sfontview`

	if [ "$pfont2" -o "$ttf" -o "$sfontview" ]; then
		echo '<menu use-underline="true" label="'$i18nTools'">'
		if [ "$pfont2" ]; then
			echo \
'				<menuitem use-underline="true" label="_Pfont2">
					<action>pfont2 "'$1'" &</action>
				</menuitem>'
		fi
		if [ "$ttf" ]; then
			echo \
'				<menuitem use-underline="true" label="Pfont2 P_review">
					<action>ttf "'$1'" &</action>
				</menuitem>'
		fi
		if [ "$sfontview" ]; then
			echo \
'				<menuitem use-underline="true" label="_Sfontview">
					<action>sfontview "'$1'" &</action>
				</menuitem>'
		fi
		echo '			</menu>'
	fi
}

## ----------------------------------------------------------------------------
## Main
## ----------------------------------------------------------------------------

## Check requirements.
if [ ! `command -v fc-query` ]; then
	echo "Couldn't find fc-query"
	exit 1
fi
if [ ! `command -v $GTKDIALOG` ]; then
	echo "Couldn't find $GTKDIALOG"
	exit 1
fi
funcAppVersionGet "$GTKDIALOG -v" 2
if [ $minor_version -ge 8 -a $micro_version -ge 2 ]; then
	true
else
	echo "Couldn't find $GTKDIALOG >= 0.8.2"
	exit 1
fi

## Create the local data directory if it doesn't already exist.
if [ ! -d $LOCAL_DATA_DIR ]; then
	mkdir $LOCAL_DATA_DIR
	if [ $? -ne 0 ]; then
		echo "Couldn't create $LOCAL_DATA_DIR"
		exit 1
	fi
fi

## Create the local font directory if it doesn't already exist.
if [ ! -d $LocalFontDir ]; then
	mkdir $LocalFontDir
	if [ $? -ne 0 ]; then
		echo "Couldn't create $LocalFontDir"
		exit 1
	fi
fi

## Create the rcfiles.
funcrcCreate

## Process the command-line argument(s).
if [ -z "$1" -o "$1" = -h -o "$1" = --help ]; then
	echo "$TITLE $VERSION (C) 2012 Thunor
Usage: $TARGET [FILE]

Where FILE is a TrueType font file.
"
	exit 1
fi

## Create a temporary directory.
TEMP_DIR=`mktemp -d -t ${TARGET}.XXXXXXXX`
if [ $? -ne 0 ]; then
	echo "Couldn't create temporary directory."
	exit 1
fi

## Load the initial rcfiles required.
FamilyTextState="`funcrcGet FamilyTextState`"
StyleTextState="`funcrcGet StyleTextState`"
FontButtonState="`funcrcGet FontButtonState`"
PreviewText="`funcrcGet PreviewText`"
WindowWidth="`funcrcGet WindowWidth`"
WindowHeight="`funcrcGet WindowHeight`"

## Internationalise.
funci18nApply

## Create a symlink within the local font dir to the TrueType font file.
FontSymlink=`mktemp $FontSymlink`
ln -fs "$1" $FontSymlink

## Query the font for the required information.
funcFontQuery $FontSymlink

## Create a custom style.
funcgtkrcCreate

## Create the initial files required.
functmpSet PreviewText "`funcrcGet PreviewText`"
functmpSet Pangrams '<span font="'"$FontFamily $FontStyle 8"'">'"$i18nPangram"'</span>
<span font="'"$FontFamily $FontStyle 12"'">'"$i18nPangram"'</span>
<span font="'"$FontFamily $FontStyle 16"'">'"$i18nPangram"'</span>
<span font="'"$FontFamily $FontStyle 24"'">'"$i18nPangram"'</span>
<span font="'"$FontFamily $FontStyle 48"'">'"$i18nPangram"'</span>
<span font="'"$FontFamily $FontStyle 64"'">'"$i18nPangram"'</span>'

## Create the XML for the main dialog.
echo '
<window title="'$WINDOW_TITLE'" image-name="'$WindowIcon'"
	default-width="'$WindowWidth'" default-height="'$WindowHeight'"
	window-position="2" border-width="0">
	<vbox spacing="0">
		<menubar space-expand="false" space-fill="false">
			<menu use-underline="true" label="'$i18nFile'">
				<menuitem stock-id="gtk-save" label="'$i18nSave_Settings'"
					accel-key="0x53" accel-mods="4">
					<action>. funcrcSave; funcrcSave</action>
				</menuitem>
				<menuitemseparator></menuitemseparator>
				<menuitem stock-id="gtk-quit" accel-key="0x51" accel-mods="4">
					<action>exit:Quit</action>
				</menuitem>
			</menu>
			<menu use-underline="true" label="'$i18nView'">
				<menuitem checkbox="'$FamilyTextState'" use-underline="true">
					<variable>muiShowFamily</variable>
					<label>"'$i18nShow_Family'"</label>
					<action>if true show:hbxFamily</action>
					<action>if true enable:hbxFamily</action>
					<action>if false hide:hbxFamily</action>
					<action>if false disable:hbxFamily</action>
				</menuitem>
				<menuitem checkbox="'$StyleTextState'" use-underline="true">
					<variable>muiShowStyle</variable>
					<label>"'$i18nShow_Style'"</label>
					<action>if true show:hbxStyle</action>
					<action>if true enable:hbxStyle</action>
					<action>if false hide:hbxStyle</action>
					<action>if false disable:hbxStyle</action>
				</menuitem>
				<menuitemseparator></menuitemseparator>
				<menu stock-id="gtk-select-font" label="'$i18nShow_Font_Button'">
					<menuitem radiobutton="'`echo $(($FontButtonState & 1))`'"
						use-underline="true">
						<variable>muiFontButtonNone</variable>
						<label>"'$i18nNone'"</label>
						<action>if true hide:ftbBig</action>
						<action>if true disable:ftbBig</action>
						<action>if true hide:ftbSmall</action>
						<action>if true disable:ftbSmall</action>
					</menuitem>
					<menuitem radiobutton="'`echo $(($FontButtonState >> 1 & 1))`'"
						use-underline="true">
						<variable>muiFontButtonSmall</variable>
						<label>"'$i18nSmall'"</label>
						<action>if true hide:ftbBig</action>
						<action>if true disable:ftbBig</action>
						<action>if true show:ftbSmall</action>
						<action>if true enable:ftbSmall</action>
					</menuitem>
					<menuitem radiobutton="'`echo $(($FontButtonState >> 2 & 1))`'"
						use-underline="true">
						<variable>muiFontButtonBig</variable>
						<label>"'$i18nBig'"</label>
						<action>if true show:ftbBig</action>
						<action>if true enable:ftbBig</action>
						<action>if true hide:ftbSmall</action>
						<action>if true disable:ftbSmall</action>
					</menuitem>
				</menu>
			</menu>
			'"$(funcToolsMenuCreate "$1")"'
		</menubar>
		<vbox border-width="5" space-expand="true" space-fill="true">
			<hbox visible="'$FamilyTextState'" sensitive="'$FamilyTextState'"
				space-expand="false" space-fill="false">
				<text use-markup="true" width-request="60"
					space-expand="false" space-fill="false">
					<label>"<b>'$i18nFamily':</b>"</label>
				</text>
				<text selectable="true"
					space-expand="false" space-fill="false">
					<label>"'$FontFamily'"</label>
				</text>
				<text space-expand="true" space-fill="true">
					<label>"'$DebugSpacer'"</label>
				</text>
				<variable>hbxFamily</variable>
			</hbox>
			<hbox visible="'$StyleTextState'" sensitive="'$StyleTextState'"
				space-expand="false" space-fill="false">
				<text use-markup="true" width-request="60"
					space-expand="false" space-fill="false">
					<label>"<b>'$i18nStyle':</b>"</label>
				</text>
				<text selectable="true"
					space-expand="false" space-fill="false">
					<label>"'$FontStyle'"</label>
				</text>
				<text space-expand="true" space-fill="true">
					<label>"'$DebugSpacer'"</label>
				</text>
				<variable>hbxStyle</variable>
			</hbox>
			<fontbutton font-name="'$FontFamily $FontStyle $PreviewFontSize'"
				use-font="true" use-size="true" show-size="false" title=""
				visible="'`funcTorF $(($FontButtonState >> 2 & 1))`'"
				sensitive="'`funcTorF $(($FontButtonState >> 2 & 1))`'"
				space-expand="false" space-fill="false">
				<variable>ftbBig</variable>
			</fontbutton>
			<vbox space-expand="true" space-fill="true">
				<vbox scrollable="true" hscrollbar-policy="2">
					<eventbox name="WhiteBg">
						<text name="BlackFg" use-markup="true" wrap="false">
							<input file>'$TEMP_DIR'/Pangrams</input>
						</text>
					</eventbox>
				</vbox>
			</vbox>
			<hbox space-expand="false" space-fill="false">
				<text space-expand="false" space-fill="false">
					<label>"'$i18nPreview':"</label>
				</text>
				<text space-expand="true" space-fill="true">
					<label>"'$DebugSpacer'"</label>
				</text>
			</hbox>
			<hbox space-expand="false" space-fill="false">
				<button space-expand="false" space-fill="false"
					visible="'`funcTorF $(($FontButtonState >> 1 & 1))`'"
					sensitive="'`funcTorF $(($FontButtonState >> 1 & 1))`'">
					<variable>ftbSmall</variable>
					<input file stock="gtk-select-font"></input>
					<action>activate:ftbBig</action>
				</button>
				<entry name="Preview" secondary-icon-stock="gtk-clear"
					space-expand="true" space-fill="true">
					<variable>entPreview</variable>
					<input file>'$TEMP_DIR'/PreviewText</input>
					<action signal="secondary-icon-release">clear:entPreview</action>
				</entry>
				<button use-stock="true" label="gtk-quit" has-focus="true"
					space-expand="false" space-fill="false">
					<action>exit:Quit</action>
				</button>
			</hbox>
		</vbox>
	</vbox>
	<variable>winMain</variable>
</window>
' > $TEMP_DIR/winMain

## Export everything necessary.
export GTKDIALOG
export TARGET
export TITLE
export VERSION
export LOCAL_DATA_DIR
export PACKAGE_DATA_DIR
export WINDOW_TITLE
export DEBUG_CONTENT
export DEBUG_TRANSITS
export PATH
export TEMP_DIR
export GTK2_RC_FILES
export GTKDIALOG_PIXMAP_PATH=/usr/local/lib/X11/mini-icons:\
/usr/local/lib/X11/pixmaps:/usr/share/pixmaps:/usr/share/icons

## And off we go...
$GTKDIALOG --space-expand=true --space-fill=true --file=$TEMP_DIR/winMain

## Remove the temporary files.
rm $FontSymlink
rm -rf $TEMP_DIR
