#!/bin/bash
#  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 2 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 Street, Fifth Floor, Boston, MA 02110-1301, USA.
#

# Note: this script overwrites existing versions of the data being exported.

set -e
export XML_CATALOG_FILES="/opt/holly/pilot-qof/share/xml/datafreedom/xsl/catalog.xml"

ZENITY=0
if [ -e /usr/bin/zenity ]; then
	ZENITY=1
fi
# even if zenity is installed, skip it if running via SSH
if [ "$DISPLAY" == "" ] || [ "$SSH_CONNECTION" != "" ]; then
	ZENITY=0
fi

# Script is installed under /usr/share so need to check if
# pilot-qof is installed as the datafreedom xsl package does 
# not have to depend on pilot-qof itself.
# Individual stylesheets can be used without pilot-qof, see the
# other scripts in this package.
if [ ! -e /usr/bin/pilot-qof ]; then
	MSG="$0 is intended to export pilot-qof data to various supported"
	MSG="$MSG applications. Unlike the datafreedom stylesheets which can work with"
	MSG="$MSG just the QSF XML, this example script uses the 'pilot-qof' utility"
	MSG="$MSG to update and query the XML. Please install 'pilot-qof', to use this script."
	if [ $ZENITY == 1 ]; then
		zenity --error --text="$MSG"
	else
		echo $MSG | fold -s
	fi
	exit
fi

# get current year
YEAR=`date +%Y`

DATADIR=$PWD
HOME=$HOME

# check for a usable $HOME variable
if [ ! -d $HOME ]; then
	MSG="Cannot locate your home directory, $HOME does not exist."
	MSG="$MSG Please check the setting of the \$HOME environment variable."
	if [ $ZENITY == 1 ]; then
		zenity --error --text="$MSG"
	else
		echo $MSG | fold -s
	fi
	exit
fi

if [ ! -f offline.xml ]; then
	MSG="$DATADIR/offline.xml not found - If this is the correct directory,"
	MSG="$MSG please connect the Palm to create the file from a pilot-qof HotSync."
	if [ $ZENITY == 1 ]; then
		MSG="$MSG Click Cancel to abort."
		zenity --warning --text="$MSG"
		if [ $? eq 0 ]; then
			pilot-qof -a -w offline.xml
		fi
	else
		echo $MSG | fold -s
		MSG="Do you want to continue? y/N : "
		echo -n $MSG
		read answer
		if [ "$answer" != "Y" ] && [ "$answer" != "y" ]; then
			exit
		else 
			pilot-qof -a -w offline.xml
		fi
	fi
fi

# split the main file into database-specific files
if [ offline.xml -nt datebook.xml ]; then
	pilot-qof -x offline.xml -d pilot_datebook -w datebook.xml
	pilot-qof -x offline.xml -d pilot_expenses -w expenses.xml
	pilot-qof -x offline.xml -d pilot_todo -w todo.xml
	pilot-qof -x offline.xml -d pilot_address -w address.xml
fi

# for calcurse: text-based calendar and todo manager
if [ -e /usr/bin/calcurse ]; then
	pilot-qof -x datebook.xml | xsltproc --nonet -o $HOME/.calcurse/apts \
	 http://www.data-freedom.org/xsl/pilot-qof-calcurse-apts.xsl -
	pilot-qof -x todo.xml | xsltproc --nonet -o $HOME/.calcurse/todo \
	 http://www.data-freedom.org/xsl/pilot-qof-calcurse-todo.xsl -
fi

# for dates: a calendar optimised for embedded devices
if [ -d $HOME/.evolution/calendar/local/system ]; then
	pilot-qof -x datebook.xml | xsltproc --nonet \
	 -o $HOME/.evolution/calendar/local/system/calendar.ics \
	 http://www.data-freedom.org/xsl/pilot-qof-icalendar.xsl -
fi

# for calendar applications like gpe-calendar.
# the ics file needs to be imported by the user, 
# direct import is not supported, yet.
if [ ! -d ./ics ]; then
	mkdir ./ics
fi
pilot-qof -x datebook.xml | xsltproc --nonet -o ics/calendar.ics \
 http://www.data-freedom.org/xsl/pilot-qof-icalendar.xsl -

# for dlume: Simple and easy to use addressbook (GTK+)
if [ -e /usr/bin/dlume ]; then
	pilot-qof -x address.xml | xsltproc --nonet -o $HOME/.dlume/dlume-data.xml \
	 http://www.data-freedom.org/xsl/pilot-qof-address-dlume.xsl -
fi

# for gpe-expenses: Simple expense records for GPE
# (limits the expenses to the current year)
# the sqlite file can be rsynced to the embedded device.
if [ -e /usr/bin/gpe-expenses ]; then
	pilot-qof -t $YEAR -x expenses.xml | \
	 xsltproc --nonet -o /tmp/gpe-expenses.xml \
	 http://www.data-freedom.org/xsl/pilot-qof-gpe-expenses.xsl -
	gpe-expenses -i /tmp/gpe-expenses.xml -w sqlite:$HOME/.gpe/expenses
	rm /tmp/gpe-expenses.xml
fi

# for applications like gtodo.
# the vtodo file needs to be imported by the user, 
# direct import is not supported, yet.
if [ ! -d ./todo ]; then
	mkdir ./todo
fi
pilot-qof -x todo.xml | xsltproc --nonet -o todo/vtodo \
 http://www.data-freedom.org/xsl/pilot-qof-todo-vtodo.xsl -

# individual, brief, vcard files
if [ ! -d ./vcard ]; then
	mkdir .vcard
fi
rm -f vcard/*.vcf
pilot-qof -x address.xml | xsltproc --nonet -o vcard/ \
 http://www.data-freedom.org/xsl/pilot-qof-address-vcard1.xsl -

# individual, verbose, vcard files
if [ ! -d ./vcard2 ]; then
	mkdir .vcard2
fi
rm -f vcard2/*.vcf
pilot-qof -x address.xml | xsltproc --nonet -o vcard2/ \
 http://www.data-freedom.org/xsl/pilot-qof-address-vcard2.xsl -

# for applications like contacts, gpe-contacts ...
# direct import is not supported, yet.
pilot-qof -x address.xml | xsltproc --nonet -o collection.vcf \
 http://www.data-freedom.org/xsl/pilot-qof-address-vcard3.xsl -
