You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

281 lines
11 KiB

#!/usr/bin/env sh
### Config
### # Get script directory
### SCRIPT_DIR="$( cd "$( dirname "${BASH_SOURCE[0]}" )" &> /dev/null && pwd )"
### # Source DB config
### source $SCRIPT_DIR/.dndbank.conf
source ${HOME}/.dndbank.conf
### Error Handling
fail_badsel() {
printf "\n$(tput setaf 3)ERROR 10:$(tput sgr0) $(tput bold)Bad selection!$(tput sgr0)\n\n"
# Restart at main menu
show_menu init
}
### Menu
hold_menu() {
echo "Press [Enter] to continue..."
read
}
show_menu() {
#[ "$1" == "init" ] || pre_menu
printf 'Select a function by number:
1. Create Account
2. Deposit
3. Withdrawal
4. Transfer
5. Show All Balances
6. Show Transaction History
7. Show Transactions for Account
8. Show Transfer History
9. Disable Account (!)
0. Enable Account
Q. Quit
'
read -p "Selection: " -n1 action
case $action in
1)
echo " Create Account"
create_account
;;
2)
echo " Make a Desposit"
make_deposit
;;
3)
echo " Make a Withdrawal"
make_withdrawal
;;
4)
echo " Transfer Funds"
bank_transfer
;;
5)
echo " Show All Balances"
show_balance
;;
6)
echo " Show All Transactions"
show_transactions_all
;;
7)
echo " Show Transactions for Account"
show_transactions
;;
8)
echo " Show All Gold Transfers"
show_transfers
;;
9)
echo " Disable Account (!)"
disable_account
;;
0)
echo " Enable Account"
enable_account
;;
[Qq])
echo " Quit"
exit 0
;;
*)
fail_badsel
;;
esac
}
### Primary Functions ###
dbconnect="psql -h ${DB_HOST} -p ${DB_PORT} -U ${DB_USER} -d ${DB_NAME}"
create_account() {
read -p "Enter Account Name: " name
tstamp=$(date +%Y-%m-%d\ %H:%M:%S)
read -p "Creating account: '${name}'. Are you sure? (y/N)" -n1 CONFIRM
case $CONFIRM in
[Yy])
printf "\n$(tput bold)Creating account...\n\n$(tput sgr0)"
eval "$dbconnect -c \"INSERT INTO account (name,created) VALUES ('${name}','${tstamp}');\"" &&
eval "$dbconnect -c \"INSERT INTO balance (id,gold) VALUES ((SELECT id FROM account WHERE name = '${name}'),'0');\"" &&
printf "\n$(tput bold)$(tput setaf 2)DONE!\n\n$(tput sgr0)" ||
printf "\n$(tput bold)$(tput setaf 1)ERROR ??: Unknown\n\n$(tput sgr0)"
;;
*)
printf "\n$(tput bold)$(tput setaf 1)ABORT\n\n$(tput sgr0)"
;;
esac
hold_menu
}
disable_account() {
#printf "\n$(tput bold)$(tput setaf 3)This action is not yet supported!\n\n$(tput sgr0)"
eval "$dbconnect -c \"SELECT id,name,created FROM account WHERE enabled = true;\""
read -p "Enter Account ID: " ID
NAME=$(eval "$dbconnect -c \"SELECT id,name,created FROM account WHERE id = ${ID};\"" | grep -E "[0-9]{4}-[0-9]{2}-[0-9]{2} [0-9]{2}:[0-9]{2}:[0-9]{2}" | awk -F '|' '{print $2}')
read -p "$(tput bold)$(tput setaf 3)DISABLING$(tput sgr0) account #${ID}:$(tput bold)${NAME}$(tput sgr0)... Are you sure? (y/N)" -n1 CONFIRM
case $CONFIRM in
[Yy])
printf "\n$(tput bold)$(tput setaf 3)Disabling account...\n\n$(tput sgr0)"
eval "$dbconnect -c \"UPDATE account SET enabled = false WHERE id = '${ID}';\"" &&
printf "\n$(tput bold)$(tput setaf 2)DONE!\n\n$(tput sgr0)" ||
printf "\n$(tput bold)$(tput setaf 1)ERROR ??: Unknown\n\n$(tput sgr0)"
;;
*)
printf "\n$(tput bold)$(tput setaf 1)ABORT\n\n$(tput sgr0)"
;;
esac
# # Delete Account (DO NOT USE)
# DELETE FROM balance WHERE id = (SELECT id FROM account WHERE name = ${name});
# DELETE FROM transaction WHERE account = (SELECT id FROM account WHERE name = ${name});
# DELETE FROM account WHERE name = ${name};
hold_menu
}
enable_account() {
echo "DISABLED Accounts:"
eval "$dbconnect -c \"SELECT id,name,created FROM account WHERE enabled = false;\""
read -p "Enter Account ID: " ID
NAME=$(eval "$dbconnect -c \"SELECT id,name,created FROM account WHERE id = ${ID};\"" | grep -E "[0-9]{4}-[0-9]{2}-[0-9]{2} [0-9]{2}:[0-9]{2}:[0-9]{2}" | awk -F '|' '{print $2}')
read -p "$(tput bold)Enabling$(tput sgr0) account #${ID}:$(tput bold)${NAME}$(tput sgr0)... Are you sure? (y/N)" -n1 CONFIRM
case $CONFIRM in
[Yy])
printf "\n$(tput bold)$(tput setaf 3)Disabling account...\n\n$(tput sgr0)"
eval "$dbconnect -c \"UPDATE account SET enabled = true WHERE id = '${ID}';\"" &&
printf "\n$(tput bold)$(tput setaf 2)DONE!\n\n$(tput sgr0)" ||
printf "\n$(tput bold)$(tput setaf 1)ERROR ??: Unknown\n\n$(tput sgr0)"
;;
*)
printf "\n$(tput bold)$(tput setaf 1)ABORT\n\n$(tput sgr0)"
;;
esac
# # Delete Account (DO NOT USE)
# DELETE FROM balance WHERE id = (SELECT id FROM account WHERE name = ${name});
# DELETE FROM transaction WHERE account = (SELECT id FROM account WHERE name = ${name});
# DELETE FROM account WHERE name = ${name};
hold_menu
}
make_deposit() {
eval "$dbconnect -c \"SELECT id,name FROM account WHERE enabled = true;\""
read -p "Enter Account ID: " ID
read -p "Enter amount of gp to deposit: " amount
tstamp=$(date +%Y-%m-%d\ %H:%M:%S)
read -p "Depositing ${amount}gp into account ${ID}. Are you sure? (y/N)" -n1 CONFIRM
case $CONFIRM in
[Yy])
printf "\n$(tput bold)Depositing...\n\n$(tput sgr0)"
eval "$dbconnect -c \"INSERT INTO transaction (account,deposit,timestamp) VALUES ('${ID}','${amount}','${tstamp}');\""
eval "$dbconnect -c \"UPDATE balance SET gold = gold + '${amount}' WHERE id = '${ID}';\""
printf "\n$(tput bold)$(tput setaf 2)DONE!\n\n$(tput sgr0)" ||
printf "\n$(tput bold)$(tput setaf 1)ERROR ??: Unknown\n\n$(tput sgr0)"
printf "\nNew balance:\n"
eval "$dbconnect -c \"SELECT account.id,account.name,gold FROM balance INNER JOIN account ON account.id = balance.id where balance.id = ${ID};\""
;;
*)
printf "\n$(tput bold)$(tput setaf 1)ABORT\n\n$(tput sgr0)"
;;
esac
hold_menu
# # Transaction (Deposit)
# INSERT INTO transaction (account,deposit,timestamp) VALUES ((SELECT id FROM account WHERE name = ${name}),${amount},${tstamp});
# UPDATE balance SET gold = gold + 123.45 WHERE id = (SELECT id FROM account WHERE name = ${name});
}
make_withdrawal() {
eval "$dbconnect -c \"SELECT id,name FROM account WHERE enabled = true;\""
read -p "Enter Account ID: " ID
read -p "Enter amount of gp to withdraw: " amount
tstamp=$(date +%Y-%m-%d\ %H:%M:%S)
read -p "Withdrawing ${amount}gp from account ${ID}. Are you sure? (y/N)" -n1 CONFIRM
case $CONFIRM in
[Yy])
printf "\n$(tput bold)Withdrawing...\n\n$(tput sgr0)"
eval "$dbconnect -c \"INSERT INTO transaction (account,withdrawal,timestamp) VALUES ('${ID}','${amount}','${tstamp}');\""
eval "$dbconnect -c \"UPDATE balance SET gold = gold - '${amount}' WHERE id = '${ID}';\""
printf "\n$(tput bold)$(tput setaf 2)DONE!\n\n$(tput sgr0)" ||
printf "\n$(tput bold)$(tput setaf 1)ERROR ??: Unknown\n\n$(tput sgr0)"
printf "\nNew balance:\n"
eval "$dbconnect -c \"SELECT account.id,account.name,gold FROM balance INNER JOIN account ON account.id = balance.id where balance.id = ${ID};\""
;;
*)
printf "\n$(tput bold)$(tput setaf 1)ABORT\n\n$(tput sgr0)"
;;
esac
hold_menu
# # Transaction (Withdraw)
# INSERT INTO transaction (account,withdrawal,timestamp) VALUES ((SELECT id FROM account WHERE name = ${name}),${amount},${tstamp});
# UPDATE balance SET gold = gold - 12.34 WHERE id = (SELECT id FROM account WHERE name = ${name});
}
bank_transfer() {
# show balances first
eval "$dbconnect -c \"SELECT account.id,account.name,gold FROM balance INNER JOIN account ON account.id = balance.id WHERE account.enabled = true ORDER BY name;\""
read -p "Enter Account ID to transfer $(tput bold)FROM$(tput sgr0): " FROM
read -p "Enter Account ID to transfer $(tput bold)TO$(tput sgr0): " TO
read -p "Enter amount of gp to transfer: " amount
tstamp=$(date +%Y-%m-%d\ %H:%M:%S)
read -p "Transfer ${amount}gp from account ${FROM} to account ${TO}. Are you sure? (y/N)" -n1 CONFIRM
case $CONFIRM in
[Yy])
printf "\n$(tput bold)Transferring...\n\n$(tput sgr0)"
eval "$dbconnect -c \"INSERT INTO transaction (account,withdrawal,timestamp) VALUES ('${FROM}','${amount}','${tstamp}');\""
eval "$dbconnect -c \"UPDATE balance SET gold = gold - '${amount}' WHERE id = '${FROM}';\""
eval "$dbconnect -c \"INSERT INTO transaction (account,deposit,timestamp) VALUES ('${TO}','${amount}','${tstamp}');\""
eval "$dbconnect -c \"UPDATE balance SET gold = gold + '${amount}' WHERE id = '${TO}';\""
eval "$dbconnect -c \"INSERT INTO transfer (from_id,to_id,gold,timestamp) VALUES ('${FROM}','${TO}','${amount}','${tstamp}');\""
printf "\n$(tput bold)$(tput setaf 2)DONE!\n\n$(tput sgr0)" ||
printf "\n$(tput bold)$(tput setaf 1)ERROR ??: Unknown\n\n$(tput sgr0)"
printf "\nNew balances:\n"
eval "$dbconnect -c \"SELECT account.id,account.name,gold FROM balance INNER JOIN account ON account.id = balance.id WHERE balance.id = ${FROM} OR balance.id = ${TO};\""
;;
*)
printf "\n$(tput bold)$(tput setaf 1)ABORT\n\n$(tput sgr0)"
;;
esac
hold_menu
}
show_balance() {
eval "$dbconnect -c \"SELECT account.id,account.name,gold FROM balance INNER JOIN account ON account.id = balance.id WHERE account.enabled = true ORDER BY name;\""
hold_menu
}
show_transactions_all() {
eval "$dbconnect -c \"SELECT transaction.id,account.name,withdrawal,deposit,timestamp FROM transaction INNER JOIN account ON account.id = transaction.account ORDER BY timestamp;\""
hold_menu
}
show_transactions() {
read -p "Enter search term (e.g. character name): " name
echo
eval "$dbconnect -c \"SELECT transaction.id,account.name,withdrawal,deposit,timestamp FROM transaction INNER JOIN account ON account.id = transaction.account ORDER BY timestamp;\"" | grep -iE "$name|timestamp|------"
echo
hold_menu
}
show_transfers() {
eval "$dbconnect -c \"SELECT transfer.id,f.name AS from,t.name AS to,gold,timestamp FROM transfer LEFT JOIN account AS f ON f.id = transfer.from_id LEFT JOIN account AS t ON t.id = transfer.to_id;\""
hold_menu
}
while true; do
show_menu init
done
exit 1