#!

#-------------------------------------------------------------
# mdb2odb.sh
#
# (C) 2007 Junta de Extremadura
# Developed by
# Monolab Consulting ( http://www.monolabs.com/ )
# and Daniel Campos <daniel.campos@juntaextremadura.net>
#
# 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 1, 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., 675 Mass Ave, Cambridge, MA 02139, USA.
#-------------------------------------------------------------

#-------------------------------------------------------------
# Test if mdb file really exists
#-------------------------------------------------------------


if [ ! -e $1 ]
then
	zenity --error --text "No existe el archivo indicado"
	exit -1
fi

cd `dirname $1`


#--------------------------------------------------------------
# Paths used in the script
#--------------------------------------------------------------
templatedir=/usr/share/mdbtools/template
hsqldbdir=/usr/lib/openoffice/program/classes/hsqldb.jar
origdir=`pwd`
dbname=`basename $1`

#--------------------------------------------------------------
# Gets an ID for temporal files 
#--------------------------------------------------------------
dbkey=`uuidgen`
sqltoolrcdir=/tmp/"$dbkey".rc
echo "urlid temporal" > $sqltoolrcdir
echo "url jdbc:hsqldb:file:/tmp/"$dbkey"tempdb" >> $sqltoolrcdir
echo "username sa" >> $sqltoolrcdir
echo "password" >> $sqltoolrcdir
echo "" >> $sqltoolrcdir

#--------------------------------------------------------------
# Copy an empty ooo database template to tmp
#--------------------------------------------------------------
cp -rf "$templatedir" /tmp/"$dbkey"template
if [ ! $? -eq 0 ]
then
	zenity --error --text "Error copiando template a /tmp"
	exit -2	
fi
templatedir=/tmp/"$dbkey"template

#--------------------------------------------------------------
# Gets target ooo database name
#--------------------------------------------------------------
db2=`echo $dbname | sed -e 's/\.mdb/\.odb/g'`
db2="$origdir"/"$db2"

#--------------------------------------------------------------
# If target already exists asks if the user wants to
# overwrite it
#-------------------------------------------------------------- 
if [ -e $db2 ] 
then
	zenity --question  --text "La base de datos ODB ya existe. Pulse Aceptar para reemplazarla y hacer una copia, o Cancelar para abrirla" 
	ret=$?

	case $ret in 
		"0")
			cp $db2 $db2.backup
			if [ ! $? -eq 0 ]
			then
				zenity --error --text "Error copiando base. Compruebe sus permisos de escritura en la carpeta actual"
				exit -3
			fi
			;;
		"1")
			oobase "$db2" &
			exit 0
			;;
	esac
fi 

#-----------------------------------------------------------------
# Dumps mdb database in text ooo-compatible format
#-----------------------------------------------------------------
mdb2odb "$dbname" | tee /tmp/"$dbkey".sql | zenity --title "Exportando $dbname" --progress --pulsate --auto-close --text="exportando $dbname"


#-----------------------------------------------------------------
# Generates ooo database from text data
#-----------------------------------------------------------------
java -Dsqlfile.charset=UTF-8 -jar $hsqldbdir --rcfile "$sqltoolrcdir" temporal < /tmp/"$dbkey".sql 2>&1 | tee /tmp/"$dbkey"mdb2odb.log | zenity --title "Importando Datos" --progress --pulsate --auto-close --text="Importando Datos"  

echo "Empaquetandola para darle formato OO.org" 
	
rm -rf ${templatedir}/database/log	
rm -rf ${templatedir}/database/properties	
	

cp /tmp/"$dbkey"tempdb.log ${templatedir}/database/log
cp /tmp/"$dbkey"tempdb.properties ${templatedir}/database/properties

dboname=`basename "$1" .mdb`
dboname=$dboname'.odb'
cd ${templatedir}
zip -r "${origdir}/${dboname}" * 
chmod 0777 "${origdir}/$dboname" 

rm -rf $templatedir
rm -f /tmp/"$dbkey"tempdb.log
rm -f /tmp/"$dbkey"tempdb.properties
rm -f /tmp/"$dbkey".sql
rm -f /tmp/"$dbkey"mdb2odb.log
rm -f $sqltoolrcdir

oobase "${origdir}/${dboname}" 

