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.
106 lines
3.5 KiB
106 lines
3.5 KiB
2 years ago
|
#!/bin/sh
|
||
|
|
||
|
# This script is a magical tome of sorcery, commanding the elusive entity known as Bitcoin.
|
||
|
|
||
|
. colors
|
||
|
|
||
|
# Returns 0 (true) if Bitcoin is installed, 1 (false) if not
|
||
|
is_bitcoin_installed() {
|
||
|
command -v bitcoin-cli >/dev/null 2>&1
|
||
|
}
|
||
|
|
||
|
# Returns 0 (true) if Bitcoin server is running, 1 (false) if not
|
||
|
is_bitcoin_running() {
|
||
|
bitcoin-cli getblockchaininfo >/dev/null 2>&1
|
||
|
}
|
||
|
|
||
|
# Get Bitcoin sync status: 'syncing', 'synced', 'unknown', or 'error'
|
||
|
get_sync_status() {
|
||
|
sync_status=$(bitcoin-cli getblockchaininfo 2>&1 | grep 'initialblockdownload' | awk '{print $2}' | tr -d ',')
|
||
|
if [ "$?" -ne "0" ]; then
|
||
|
echo "error"
|
||
|
elif [ "$sync_status" = "true" ]; then
|
||
|
echo "syncing"
|
||
|
elif [ "$sync_status" = "false" ]; then
|
||
|
echo "synced"
|
||
|
else
|
||
|
echo "unknown"
|
||
|
fi
|
||
|
}
|
||
|
|
||
|
# Returns colored text based on status
|
||
|
color_for_status() {
|
||
|
case "$1" in
|
||
|
"installed, running, synced")
|
||
|
printf "${GREEN}"
|
||
|
;;
|
||
|
"not installed")
|
||
|
printf "${GRAY}"
|
||
|
;;
|
||
|
"installed, not running"|"installed, running, syncing"*|"installed, running, not synced")
|
||
|
printf "${YELLOW}"
|
||
|
;;
|
||
|
"installed, running, error"|"*")
|
||
|
printf "${RED}"
|
||
|
;;
|
||
|
esac
|
||
|
}
|
||
|
|
||
|
# Returns colored status message
|
||
|
get_status() {
|
||
|
if is_bitcoin_installed; then
|
||
|
if is_bitcoin_running; then
|
||
|
running_status="running, "
|
||
|
sync_status=$(get_sync_status)
|
||
|
if [ "$sync_status" = "synced" ]; then
|
||
|
install_status="installed, $running_status synced"
|
||
|
elif [ "$sync_status" = "syncing" ]; then
|
||
|
progress=$(bitcoin-cli getblockchaininfo | grep 'progress' | awk '{print int($2*100)}')
|
||
|
install_status="installed, $running_status syncing $progress%"
|
||
|
elif [ "$sync_status" = "unknown" ]; then
|
||
|
install_status="installed, $running_status not synced"
|
||
|
elif [ "$sync_status" = "error" ]; then
|
||
|
install_status="installed, $running_status error"
|
||
|
fi
|
||
|
else
|
||
|
install_status="installed, not running"
|
||
|
fi
|
||
|
else
|
||
|
install_status="not installed"
|
||
|
fi
|
||
|
|
||
|
printf "$(color_for_status "$install_status")$install_status${RESET}\n"
|
||
|
}
|
||
|
|
||
|
# Displays the menu
|
||
|
display_menu() {
|
||
|
title="${BOLD}${CYAN}Bitcoin: $(get_status)"
|
||
|
blockchain_info="Display Blockchain Info%bitcoin-cli getblockchaininfo"
|
||
|
change_directory="Change Bitcoin Directory%change-bitcoin-directory"
|
||
|
set_config="Configure Bitcoin%configure-bitcoin"
|
||
|
clear_cache="Clear Bitcoin Cache%# sometimes necessary when cache becomes corrupted"
|
||
|
repair_permissions="Repair Permissions%repair-bitcoin-permissions # Sometimes necessary"
|
||
|
exit_option="Exit%kill -2 $$" # Commands are run with 'eval' by the menu script, so we can't simply say 'exit'. The keyword $$ gets this scripts PID and the -2 code is SIGINT aka Ctrl-C
|
||
|
|
||
|
if is_bitcoin_installed; then
|
||
|
install_bitcoin_option="Uninstall Bitcoin%uninstall-bitcoin"
|
||
|
if is_bitcoin_running; then
|
||
|
bitcoin_server_option="Stop Bitcoin Service%sudo systemctl stop bitcoind"
|
||
|
menu "$title" "$blockchain_info" "$change_directory" "$set_config" "$clear_cache" "$repair_permissions" "$bitcoin_server_option" "$install_bitcoin_option" "$exit_option"
|
||
|
else
|
||
|
bitcoin_server_option="Start Bitcoin Service%sudo systemctl start bitcoind"
|
||
|
menu "$title" "$change_directory" "$set_config" "$clear_cache" "$repair_permissions" "$bitcoin_server_option" "$install_bitcoin_option" "$exit_option"
|
||
|
fi
|
||
|
else
|
||
|
install_bitcoin_option="Install Bitcoin%install-bitcoin"
|
||
|
menu "$title" "$install_bitcoin_option" "$exit_option"
|
||
|
fi
|
||
|
}
|
||
|
|
||
|
# Catch Ctrl-C and exit
|
||
|
trap 'echo exiting; exit' INT
|
||
|
|
||
|
# Main execution loop
|
||
|
while true; do
|
||
|
display_menu
|
||
|
done
|