#!/bin/sh
#
#   Copyright (C) 2006-2007 Jaroslaw Staniek <js@iidea.pl>
#
#   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; see the file COPYING.  If not, write to
#   the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
#   Boston, MA 02111-1307, USA.
#

basedir=`dirname "$0"`

function setup_messages {
	lang=`grep Language= ~/.kde/share/config/kdeglobals | head -n 1 | \
		sed -e 's/Language=\(.*\):.*/\1/'`
	if [ -z "$lang" ] ; then lang="en" ; fi
	
	IFS=:
	for dir in `kde-config --expandvars --path locale` ; do
		transl_file="$dir"$lang"/LC_MESSAGES/kexi_delete_column_gui_transl_$lang.sh";
		if [ -f "$transl_file" ] ; then
			source "$transl_file"
			break
		else
			transl_file=;
		fi
	done
	IFS=" "
	if [ -z "$transl_file" ] ; then
		transl_file="$basedir/kexi_delete_column_gui_transl_$lang.sh";
		if [ -f "$transl_file" ] ; then	source "$transl_file"; else transl_file=; fi
	fi
echo 	$transl_file
	if  [ -z "$transl_file" ] ; then
	# default: english messages:
		msg_filters="*.kexi|Kexi Project stored in a file
*.*|All files"
		msg_select_db_file="Select database file"
		msg_enter_table_name="Table name (not caption):"
		msg_enter_column_name="Column name (not caption):"
	fi
} # /setup_messages

setup_messages

ksqlite="ksqlite -noheader"

database_name=`kdialog --title "$msg_select_db_file" --getopenfilename . "$msg_filters"` || exit 1

table_name=`kdialog --inputbox "$msg_enter_table_name"` || exit 1

# show list of columns and ask for one

msg=`$ksqlite "$database_name" "SELECT f_name FROM kexi__objects o, kexi__fields f WHERE o.o_id=f.t_id AND o.o_name='$table_name' AND o_type=1 ORDER BY f_order;" || exit 1`

command_file=`mktemp "$database_name"XXXXXXXX`".sh"
echo -n "kdialog --radiolist \"$msg_enter_column_name\"" > $command_file
echo $msg | while read f ; do
	echo -n " $f $f off" >> $command_file
done

column_name=`source $command_file || exit 1`
rm -f $command_file

# call the command line tool

msg=`sh kexi_delete_column "$database_name" "$table_name" "$column_name" 2>&1`

[ -z "$msg" ] && exit 0

kdialog --error "$msg"
exit 1
