#! /bin/sh
###############################################################
# parse the command line, and call the appropriate executable #
#                                                             #
###############################################################

function helpq () {
	echo "Syntax: $0 [options]"
	echo " General Options:"
	echo "  [-f|--home      <QHACC_HOME>]          set QHACC_HOME"
	echo "  [-w|--warranty]                        print warranty information"
	echo "  [--gui]                                force GUI initialization"
	echo "  [--cli]                                force CLI initialization"
	echo "  [--version]                            print version information"
	echo 
	echo " Command-Line Options:"
	echo "  [--debug        <Level>]               set debugging output"
	echo "  [--archive      <Date String|Account>:<Target Location>]"
	echo "                                         archive transactions"
	echo "  [--restore      <Archive Location>]    restore transactions"
	echo "  [--report       <Report Plugin>]       reports"
	echo "  [--export       <Directory>]           export data to directory"
	echo "  [--import       <Directory>]           import data from directory"
	echo "  [--create       <QHACC_HOME>]          make a QHACC_HOME creation script"
	echo "  [--verifydb]                           verify database integrity"
	echo "  [--fixdb]                              fix database integrity"
	echo "  [--plugins]                            list available plugins"
	echo 
	echo " GUI Options:"
	echo "  [--debug        <Level>]               set debugging output"
	echo "  [-l             <Language Directory>]  set QHacc Locale Directory"
	exit 1
}
prefix=/usr
exec_prefix=${prefix}
PATH=${exec_prefix}/bin:$PATH


# You can change QHACC_ROOT and QHACC_HOME to fit your needs
QHACC_ROOT=${QHACC_ROOT:="${exec_prefix}/lib"}
QHACC_HOME=${QHACC_HOME:="${HOME}/.qhacc"}
QHACC_LANGDIR=${QHACC_LANGDIR:="${prefix}/share"}

export LD_LIBRARY_PATH=$QHACC_ROOT:$LD_LIBRARY_PATH

typeset USEGUI="YES"
typeset OPTIONS=""
typeset LANGFIRST="YES"

while [ $# -gt 0 ]
do
  case $1 in
	"--gui")
	  USEGUI="YES"
		shift
	;;

	"--cli")
	  USEGUI="NO"
		shift
	;;

	"-f" | "--home")
	  shift
		if [ $# -lt 1 ] ; then helpq; fi
		QHACC_HOME="$1"
		shift 
	;;

	"--accounts")
		OPTIONS="$OPTIONS --report ACCT:"
	  shift
		USEGUI="NO"
	;;

	"--plugins")
		OPTIONS="$OPTIONS --plugins"
		LANGFIRST="NO"
	  shift
		USEGUI="NO"
	;;

	"--mbudget")
		OPTIONS="$OPTIONS --report MBUDGET:"
	  shift
		USEGUI="NO"
	;;
	
	"--debug")
	  shift
		if [ $# -lt 1 ] ; then helpq; fi
		OPTIONS="$OPTIONS --debug $1"
		shift
	;;

	"--reconcile")
	  shift
		if [ $# -lt 1 ] ; then helpq; fi
		OPTIONS="$OPTIONS --reconcile $1"
		shift
		USEGUI="NO"
		LANGFIRST="NO"
	;;
	
	"--preport")
	  shift
		if [ $# -lt 1 ] ; then helpq; fi
	  OPTIONS="$OPTIONS --report PAYEE:$1"
		shift
		USEGUI="NO"
	;;

	"--breport")
	  shift
		if [ $# -lt 1 ] ; then helpq; fi
		OPTIONS="$OPTIONS --report BALS:$1"
		shift
		USEGUI="NO"
	;;

	"--dreport")
	  shift
		if [ $# -lt 1 ] ; then helpq; fi
		OPTIONS="$OPTIONS --report DELTAS:$1"
		shift
		USEGUI="NO"
	;;

	"--areport")
	  shift
		if [ $# -lt 1 ] ; then helpq; fi
		OPTIONS="$OPTIONS --report AVES:$1"
		shift
		USEGUI="NO"
	;;

	"--lreport")
	  shift
		if [ $# -lt 1 ] ; then helpq; fi
		OPTIONS="$OPTIONS --report PFLS:$1"
		shift
		USEGUI="NO"
	;;

	"--report")
	  shift
		if [ $# -lt 1 ] ; then helpq; fi
		OPTIONS="$OPTIONS --report $1"
		shift
		USEGUI="NO"
	;;

	"--budget")
	  shift
		if [ $# -lt 1 ] ; then helpq; fi
		OPTIONS="$OPTIONS --report BUDGET:$1"
		shift
		USEGUI="NO"
	;;

	"--transactions")
	  shift
		if [ $# -lt 1 ] ; then helpq; fi
		OPTIONS="$OPTIONS --report TRANS:$1"
		shift
		USEGUI="NO"
	;;

	"--journal")
	  shift
		if [ $# -lt 1 ] ; then helpq; fi
		OPTIONS="$OPTIONS --report JOURNAL:$1"
		shift
		USEGUI="NO"
	;;

	"--export")
	  shift
		if [ $# -lt 1 ] ; then helpq; fi
		OPTIONS="$OPTIONS --export $1"
		shift
		USEGUI="NO"
	;;

	"--import")
	  shift
		if [ $# -lt 1 ] ; then helpq; fi
		OPTIONS="$OPTIONS --import $1"
		shift
		USEGUI="NO"
	;;

	"--archive")
	  shift
		if [ $# -lt 1 ] ; then helpq; fi
		OPTIONS="$OPTIONS --export ARC:$1"
		shift
		USEGUI="NO"
	;;

	"--restore")
	  shift
		if [ $# -lt 1 ] ; then helpq; fi
		OPTIONS="$OPTIONS --import ARC:$1"
		shift
		USEGUI="NO"
	;;

	"--create")
	  shift
		if [ $# -lt 1 ] ; then helpq; fi
		OPTIONS="$OPTIONS --create $1"
		shift
		USEGUI="NO"
	;;

	"--verifydb")
	  shift
		OPTIONS="$OPTIONS --verifydb"
		USEGUI="NO"
	;;

	"--fixdb")
	  shift
		OPTIONS="$OPTIONS --fixdb"
		USEGUI="NO"
	;;

	"-l")
	  shift
		if [ $# -lt 1 ] ; then helpq; fi
		QHACC_LANGDIR="$1"
		USEGUI="YES"
		shift
	;;

	"-w" | "--warranty")
	  cat <<EOF
                                NO WARRANTY
