#!/usr/bin/env sh if [ -f $HOME/git/knd-ddbank/ddbank_config ]; then { . $HOME/git/knd-ddbank/ddbank_config } else { echo "Configuration file not present." echo "Refer to the Git README for setup" echo "" echo "https://git.skyfall.tech/theissenm/knd-ddbank/src/branch/master" echo "" exit 1 } fi db_conn="psql -h $dbhost -p $dbport -U $dbuser -d $datab -c" # Functions new_account() { clear read -p "Enter the name of your account: " user1 echo "" echo "-----------------------------------" echo "Your account name will be $user1." echo "-----------------------------------" echo "" read -n1 -p "Is that correct? [y/N] " yn case $yn in # If user selects yes. Insert their username and a timestamp into the database. [Yy] ) echo "" $db_conn "insert into account (username,timestamp,active) values ('$user1','now','t');" # Sets the user's id as the variable. user1_id=$($db_conn "select id from account where username='$user1';" | awk 'FNR == 3 {print}') # Gives the user the starting balance. $db_conn "insert into balance (id,balance) values ('$user1_id',$bal);" echo "" echo "-----------------------------------" echo "Account name: $user1" echo "Account ID: $user1_id" echo "-----------------------------------" echo "" # Transactioning $db_conn "insert into transaction (id,username,amount,timestamp) values ('$user1_id','$user1','+$bal','now');" ;; # If the user selects anything other than [Yy] * ) echo "Aborted" ;; esac } list_user() { echo "" $db_conn "select id,username,timestamp from account where active ='t';" echo "" } list_balance() { # Bal Menu clear while true do echo "" echo "1) List all users balances." echo "2) Exact User Search" echo "3) User Search" echo "" echo "q) Go back." echo "" read -n1 -p "*Balance Menu: Select from the above: " ii echo "" case $ii in 1 ) clear $db_conn "select id,username,balance from account join balance using (id) where active = 't';" ;; 2 ) read -p "Enter the user account name: " ex_mat $db_conn "select id,username,balance from account join balance using (id) where username='$ex_mat' and active = 't';" ;; 3 ) read -p "Enter search term: " bal_user bal_user1=$($db_conn "select id,username,balance from account join balance using (id) where active = 't';") echo "$bal_user1" | awk 'FNR == 1 {print}' echo "$bal_user1" | grep -iE "$bal_user" echo "" ;; [0q] ) break ;; esac done } bank_menu() { clear while true do echo "" echo "1) Withdraw" echo "2) Deposit" echo "3) Transfer" echo "4) Quick balance check" echo "" echo "q) Go Back" echo "" read -n1 -p "*Banking Menu: Select from the above: " ii_bank echo "" case $ii_bank in [0q] ) break ;; 1 ) read -p "Withdraw from user: " with_user read -p "Amount to withdraw: " with_amt LAST_USER=$with_user ## Not sure why this was here... #echo "$with_user" | awk 'FNR == 1 {print}' # Grabs the current amount the user has stored in the database. with_1_amt=$($db_conn "select id,username,balance from account join balance using (id) where username='$with_user';"| awk 'FNR == 3' | awk -F\| '{print $3}') # This grabs the user's ID to use on the balance table. with_user_id=$($db_conn "select id,username,balance from account join balance using (id) where username='$with_user';" | awk 'FNR == 3' | awk -F\| '{print $1}') # Updates the database with the new amount. $db_conn "update balance set balance = balance - '$with_amt' where id = '$with_user_id';" # Transactioning $db_conn "insert into transaction (id,username,amount,timestamp) values ('$with_user_id','$with_user','-$with_amt','now');" ;; 2 ) read -p "Deposit to user: " depo_user read -p "Amount to deposit: " depo_amt LAST_USER=$depo_user depo_1_amt=$($db_conn "select id,username,balance from account join balance using (id) where username='$depo_user';"| awk 'FNR == 3' | awk -F\| '{print $3}') # This grabs the user's ID to use on the balance table. depo_user_id=$($db_conn "select id,username,balance from account join balance using (id) where username='$depo_user';" | awk 'FNR == 3' | awk -F\| '{print $1}') # Updates the database with the new amount. $db_conn "update balance set balance = balance + '$depo_amt' where id = '$depo_user_id';" # Transactioning $db_conn "insert into transaction (id,username,amount,timestamp) values ('$depo_user_id','$depo_user','+$depo_amt','now');" ;; 3 ) read -p "Transfer from user: " tran_user1 read -p "Transer to user: " tran_user2 read -p "Amount to transfer: " tran_amt # Grabs user IDs. tran_user1_id=$($db_conn "select id,username,balance from account join balance using (id) where username='$tran_user1';" | awk 'FNR == 3' | awk -F\| '{print $1}') tran_user2_id=$($db_conn "select id,username,balance from account join balance using (id) where username='$tran_user2';" | awk 'FNR == 3' | awk -F\| '{print $1}') # Updates the database with the new amounts. $db_conn "update balance set balance = balance - '$tran_amt' where id = '$tran_user1_id';" $db_conn "update balance set balance = balance + '$tran_amt' where id = '$tran_user2_id';" # Transactioning $db_conn "insert into transaction (id,username,amount,timestamp) values ('$tran_user1_id','$tran_user1','-$tran_amt','now');" $db_conn "insert into transaction (id,username,amount,timestamp) values ('$tran_user2_id','$tran_user2','+$tran_amt','now');" LAST_USER=$tran_user1 ;; 4 ) $db_conn "select id,username,balance from account join balance using (id) where username='$LAST_USER';" ;; * ) clear ;; esac done } tran_menu() { clear while true do echo "" echo "1) Show Transactions for user" echo "2) Show last # Transactions." echo "3) Show last 5 Transactions." echo "" echo "q) Go back." echo "" read -n1 -p "*Transaction Menu: Select from above: " ii_tmenu case $ii_tmenu in 1 ) echo "" read -p "Transactions for user: " tmenu_user read -p "Number of transactions: (0 for no limit) " tmenu_num1 if [ $tmenu_num1 = 0 ]; then { tmenu_num1=all } fi $db_conn "select id,username,amount,timestamp from transaction where username = '$tmenu_user' order by timestamp DESC limit $tmenu_num1;" ;; 2 ) echo "" read -p "Number of transactions: " tmenu_num2 if [ $tmenu_num2 = 0 ]; then { tmenu_num2=all } fi $db_conn "select id,username,amount,timestamp from transaction order by timestamp DESC limit $tmenu_num2;" ;; 3 ) echo "" $db_conn "select id,username,amount,timestamp from transaction order by timestamp DESC limit 5;" ;; [0q] ) break ;; * ) clear ;; esac done } dis_user() { clear echo "Warning!!! Users cannot be re-enabled." read -p "**Disable User: " dis_user echo"" echo"" echo "Verify the following information is correct." $db_conn "select id,username from account where username = '$dis_user';" read -n1 -p "Disable User (y/N) " i_dis case $i_dis in [Yy] ) $db_conn "update account set active = 'f' where username = '$dis_user';" ; echo "" ;; * ) echo "Aborted" echo "" ;; esac } clear while true do # Main Menu echo "" echo "DDBank Main Menu: " echo "1) Create new user account." echo "2) List user accounts." echo "3) List user(s) balance." echo "4) Banking Menu (Deposit, withdraw, transfer)" echo "5) Transaction Menu" echo "" echo "8) *Disable User!" echo "9) Clear contents of screen" echo "0) Exit" echo "" read -p "Select from the menu above: " -n1 i case $i in 1 ) new_account ;; 2 ) list_user ;; 3 ) list_balance ;; 4 ) bank_menu ;; 5 ) tran_menu ;; 8 ) dis_user ;; 9 ) clear ;; 0 ) break ;; * ) clear ;; esac done #if [ $i == 1 ]; then { # new_account #} else { # echo "Some other thing..." #} fi