-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathchkbk
1983 lines (1722 loc) · 54 KB
/
chkbk
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
#!/bin/sh
# =============================================================================
# Script: chkbk
# By: SMK
# Date: 04/07/05
# Purpose: main menu for chkbk program
#
# License: GPL
# License file LICENSE.txt
# Email: [email protected]
#
# Copyright (C) 2005 Stephen M. Kaiser
#
# 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; if not, write to the Free Software
# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
#
# =============================================================================
#################### Begin Variables ########################
VERSION="1.55"
HELP="$HOME/.chkbk/HELP.txt"
LICENSE="$HOME/.chkbk/LICENSE.txt"
S_DEFAULT="$HOME/.chkbk/sav.db.bak"
C_DEFAULT="$HOME/.chkbk/chk.db.bak"
SAVDB="$HOME/.chkbk/sav.db"
CHKDB="$HOME/.chkbk/chk.db"
TMPDB="$HOME/.chkbk/new.db"
TMPDB2="$HOME/.chkbk/tmpdb2"
TMPDB3="$HOME/.chkbk/tmpdb3"
TMPDB4="$HOME/.chkbk/tmpdb4"
TMPFILE="$HOME/.chkbk/tmpfile"
NEWTMP="$HOME/.chkbk/newtmp"
SUBTMP="$HOME/.chkbk/subtmp"
OUTPUTF="$HOME/.chkbk/balance.txt"
loop=y
#################### End Variable Section ###################
#################### Begin Functions ########################
####
## Print Main menu to scren
##
####
mainmenu()
{
while test "$loop" = "y"
do
clear
tput cup 4 29 ; echo "Main Menu"
tput cup 5 23 ; echo "===================="
tput cup 6 25 ; echo "(S) Savings"
tput cup 7 25 ; echo "(C) Checking"
tput cup 8 25 ; echo "(B) Backup"
tput cup 9 25 ; echo "(R) Restore"
tput cup 12 25 ; echo "(Q) Quit"
tput cup 15 20 ; echo "Enter choice: "
tput cup 20 17 ; echo "(H) Help (A) About (L) License"
tput cup 21 14 ; echo "(C) Copyleft 2005 by Stephen M. Kaiser"
### Display Welcome to ChkBk message as an animation
tput cup 2 18 ; echo -n "W " ; sleep 0.01 ; echo -n "e " ; sleep 0.01 ; echo -n "l " ; sleep 0.01 ; echo -n "c " ; sleep 0.01 ; echo -n "o " ; sleep 0.01 ; echo -n "m " ; sleep 0.01 ; echo -n "e " ; sleep 0.01 ; echo -n " t " ; sleep 0.01 ; echo -n "o " ; sleep 0.01 ; echo -n " C " ; sleep 0.01 ; echo -n "h " ; sleep 0.01 ; echo -n "k " ; sleep 0.01 ; echo -n "B " ; sleep 0.01 ; echo -n "k "
tput cup 15 34 ; read choice
#### Begin case
case $choice in
[Ss]) DB="$SAVDB" ; printmenus ;; # change variable so it doesn't bother constants
[Cc]) DB="$CHKDB" ; printmenuc ;; # change variable so it doesn't bother constants
[Bb]) backupmenu ;;
[Rr]) restoremenu ;;
[Hh]) less "$HELP" ;;
[Aa]) tput cup 22 22 ; echo ChkBk Version "$VERSION" ; tput cup 23 19 ; echo "Press <ENTER> to continue" ; read prompt ;;
[Ll]) less "$LICENSE" ;;
[Qq]) clear ; exit ;;
*) invaliderror ;;
esac
#### End case
done
#### End while
}
####
## print main chkmenu
##
####
printmenuc()
{
while test "$loop" = "y"
do
clear
tput cup 4 29 ; echo "Checking"
tput cup 5 23 ; echo "===================="
tput cup 6 25 ; echo "(D) Deposit"
tput cup 7 25 ; echo "(W) Withdrawal"
tput cup 8 25 ; echo "(C) Check Balance"
tput cup 9 25 ; echo "(S) Search Database"
tput cup 10 25 ; echo "(X) Delete Entry"
tput cup 12 25 ; echo "(Q) Quit"
tput cup 15 20 ; echo "Enter choice: "
tput cup 15 34 ; read choice2
case $choice2 in
[Dd]) menu ;;
[Ww]) menu ;;
[Cc]) if test -e "$DB"
then
balmenu
else
neednewdb
fi ;;
[Ss]) if test -e "$DB"
then
searchmenu
else
neednewdb
fi ;;
[Xx]) if test -e "$DB"
then
searchmenu
else
neednewdb
fi ;;
[Hh]) less "$HELP" ;;
[Qq]) mainmenu ;;
*) invaliderror ;;
esac
done
}
####
## print main savings menu
##
####
printmenus()
{
while test "$loop" = "y"
do
clear
tput cup 4 29 ; echo "Savings"
tput cup 5 23 ; echo "===================="
tput cup 6 25 ; echo "(D) Deposit"
tput cup 7 25 ; echo "(W) Withdrawal"
tput cup 8 25 ; echo "(C) Check Balance"
tput cup 9 25 ; echo "(S) Search Database"
tput cup 10 25 ; echo "(X) Delete Entry"
tput cup 12 25 ; echo "(Q) Quit"
tput cup 15 20 ; echo "Enter choice: "
tput cup 15 34 ; read choice2
case $choice2 in
[Dd]) menu ;;
[Ww]) menu ;;
[Cc]) if test -e "$DB"
then
balmenu
else
neednewdb
fi ;;
[Ss]) if test -e "$DB"
then
searchmenu
else
neednewdb
fi ;;
[Xx]) if test -e "$DB"
then
searchmenu
else
neednewdb
fi ;;
[Hh]) less "$HELP" ;;
[Qq]) mainmenu ;;
*) invaliderror ;;
esac
done
}
####
## Print Backup menu
##
####
backupmenu()
{
while test "$loop" = "y"
do
clear
tput cup 4 29 ; echo "Backup?"
tput cup 5 23 ; echo "===================="
tput cup 6 25 ; echo "(S) Savings"
tput cup 7 25 ; echo "(C) Checking"
tput cup 12 25; echo "(Q) Quit"
tput cup 15 20; echo "Enter Choice: "
tput cup 15 34; read choice3
case $choice3 in
# change variables so they don't affect the constants
[Ss]) DB="$SAVDB" ; DB_DEF="$S_DEFAULT" ; pathb ;;
[Cc]) DB="$CHKDB" ; DB_DEF="$C_DEFAULT" ; pathb ;;
[Hh]) less "$HELP" ;;
[Qq]) mainmenu ;;
*) invaliderror ;;
esac
done
}
####
## Print restore menu
##
####
restoremenu()
{
while test "$loop" = "y"
do
clear
tput cup 4 29 ; echo "Restore?"
tput cup 5 23 ; echo "===================="
tput cup 6 25 ; echo "(S) Savings"
tput cup 7 25 ; echo "(C) Checking"
tput cup 12 25; echo "(Q) Quit"
tput cup 15 20; echo "Enter Choice: "
tput cup 15 34; read choice3
case $choice3 in
# change variables so they don't affect the constants
[Ss]) DB="$SAVDB" ; DB_DEF="$S_DEFAULT" ; pathr ;;
[Cc]) DB="$CHKDB" ; DB_DEF="$C_DEFAULT" ; pathr ;;
[Hh]) less "$HELP" ;;
[Qq]) mainmenu ;;
*) invaliderror ;;
esac
done
}
####
## print data entry menu
##
####
menu()
{
clear
menuloop="y"
while test "$menuloop" = "y"
do
tput cup 4 29 ; echo "Enter Info"
tput cup 5 23 ; echo "===================="
tput cup 6 25 ; echo "Check# : "
tput cup 7 25 ; echo "Date : (MMDDYY)"
tput cup 8 25 ; echo "Comment : "
tput cup 9 25 ; echo "Amount : "
tput cup 10 25 ; echo "Code : "
tput cup 11 25 ; echo "Fee : "
tput cup 6 35 ; read checknum
if test "$checknum" = "q"
then
case $choice in
[Ss]) printmenus ;;
[Cc]) printmenuc ;;
esac
elif test "$checknum" = "h"
then
less "$HELP" ; menu
else
if test "$checknum" = "" # allow blank check no.
then
clearerror ; date
else # verify that check has between 3 and 4 digits
case "$checknum" in
[0-9][0-9][0-9]) menuloop="n" ; checkchk ; clearerror ; date ;;
[0-9][0-9][0-9][0-9]) menuloop="n" ; checkchk ; clearerror ; date ;;
*) tput cup 6 35 ; echo " "
tput cup 20 25 ; echo "BAD check#. Check numbers can range"
tput cup 21 25 ; echo "from 000-9999" ;;
esac
fi
fi
done
}
####
## make sure of no duplicate check no. entries
##
####
checkchk()
{
if test -e "$DB"
then
while cut -d "^" -f 3-8 "$DB" | grep "^$checknum" > "$TMPFILE"
do
tput cup 20 25 ; echo "This number has already been assigned to: "
tput cup 22 1 ; tr '^' ' ' < "$TMPFILE"
echo ; echo "Press ENTER to continue... "
read prompt
menu
done
fi
}
####
## get date and verify format
##
####
date()
{
dateloop="y"
while test "$dateloop" = "y"
do
tput cup 7 35 ; read date
case "$date" in
[1][0-2][0][1-9][0-9][0-9]) dateloop="n" ; clearerror ; comment;;
[1][0-2][1-2][0-9][0-9][0-9]) dateloop="n" ; clearerror ; comment;;
[1][0-2][3][0-1][0-9][0-9]) dateloop="n" ; clearerror ; comment;;
[0][1-9][0][1-9][0-9][0-9]) dateloop="n" ; clearerror ; comment ;;
[0][1-9][1-2][0-9][0-9][0-9]) dateloop="n" ; clearerror ; comment ;;
[0][1-9][3][0-1][0-9][0-9]) dateloop="n" ; clearerror ; comment;;
[-]) while test "$date" = "-"
do
menu
done ;;
[Hh]) less "$HELP"
tput cup 7 35 ; echo " (MMDDYY) " ;;
[Qq]) case $choice in
[Ss]) printmenus ;;
[Cc]) printmenuc ;;
esac ;;
*) tput cup 7 35 ; echo " (MMDDYY) "
tput cup 21 25 ; echo "BAD date format"
;;
esac
done
}
####
## get comment field and disallow '^', '/', and '|' characters
##
####
comment()
{
commentloop="y"
while test "$commentloop" = "y"
do
tput cup 8 35 ; read comment
case "$comment" in
*['^']*) clearerror ; tput cup 20 25 ; echo "Sorry, but the ^ character is not allowed"
tput cup 21 25 ; echo "due to its use in the database"
tput cup 8 35 ; echo " " ;;
*['|']*) clearerror ; tput cup 20 25 ; echo -e "Sorry, but the | character is not allowed for security reasons\n"
tput cup 8 35 ; echo " " ;;
*['/']*) clearerror ; tput cup 20 25 ; echo -e "Sorry, but the / character is not allowed for security reasons\n"
tput cup 8 35 ; echo " " ;;
[-]) while test "$comment" = "-"
do
tput cup 8 35 ; echo " "
tput cup 7 35 ; echo " " ; clearerror ; date
done ;;
[Hh]) less "$HELP"
tput cup 8 35 ; echo " " ;;
[Qq]) case $choice in
[Ss]) printmenus ;;
[Cc]) printmenuc ;;
esac ;;
*) comsize="$(echo "$comment" | wc -m )"
if test "$comsize" -gt "33"
then
clearerror ; tput cup 20 25 ; echo -e "Sorry, but comments are limited to 32 characters...\n"
tput cup 8 35 ; echo " "
else
commentloop="n" ; clearerror ; amount
fi ;;
esac
done
}
####
## get amt of transaction and verify in dollar format
## allow up to $999999.99 dollars to be entered
####
amount()
{
amountloop="y"
while test "$amountloop" = "y"
do
tput cup 9 35 ; read amount
case "$amount" in
# allow user to enter amount without ending zeros and fill the zeros in
[0-9]) amountloop="n" ; type="a" ; addzeroz ; clearerror ; code ;;
[1-9][0-9]) amountloop="n" ; type="a" ; addzeroz ; clearerror ; code ;;
[1-9][0-9][0-9]) amountloop="n" ; type="a" ; addzeroz ; clearerror ; code ;;
[1-9][0-9][0-9][0-9]) amountloop="n" ; type="a" ; addzeroz ; clearerror ; code ;;
[1-9][0-9][0-9][0-9][0-9]) amountloop="n" ; type="a" ; addzeroz ; clearerror ; code ;;
[1-9][0-9][0-9][0-9][0-9][0-9]) amountloop="n" ; type="a" ; addzeroz ; clearerror ; code ;;
# amount entered with one decimal place- auto add one zero to the end
[0-9].[0-9]) amountloop="n" ; type="a1" ; addzeroz ; clearerror ; code ;;
[1-9][0-9].[0-9]) amountloop="n" ; type="a1" ; addzeroz ; clearerror ; code ;;
[1-9][0-9][0-9].[0-9]) amountloop="n" ; type="a1" ; addzeroz ; clearerror ; code ;;
[1-9][0-9][0-9][0-9].[0-9]) amountloop="n" ; type="a1" ; addzeroz ; clearerror ; code ;;
[1-9][0-9][0-9][0-9][0-9].[0-9]) amountloop="n" ; type="a1" ; addzeroz ; clearerror ; code ;;
[1-9][0-9][0-9][0-9][0-9][0-9].[0-9]) amountloop="n" ; type="a1" ; addzeroz ; clearerror ; code ;;
# amount entered with both decimal places
[0-9].[0-9][0-9]) amountloop="n" ; clearerror ; code ;;
[1-9][0-9].[0-9][0-9]) amountloop="n" ; clearerror ; code ;;
[1-9][0-9][0-9].[0-9][0-9]) amountloop="n" ; clearerror ; code ;;
[1-9][0-9][0-9][0-9].[0-9][0-9]) amountloop="n" ; clearerror ; code ;;
[1-9][0-9][0-9][0-9][0-9].[0-9][0-9]) amountloop="n" ; clearerror ; code ;;
[1-9][0-9][0-9][0-9][0-9][0-9].[0-9][0-9]) amountloop="n" ; clearerror ; code ;;
[-]) while test "$amount" = "-"
do
tput cup 9 35 ; echo " "
tput cup 8 35 ; echo " " ; clearerror ; comment
done ;;
[Hh]) less "$HELP"
tput cup 9 35 ; echo " " ;;
[Qq]) case $choice in
[Ss]) printmenus ;;
[Cc]) printmenuc ;;
esac ;;
*) tput cup 9 35 ; echo " "
tput cup 21 25 ; echo "BAD amount. Please enter a dollar amount" ;;
esac
done
}
####
## add zeros to amount or fee if only the first part of the number is entered
## This is intended to ease use by allowing the user to only have to input
## 11 instead of 11.00 for an $11.00 transaction
####
addzeroz()
{
if test "$type" = "a"
then
amount="$amount".00
tput cup 9 35 ; echo "$amount"
elif test "$type" = "a1"
then
amount="$amount"0
tput cup 9 35 ; echo "$amount"
elif test "$type" = "f"
then
fee="$fee".00
tput cup 11 35 ; echo "$fee"
elif test "$type" = "f1"
then
fee="$fee"0
tput cup 11 35 ; echo "$fee"
else
tput cup 21 25 ; echo "INVALID TYPE! TRY AGAIN..."
if test "$fee" = ""
then
clearerror ; amount
else
clearerror ; fee
fi
fi
}
####
## get any single char code from user for the transaction if one exists
##
####
code()
{
codeloop=y
while test "$codeloop" = "y"
do
tput cup 10 35 ; read code
if test "$code" = ""
then
clearerror ; fee
else
case "$code" in
[Qq]) case $choice in
[Ss]) printmenus ;;
[Cc]) printmenuc ;;
esac ;;
[0-9a-zA-Z]) codeloop="n" ; clearerror ; fee ;;
[-]) while test "$code" = "-"
do
tput cup 10 35 ; echo " "
tput cup 9 35 ; echo " " ; clearerror ; amount
done ;;
[Hh]) less "$HELP"
tput cup 10 35 ; echo " " ;;
*) tput cup 10 35 ; echo " "
tput cup 21 25 ; echo "BAD code. Please enter a single letter or number" ;;
esac
fi
done
}
####
## get fee amt of transaction if one exists
## allow up to $999.99 fee-seems a bit excessive,
## but who knows?
####
fee()
{
feeloop=y
while test "$feeloop" = "y"
do
tput cup 11 35 ; read fee
if test "$fee" = ""
then
clearerror ; printvalues
else
case "$fee" in
# allow user to enter amount without ending zeros and fill the zeros in
[0-9]) feeloop="n" ; type="f" ; addzeroz ; clearerror ; printvalues ;;
[1-9][0-9]) feeloop="n" ; type="f" ; addzeroz ; clearerror ; printvalues ;;
[1-9][0-9][0-9]) feeloop="n" ; type="f" ; addzeroz ; clearerror ; printvalues ;;
# allow user to enter amount with only one decimal point filled in
[0-9].[0-9]) feeloop="n" ; type="f1" ; addzeroz ; clearerror ; printvalues ;;
[1-9][0-9].[0-9]) feeloop="n" ; type="f1" ; addzeroz ; clearerror ; printvalues ;;
[1-9][0-9][0-9].[0-9]) feeloop="n" ; type="f1" ; addzeroz ; clearerror ; printvalues ;;
# fee amount entered with decimal places
[0-9].[0-9][0-9]) feeloop="n" ; clearerror ; printvalues ;;
[1-9][0-9].[0-9][0-9]) feeloop="n" ; clearerror ; printvalues;;
[1-9][0-9][0-9].[0-9][0-9]) feeloop="n" ; clearerror ; printvalues ;;
[-]) while test "$fee" = "-"
do
tput cup 11 35 ; echo " "
tput cup 10 35 ; echo " " ; clearerror ; code
done ;;
[Hh]) less "$HELP"
tput cup 11 35 ; echo " " ;;
[Qq]) case $choice in
[Ss]) printmenus ;;
[Cc]) printmenuc ;;
esac ;;
*) tput cup 11 35 ; echo " "
tput cup 21 25 ; echo "BAD fee. Please enter a dollar amount." ;;
esac
fi
done
}
####
## clear any error output from menu screen
##
####
clearerror()
{
tput cup 20 25 ; echo " "
tput cup 21 25 ; echo " "
}
####
## show the user the values he/she entered and ask
## if they are ok...then send to the appropriate
## calc function based on input in step 1 or
## start allover again
####
printvalues()
{
tput cup 4 29 ; echo "You Entered:"
tput cup 5 23 ; echo "===================="
tput cup 6 25 ; echo "Check# : "$checknum""
tput cup 7 25 ; echo "Date : "$date""
tput cup 8 25 ; echo "Comment : "$comment""
tput cup 9 25 ; echo "Amount : "$amount""
tput cup 10 25 ; echo "Code : "$code""
tput cup 11 25 ; echo "Fee : "$fee""
echo
echo -en "\nDo you wish to save these values (y/n)? "
read save
if test "$save" = "y"
then
case "$choice2" in
[Dd]) calcdeposit ;;
[Ww]) calcwithdrawal ;;
esac
else
echo -en "\n\nWould you like to start again (y/n)? "
read nloop
if test "$nloop" = "y"
then
menu
else
mainmenu
fi
fi
}
####
## calculate a deposit transaction
##
####
calcdeposit()
{
trans_type=d
if test "$fee" = ""
then fee="0.00" # this is just to make output prettier
fi
deposit="$amount"
withdrawal="" # blank withdrawal amount since we are depositing
# get total from previous transaction
oldtotal="$(tail -1 "$DB" 2> /dev/null | cut -d '^' -f 10)"
echo ""$oldtotal"^"$deposit"^" > "$TMPDB"
# calc total(without fee) of this new transaction
newtotal="$(tail -1 "$TMPDB" | cut -d '^' -f 1,2 | awk -F '^' '{ print $1+$2 }')"
echo ""$newtotal"^"$fee"" > "$TMPDB"
# calc total including fee
total="$(cut -d '^' -f 1,2 "$TMPDB" | awk -F '^' '{ print $1-$2 }')"
# show transaction with new balance to user
echo
echo "Previous Balance: $"$oldtotal" + Deposited $"$deposit" - Fee: $"$fee" = New Balance: $"$total""
# make db prettier for output later
if test "$code" = ""
then code="n/a"
fi
# get unique record number
chk_rec_num
# put records in date order
sort_by_date
# prompt to do again...duh?
echo -en "\n\nWould you like to make another deposit (y/n)?"
read dloop
if test "$dloop" = "y"
then
menu
else
mainmenu
fi
}
####
## calculate a withdrawal transaction
##
####
calcwithdrawal()
{
trans_type=w
if test "$fee" = ""
then fee="0.00" # this is just to make output prettier
fi
deposit="" # blank deposit amount since we are withdrawing
withdrawal="$amount"
# get total from previous transaction
oldtotal="$(tail -1 "$DB" 2> /dev/null | cut -d '^' -f 10)"
echo ""$oldtotal"^"$withdrawal"^" > "$TMPDB"
# calc total(without fee) of this new transaction
newtotal="$(tail -1 "$TMPDB" | cut -d '^' -f 1,2 | awk -F '^' '{ print $1-$2 }')"
echo ""$newtotal"^"$fee"" > "$TMPDB"
# calc total including fee
total="$(cut -d '^' -f 1,2 "$TMPDB" | awk -F '^' '{ print $1-$2 }')"
# show transaction with new balance to user
echo
echo "Previous Balance: $"$oldtotal" - Withdrawn: $"$withdrawal" - Fee: $"$fee" = New Balance: $"$total""
# make db prettier for output later
if test "$code" = ""
then code="n/a"
fi
# get unique record number
chk_rec_num
# make sure date gets put in chronological order
sort_by_date
# you know what this does, i hope...
echo -en "\n\nWould you like to make another withdrawal (y/n)?"
read wloop
if test "$wloop" = "y"
then
menu
else
mainmenu
fi
}
####
## Generate recoord # and make sure it will be unique
## beefore writing it to the database cuz this will
## really screw things up later if there are duplicate
## records and it will be a REAL pain in the ass! Plus, people
## will prolly get really pissed at the author of CHKBK
## if this happens...
##
####
chk_rec_num()
{
rec_loop="y"
while test "$rec_loop" = "y"
do
# generate record number by incrementing by one
# from the last line of existing db
# we also send complaints from tail if no
# db exists yet to /dev/null
i="$(tail -1 "$DB" 2> /dev/null | cut -d'^' -f 1)"
# j will be new record number, hopefully
let "j = i + 1"
# make sure that record isn't already existing
# if it is, keep increasing number by one until
# it be the onlyest record wit dat numbah
# same reason for /dev/null use as above
while cut -d'^' -f 1 "$DB" 2> /dev/null | grep "^$j" > "$TMPFILE"
do
let "j = j + 1"
done
# only allow 99999 transaction entries
if test "$j" -gt "99999"
then
echo ; echo
echo "Sorry, you have too many records :("
echo ; echo
echo "I will return you to the previous menu"
echo "from which you may delete records..."
echo
pressenter ; rec_loop=n
case $choice in
[Ss]) printmenus ;;
[Cc]) printmenuc ;;
esac
else
rec_loop=n
fi
done
}
####
## Inserts the record to be added to the database in chronological order
##
####
sort_by_date()
{
# loop control
i=2
# date for the last line of the current db
eof_date="$(tail -1 "$DB" 2> /dev/null | cut -d '^' -f 4)"
# date for the line above whatever line we were just on once we
# start the loop
next_date="$(tail -"$i" "$DB" 2> /dev/null | cut -d '^' -f 4 | head -1)"
# date of the first line in the current db
tof_date="$(head -1 "$DB" 2> /dev/null | cut -d '^' -f 4)"
# record number of the last line in the file
eof_rec="$(tail -1 "$DB" 2> /dev/null | cut -d '^' -f 1)"
# if tof_date is empty, there are no records currently existing
# in the database, so we just write the new data
if test "$tof_date" = ""
then
last_rec=y ; write_vals
# if new date is older than the first date in the db, we don't allow
# this since it's older than the initial entry.
elif test "$date" -lt "$tof_date"
then
echo
echo "Sorry, but you've entered a date that is older than"
echo "the date of the first entry in the database. You'll need"
echo "to enter this manually if that's what you really wish to do."
pressenter
case $choice in
[Ss]) printmenus ;;
[Cc]) printmenuc ;;
esac
# we see if new record is already younger than any other
# entries in the db...if it is we just need to append it to existing
# db and no further modifications are needed...yay!!!!
elif test "$date" -ge "$eof_date"
then
last_rec=y ; write_vals
# if eof date equals top of file date, that means we only have
# one date currently in the db...this could be do to only one record being
# in the db already or multiple entries with the same initial start date
elif test "$eof_date" = "$tof_date"
then
last_rec=y ; write_vals
# new record is some older date in history than the last entry in the
# database...this SEVERELY complicates things...
# keep going through the database from the bottom until we find somewhere
# to enter the new record by date order until we get to the first entry in
# the database
else
last_rec=n
# test to see if there are only two diff dates, in the current db
# since all the previous
# criteria for where to put the new record haven't been met yet,
# this means that there are at least 2 records with diff dates and the new
# record's date is the same as the first record in the db, so we
# have to modify our criteria for determining next_date by getting
# next_date to match eof_date so we can get into the loop below.
if test "$next_date" = "$tof_date" && test "$eof_date" != "$tof_date"
then
next_date="$eof_date"
fi
while test "$next_date" != "$tof_date"
do
# get next_date's current value
next_date="$(tail -"$i" "$DB" 2> /dev/null | cut -d '^' -f 4 | head -1)"
# see if new date equal to or greater than the next date in db
if test "$date" -ge "$next_date"
then
# we need to know what line we are at in the db
curr_rec="$(tail -"$i" "$DB" 2> /dev/null | cut -d '^' -f 1 | head -1)"
# Step 1: reverse db
sed -n '1!G;h;$p' "$DB" > "$TMPDB2"
# Step 2: cut all lines below current record giving us top portion
# only in reversed order
sed -n "/^"$curr_rec"^/,\$p" "$TMPDB2" > "$TMPDB3"
# Step 3: reverse again to put back in original order
sed -n '1!G;h;$p' "$TMPDB3" > "$NEWTMP"
# Step 4: send data to be recalculated because it's out of order
recalc
# Step 5: append new record values to existing db in their
# respective order datewise
write_vals
# step 6: make a loop to keep getting the rest of the lines and writing
# them to the end of the file
while test "$curr_rec" != "$eof_rec"
do
# get line after current line...this starts to form what will
# soon be the bottom portion of the db
sed -n "/^"$curr_rec"^/{n;p;}" "$DB" > "$TMPDB2"
# get record number of this line so that we can keep moving to
# the next record in the file each time through this loop
curr_rec="$(cut -d '^' -f 1 "$TMPDB2")"
# get old total before all this mess happened
tmp_oldtotal="$(cut -d "^" -f 2 "$TMPDB2")"
# get the total after the transaction occured...
# this is the number that is the actual total for the
# line and is equal tmp_oldtotal of the line that follows it...got it
tmp_total="$(cut -d "^" -f 10 "$TMPDB2")"
# see what type of transaction we had then write important stuff to temp
if test "$trans_type" = "w"
then
sub_amt="$withdrawal"
else
sub_amt="$deposit"
fi
echo ""$sub_amt"^"$tmp_oldtotal"^"$tmp_total"^"$fee"" > "$TMPFILE"
# test if we had a withdrawal or deposit and act accordingly
if test "$trans_type" = "w"
then
# this goes in field 2 of the updated db
new_oldtotal="$(cut -d "^" -f 2,1,4 "$TMPFILE" | awk -F '^' '{ print $2 - $1 - $3 }')"
# this goes in field 10 of the updated db
new_total="$(cut -d '^' -f 3,1,4 "$TMPFILE" | awk -F '^' '{ print $2 - $1 - $3 }')"
else
# this goes in field 2 of the updated db
new_oldtotal="$(cut -d "^" -f 2,1,4 "$TMPFILE" | awk -F '^' '{ print $2 + $1 - $3 }')"
# this goes in field 10 of the updated db
new_total="$(cut -d '^' -f 3,1,4 "$TMPFILE" | awk -F '^' '{ print $2 + $1 - $3 }')"
fi
# set new field amounts to be rewritten for updated db
field1="$curr_rec"
field2="$new_oldtotal"
field3="$(cut -d '^' -f 3 "$TMPDB2")"
field4="$(cut -d '^' -f 4 "$TMPDB2")"
field5="$(cut -d '^' -f 5 "$TMPDB2")"
field6="$(cut -d '^' -f 6 "$TMPDB2")"
field7="$(cut -d '^' -f 7 "$TMPDB2")"
field8="$(cut -d '^' -f 8 "$TMPDB2")"
field9="$(cut -d '^' -f 9 "$TMPDB2")"
field10="$new_total"
# actually rewrite the data to temp db...and continue to add
# to the file each following line...this is forming the bottom portion of the database
echo ""$field1"^"$field2"^"$field3"^"$field4"^"$field5"^"$field6"^"$field7"^"$field8"^"$field9"^"$field10"" >> "$NEWTMP"
done
##### end of while statement
# get out of loop since we have done what we needed to do
break
# increment $i so we can move to next line and check everything all over again
else
let "i = i + 1"
fi
done
# let user know we are done recalculating and writing data
echo
echo "Done!"
# temp db is now updated version of db, so we overwrite old version with it
cp -f "$NEWTMP" "$DB"
fi
}
########
## re-calculate current data because it's out of date order
##
####
recalc()
{
# it could take a few seconds for sed and awk to do their
# thing depending on how far back we had to go to recalculate things.
# so we just print a little message saying that we are working
# and not locked up
echo
echo "Re-calculating transactions. This could take a little while..."
# set value based on what type of transaction
if test "$trans_type" = "w"
then
sub_amt="$withdrawal"
else
sub_amt="$deposit"
fi
# get a new oldtotal value since it was originally based on being
# taken from the end of the original db, here we get it
# from the next line above it in date order
oldtotal="$(tail -1 "$NEWTMP" 2> /dev/null | cut -d '^' -f 10)"
echo ""$oldtotal"^"$sub_amt"^" > "$TMPDB"
if test "$trans_type" = "w"
then
# calc total(without fee) of this new transaction
newtotal="$(tail -1 "$TMPDB" | cut -d '^' -f 1,2 | awk -F '^' '{ print $1-$2 }')"
echo ""$newtotal"^"$fee"" > "$TMPDB"
# calc total including fee
total="$(cut -d '^' -f 1,2 "$TMPDB" | awk -F '^' '{ print $1-$2 }')"
else