BECAUSE THE PROGRAM IS LICENSED FREE OF CHARGE, THERE IS NO WARRANTY
FOR THE PROGRAM, TO THE EXTENT PERMITTED BY APPLICABLE LAW.  EXCEPT WHEN
OTHERWISE STATED IN WRITING THE COPYRIGHT HOLDERS AND/OR OTHER PARTIES
PROVIDE THE PROGRAM "AS IS" WITHOUT WARRANTY OF ANY KIND, EITHER EXPRESSED
OR IMPLIED, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.  THE ENTIRE RISK AS
TO THE QUALITY AND PERFORMANCE OF THE PROGRAM IS WITH YOU.  SHOULD THE
PROGRAM PROVE DEFECTIVE, YOU ASSUME THE COST OF ALL NECESSARY SERVICING,
REPAIR OR CORRECTION.
    IN NO EVENT UNLESS REQUIRED BY APPLICABLE LAW OR AGREED TO IN WRITING
WILL ANY COPYRIGHT HOLDER, OR ANY OTHER PARTY WHO MAY MODIFY AND/OR
REDISTRIBUTE THE PROGRAM AS PERMITTED ABOVE, BE LIABLE TO YOU FOR DAMAGES,
INCLUDING ANY GENERAL, SPECIAL, INCIDENTAL OR CONSEQUENTIAL DAMAGES ARISING
OUT OF THE USE OR INABILITY TO USE THE PROGRAM (INCLUDING BUT NOT LIMITED
TO LOSS OF DATA OR DATA BEING RENDERED INACCURATE OR LOSSES SUSTAINED BY
YOU OR THIRD PARTIES OR A FAILURE OF THE PROGRAM TO OPERATE WITH ANY OTHER
PROGRAMS), EVEN IF SUCH HOLDER OR OTHER PARTY HAS BEEN ADVISED OF THE
POSSIBILITY OF SUCH DAMAGES.
EOF
	  exit 0
	;;

	"--version")
		shift
		cat <<EOF
QHacc version 3.5 Copyright (C) 2005 Ryan Bobko <ryan@ostrich-emulators.com>
QHacc comes with ABSOLUTELY NO WARRANTY; for details type \`qhacc -w'.
This is free software, and you are welcome to redistribute it under
certain conditions; Please see the GNU Public License for details.
EOF
	  exit 0
	;;

	*)
		helpq
	;;
	esac
done

if [ "x$USEGUI" = "xYES" ]
then
  QHACC_ROOT=$QHACC_ROOT QHACC_HOME=$QHACC_HOME qhacc-gui -l $QHACC_LANGDIR $OPTIONS
else
  if [ "X$LANGFIRST" = "xYES" ]
  then 
    QHACC_ROOT=$QHACC_ROOT QHACC_HOME=$QHACC_HOME qhacc-cli -l $QHACC_LANGDIR $OPTIONS
  else
    QHACC_ROOT=$QHACC_ROOT QHACC_HOME=$QHACC_HOME qhacc-cli $OPTIONS -l $QHACC_LANGDIR
  fi
fi
