#!/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

#
# fnInput () is a function that contains the guts of the program
#
fnInput () { 

# get the date and put it into the output file
  dialog --inputbox "Zadej datum ve tvaru YYYYMMDD" 0 0 2>> $outfile ;
  echo -n " " >> $outfile ;

# get the description and put it into the output file
  dialog --inputbox "Zadej popis operace etnho denku" 0 0 2>> $outfile ;
  echo >> $outfile ; 

# keep asking for the account number to debit until user gets it right
  while `echo -n` 
  do {

# ask for the account number to debit
    dialog --inputbox "Zadej et M DTI" 0 0 2> /tmp/acctnum ;

# check if account number was given in the correct form
    ./acct_query `cat /tmp/acctnum` > /dev/null ;
    retval=$?
    if [ $retval -eq 1 ]
    then 
      dialog --msgbox "Zadej et ve tvaru XXX YY\n(Kdy nen analytika pak YY == \"--\")" 0 0 ;
      continue ;
    fi ;
    if [ $retval -eq 2 ]
    then
      dialog --msgbox "Takov et nen v etn osnov!" 0 0 ;
      continue ;
    fi;
    break ;

# if the number is not right, print a nasty error message
    dialog --msgbox "Takov et nen v etn osnov!" 0 0 ;
  }
  done ;

# if we've escaped from the above while loop, then the user must have entered
# a correct account number, which is now in the file /tmp/acctnum
  cat /tmp/acctnum >> $outfile ;

# now get the amount and put it in the output file, along with the proper 0
# for processing by mdebs
  buff="./acct_query `cat /tmp/acctnum`" ;
  dialog --inputbox "Zadej stku M DTI\n`cat /tmp/acctnum` `$buff`"  0 0 2> /tmp/amount ;
  echo " $(./times100 `cat /tmp/amount`) 0" >> $outfile ;

# now repeat the validation process for the account to credit
  while `echo -n` 
  do {
    dialog --inputbox "Zadej et DAL" 0 0 2> /tmp/acctnum ;

# check if account number was given in the correct form
    ./acct_query `cat /tmp/acctnum` > /dev/null ;
    retval=$?
    if [ $retval -eq 1 ]
    then 
      dialog --msgbox "Zadej et ve tvaru XXX YY\n(Kdy nen analytika pak YY == \"--\")" 0 0 ;
      continue ;
    fi ;
    if [ $retval -eq 2 ]
    then
      dialog --msgbox "Takov et nen v etn osnov!" 0 0 ;
      continue ;
    fi ;
    break ;
  }
  done ;
  cat /tmp/acctnum >> $outfile ;

  buff="./acct_query `cat /tmp/acctnum`" ;
  dialog --inputbox "Zadej stku DAL\n`cat /tmp/acctnum` `$buff`" 0 0 2> /tmp/amount ;
  echo " 0 $(./times100 `cat /tmp/amount`)" >> $outfile ;
  echo >> $outfile ;
  dialog --yesno "Chce jet?" 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

