#!/bin/sh
#
# mdebs/inputop
#
# This is a hacked-together shell script that uses 'dialog' to provide
# for user-friendlier input of journal entries.
#
# Author: Nathan L. Cutler, March 28, 1997
    
declare MDEBSDIR="/home/ncutler/work/mdebs"

true () {

    return 0 ;
}

errAcctFmt () {

    #
    # return value of 1 from acct_query means form was not XXX YY
    #
    dialog --msgbox "Zadej ucet ve tvaru XXX YY\n(Kdyz neni analytika pak YY == \"--\")" 0 0 ;
    if [ $? -eq 1 ]
    then
	exit 1 ;
    fi ;
    return 0 ;
}

errAcctExist () {

    #
    # return value of 2 from acct_query means nonexistent account
    #
    dialog --msgbox "Takovy ucet neni v ucetni osnove!" 0 0 ;
    if [ $? -eq 1 ]
    then
	exit 1 ;
    fi ;
    return 0 ;
}
    
#
# fnInput () is a function that contains the guts of the program
#
fnInput () { 
    
    #
    # keep asking for date until valid date entered or user hits cancel
    #
    while true
    do {
	dialog --inputbox "Zadej datum ve tvaru YYYYMMDD" 0 0 2> /tmp/datum ;
	if [ $? -eq 1 ]
	then
	    return 1 ;
	fi ;

	#
	# check if date is valid
	#
	$MDEBSDIR/dbinit_validate `cat /tmp/datum`;
	if [ $? -eq 1 ]
	then
	    dialog --msgbox "Invalid date -- try again" 0 0 ;
	    if [ $? -eq 1 ]
	    then
		return 1 ;
	    fi ;
	    continue ;
	fi;
	break ;
    }
    done ;

    #
    # date is valid ; put it in output file
    #
    echo -n "`cat /tmp/datum` " >> $outfile ;
    
    #
    # get the description and put it into the output file
    #
    dialog --inputbox "Zadej popis operace ucetniho deniku" 0 0 2>> $outfile ;
    if [ $? -eq 1 ]
    then
	return 1 ;
    fi ;
    echo >> $outfile ; 
    
    #
    # keep asking for account numbers until user gets them right
    # or hits 'cancel'
    #
    while true
    do {
    
	#
	# ask for the account number to debit
	#
	dialog --inputbox "Zadej ucet MA DATI" 0 0 2> /tmp/acctnum_md ;
	if [ $? -eq 1 ]
	then
	    return 1 ;
	fi ;
	
	#
	# check if account number was given in the correct form
	#
	$MDEBSDIR/acct_query `cat /tmp/acctnum_md` > /dev/null ;
	retval=$?
	if [ $retval -eq 1 ]
	then 
	    errAcctFmt ;
	    continue ;
	fi ;
	if [ $retval -eq 2 ]
	then
	    errAcctExist ;
	    continue ;
	fi;
	break ;
    }
    done ;

    #
    # now repeat the process for the account to credit
    #
    while true
    do {
	dialog --inputbox "Zadej ucet DAL" 0 0 2> /tmp/acctnum_d ;
	if [ $? -eq 1 ]
	then
	    return 1 ;
	fi ;

	#
	# check if account number was given in the correct form
	#
	$MDEBSDIR/acct_query `cat /tmp/acctnum_d` > /dev/null ;
	retval=$?
	if [ $retval -eq 1 ]
	then 
	    errAcctFmt ;
	    continue ;
	fi ;
	if [ $retval -eq 2 ]
	then
	    errAcctExist ;
	    continue ;
	fi;
	break ;
    }
    done ;
   
    #
    # now get the amount 
    #
    buff1="$MDEBSDIR/acct_query `cat /tmp/acctnum_md`" ;
    buff2="$MDEBSDIR/acct_query `cat /tmp/acctnum_d`" ;
    dialog --inputbox "Zadej castku\nMA DATI: `$buff1`\n    DAL: `$buff2`"  0 0 2> /tmp/amount ;
    if [ $? -eq 1 ]
    then
	return 1 ;
    fi ;

    #
    # now fill in the lines in the output file in the correct format
    #
    echo "`cat /tmp/acctnum_md` $($MDEBSDIR/times100 `cat /tmp/amount`) 0" >> $outfile ;
    echo "`cat /tmp/acctnum_d` 0 $($MDEBSDIR/times100 `cat /tmp/amount`)" >> $outfile ;
    echo >> $outfile ;

    dialog --yesno "Chces jeste?" 0 0 ; 
}

#
# This is where execution starts
#
if [ $# -lt 1 ]
then
  echo "Usage: inputop [output_file_name]"
  exit 0
fi
declare outfile=$1
if [ -e $outfile ]
then
  dialog --msgbox "Warning: $outfile exists.\nAppending, but this may lead to parsing problems." 0 0
else
  touch $outfile
fi

while fnInput 
do 
echo -n 
done

