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.
276 lines
10 KiB
276 lines
10 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. Direct Transfer
|
|
|
|
5. Show All Balances
|
|
6. Show Transaction History
|
|
7. Show Transactions for Account
|
|
8. Show Direct 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}"
|
|
get_name() {
|
|
# Arg1 == Account ID
|
|
# Arg2 == "bold" for bold text
|
|
NAME=$($dbconnect -c "SELECT id,name,created FROM account WHERE id = $1;" | grep -E "[0-9]{4}-[0-9]{2}-[0-9]{2} [0-9]{2}:[0-9]{2}:[0-9]{2}" | awk -F '|' '{print $2}')
|
|
NAME=${NAME#"${NAME%%[! ]*}"} NAME=${NAME%"${NAME##*[! ]}"} #Strip leading/trailing space from $NAME
|
|
|
|
if [ "$2" = "bold" ]; then
|
|
echo $(tput bold)${NAME}$(tput sgr0)
|
|
else
|
|
echo $NAME
|
|
fi
|
|
}
|
|
|
|
create_account() {
|
|
read -p "Enter Account Name ('c' to cancel): " name
|
|
([ "$name" = "c" ] || [ "$name" = "c" ]) && return
|
|
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)"
|
|
$dbconnect -c "INSERT INTO account (name,created) VALUES ('${name}','${tstamp}');" &&
|
|
$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() {
|
|
$dbconnect -c "SELECT id,name,created FROM account WHERE enabled = true ORDER BY name;"
|
|
read -p "Enter Account ID ('c' to cancel): " ID
|
|
([ "$ID" = "c" ] || [ "$ID" = "c" ]) && return
|
|
read -p "$(tput bold)$(tput setaf 3)DISABLING$(tput sgr0) account #${ID}: $(get_name $ID bold). Are you sure? (y/N)" -n1 CONFIRM
|
|
case $CONFIRM in
|
|
[Yy])
|
|
printf "\n$(tput bold)$(tput setaf 3)Disabling account...\n\n$(tput sgr0)"
|
|
$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
|
|
hold_menu
|
|
}
|
|
|
|
enable_account() {
|
|
echo "DISABLED Accounts:"
|
|
$dbconnect -c "SELECT id,name,created FROM account WHERE enabled = false ORDER BY name;"
|
|
read -p "Enter Account ID ('c' to cancel): " ID
|
|
([ "$ID" = "c" ] || [ "$ID" = "c" ]) && return
|
|
read -p "$(tput bold)Enabling$(tput sgr0) account #${ID}: $(get_name $ID bold). Are you sure? (y/N)" -n1 CONFIRM
|
|
case $CONFIRM in
|
|
[Yy])
|
|
printf "\n$(tput bold)$(tput setaf 3)Disabling account...\n\n$(tput sgr0)"
|
|
$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
|
|
hold_menu
|
|
}
|
|
|
|
make_deposit() {
|
|
$dbconnect -c "SELECT id,name FROM account WHERE enabled = true ORDER BY name;"
|
|
read -p "Enter Account ID ('c' to cancel): " ID
|
|
([ "$ID" = "c" ] || [ "$ID" = "c" ]) && return
|
|
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}: $(get_name $ID bold). Are you sure? (y/N)" -n1 CONFIRM
|
|
case $CONFIRM in
|
|
[Yy])
|
|
printf "\n$(tput bold)Depositing...\n\n$(tput sgr0)"
|
|
$dbconnect -c "INSERT INTO transaction (account,deposit,timestamp) VALUES ('${ID}','${amount}','${tstamp}');"
|
|
$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"
|
|
$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
|
|
}
|
|
|
|
make_withdrawal() {
|
|
$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;" #Show balances first
|
|
read -p "Enter Account ID ('c' to cancel): " ID
|
|
([ "$ID" = "c" ] || [ "$ID" = "c" ]) && return
|
|
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}: $(get_name $ID bold). Are you sure? (y/N)" -n1 CONFIRM
|
|
case $CONFIRM in
|
|
[Yy])
|
|
printf "\n$(tput bold)Withdrawing...\n\n$(tput sgr0)"
|
|
$dbconnect -c "INSERT INTO transaction (account,withdrawal,timestamp) VALUES ('${ID}','${amount}','${tstamp}');"
|
|
$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"
|
|
$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
|
|
}
|
|
|
|
bank_transfer() {
|
|
$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;" #Show balances first
|
|
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 "Transferring ${amount}gp from $(get_name $FROM bold) to $(get_name $TO bold). Are you sure? (y/N)" -n1 CONFIRM
|
|
case $CONFIRM in
|
|
[Yy])
|
|
printf "\n$(tput bold)Transferring...\n\n$(tput sgr0)"
|
|
$dbconnect -c "INSERT INTO transaction (account,withdrawal,timestamp) VALUES ('${FROM}','${amount}','${tstamp}');"
|
|
$dbconnect -c "UPDATE balance SET gold = gold - '${amount}' WHERE id = '${FROM}';"
|
|
$dbconnect -c "INSERT INTO transaction (account,deposit,timestamp) VALUES ('${TO}','${amount}','${tstamp}');"
|
|
$dbconnect -c "UPDATE balance SET gold = gold + '${amount}' WHERE id = '${TO}';"
|
|
$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"
|
|
$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() {
|
|
$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() {
|
|
$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
|
|
$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() {
|
|
$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 ORDER BY timestamp;"
|
|
hold_menu
|
|
}
|
|
|
|
while true; do
|
|
show_menu init
|
|
done
|
|
exit 1
|
|
|