From 75b43b5dfc9003c68236c719660104a367ed2ccb Mon Sep 17 00:00:00 2001 From: deicidus Date: Thu, 25 May 2023 23:28:47 -0700 Subject: [PATCH] bitcoin menu almost finished --- spells/cantrips/max-length | 8 +- spells/cantrips/menu | 4 +- spells/disenchant | 2 +- spells/enchant | 3 +- spells/jump-to-marker | 7 +- spells/menu/bitcoin-menu | 106 +++++++++++++++++++++++++ spells/menu/change-bitcoin-directory | 78 ++++++++++++++++++ spells/menu/configure-bitcoin | 78 ++++++++++++++++++ spells/menu/install-bitcoin | 88 ++++++++++++++++++++ spells/menu/mud-admin | 0 spells/menu/repair-bitcoin-permissions | 42 ++++++++++ spells/menu/uninstall-bitcoin | 70 ++++++++++++++++ 12 files changed, 476 insertions(+), 10 deletions(-) create mode 100755 spells/menu/bitcoin-menu create mode 100755 spells/menu/change-bitcoin-directory create mode 100755 spells/menu/configure-bitcoin create mode 100644 spells/menu/install-bitcoin mode change 100644 => 100755 spells/menu/mud-admin create mode 100755 spells/menu/repair-bitcoin-permissions create mode 100644 spells/menu/uninstall-bitcoin diff --git a/spells/cantrips/max-length b/spells/cantrips/max-length index 6c1a134..6b63674 100755 --- a/spells/cantrips/max-length +++ b/spells/cantrips/max-length @@ -20,7 +20,7 @@ max_length() { fi done else - # Argument is an array + # Argument is an array # Todo: This never happens and also doesn't work if it does. longest=0 for arg in "${!1}"; do arg_length=${#arg} @@ -35,8 +35,4 @@ max_length() { echo $longest } -# Check if the script is being called from another script -if [ "${BASH_SOURCE[0]}" = "$0" ]; then - # If not, call the function and pass it the command line arguments - max_length "$@" -fi \ No newline at end of file +max_length "$@" \ No newline at end of file diff --git a/spells/cantrips/menu b/spells/cantrips/menu index 999226c..5bf3f66 100755 --- a/spells/cantrips/menu +++ b/spells/cantrips/menu @@ -138,7 +138,9 @@ test_menu() { cursor-blink off # Display the menu's description -echo $description +if [ -n "$description" ]; then + printf "$description\n" +fi # Menu loop while true; do diff --git a/spells/disenchant b/spells/disenchant index f77d9aa..5267313 100755 --- a/spells/disenchant +++ b/spells/disenchant @@ -35,7 +35,7 @@ if [ -z "$1" ]; then fi # Read the extended attribute keys from the file -keys=$(./read-magic "$1") +keys=$(read-magic "$1") # Extract the attribute keys from the output of read-magic keys=$(echo "$keys" | awk -F: '{print $1}' | tr -d ' ') echo $keys diff --git a/spells/enchant b/spells/enchant index 16b45f7..2513955 100755 --- a/spells/enchant +++ b/spells/enchant @@ -16,7 +16,7 @@ attribute=$2 value=$3 # Check that the file exists -if [ ! -f "$file" ]; then +if [ ! -f "$file" ] && [ ! -d "$file" ]; then echo "Error: The file does not exist." exit 1 fi @@ -40,6 +40,7 @@ set_attribute_value() { } # Set the attribute value using the set_attribute_value function +value=$(printf "%q\n" "$value") set_attribute_value "$attribute" "$value" "$file" echo "The file has been enchanted with the attribute '$attribute' and value '$value'." diff --git a/spells/jump-to-marker b/spells/jump-to-marker index 2298c66..12f44cb 100755 --- a/spells/jump-to-marker +++ b/spells/jump-to-marker @@ -6,6 +6,8 @@ # Having to source the script explicitly can be avoided if the spell is 'memorized' (sourced in .bashrc). # The spell will offer to do this, installing 'jump' as a systemwide keyword for you. +# todo: you can automatically source with 'exec bash'. this could simplify this script a lot + # Check if the "jump" spell is already installed if ! grep -q "jump" ~/.bashrc; then printf "You have not yet memorized the 'jump-to-marker' spell, so you can't cast it anywhere. This spell is so powerful that it must be memorized (sourced in .bashrc) to use it as a normal command. Memorize the 'jump' spell now? (y/n) " @@ -36,7 +38,7 @@ jump() { else # If the current directory is not the destination, change the current directory to the destination cd "$destination" - + # Print a random teleportation message messages=("You feel a sudden warmth as you are enveloped in a glowing aura. When it fades, you find yourself at your destination." "A strange sensation comes over you as you close your eyes. When you open them again, you are standing at your destination." @@ -67,6 +69,9 @@ jump() { "You channel the energy of the celestial bodies to jump to the marked location." "You recite an ancient incantation, and a magical portal appears, taking you to the marked location.") echo "${messages[$RANDOM % ${#messages[@]}]}" + + # Look around, since the modified 'cd' command won't be available in this subshell context + look fi else # If the marker file does not exist, inform the user that no location has been marked diff --git a/spells/menu/bitcoin-menu b/spells/menu/bitcoin-menu new file mode 100755 index 0000000..cf72a18 --- /dev/null +++ b/spells/menu/bitcoin-menu @@ -0,0 +1,106 @@ +#!/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 \ No newline at end of file diff --git a/spells/menu/change-bitcoin-directory b/spells/menu/change-bitcoin-directory new file mode 100755 index 0000000..8063af2 --- /dev/null +++ b/spells/menu/change-bitcoin-directory @@ -0,0 +1,78 @@ +#!/bin/sh + +# This spell alters the location of thy Bitcoin data chamber on your system. + +# Check if a command needs to be re-invoked with sudo +retry_with_sudo() { + echo "Permission denied. Invoking the power of the super user..." + sudo "$@" + if [ $? -ne 0 ]; then + echo "Failed to execute with elevated privileges. Make certain thou hast the necessary permissions and try again." + exit 1 + fi +} + +# Check for the bitcoin.conf file +if [ ! -f "$HOME/.bitcoin/bitcoin.conf" ]; then + echo "Unable to locate bitcoin.conf. Canst thou provide its location?" + read bitcoin_conf + if [ ! -f "$bitcoin_conf" ]; then + echo "bitcoin.conf file not found at the provided location. The spell must end here." + exit 1 + fi +else + bitcoin_conf="$HOME/.bitcoin/bitcoin.conf" +fi + +# Get the current Bitcoin directory +current_directory=$(grep "datadir" "$bitcoin_conf" | cut -d'=' -f2) +if [ -z "$current_directory" ]; then + current_directory="$HOME/.bitcoin" +else + # Confirm that the directory actually exists + if [ ! -d "$current_directory" ]; then + echo "The Bitcoin data chamber specified in bitcoin.conf doth not exist. Please ensure the correctness of the 'datadir' value in bitcoin.conf." + exit 1 + fi + echo "Verified the existence of the Bitcoin data chamber at: $current_directory" +fi + +# Ask for the new directory +echo "Enter the new haven for thy Bitcoin data:" +read new_directory + +# Create the new directory if it does not exist +if [ ! -d "$new_directory" ]; then + mkdir "$new_directory" || retry_with_sudo mkdir "$new_directory" +fi + +# Ask user what to move +echo "What wouldst thou like to move to the new Bitcoin data haven?" +echo "1. The entire Bitcoin data chamber" +echo "2. Only the blockchain data" +echo "3. Move naught" + +read move_choice + +case "$move_choice" in + 1) + echo "Moving the entire Bitcoin data chamber..." + mv "$current_directory"/* "$new_directory" || retry_with_sudo mv "$current_directory"/* "$new_directory" + ;; + 2) + echo "Moving only the blockchain data..." + mv "$current_directory/blocks" "$current_directory/chainstate" "$new_directory" || retry_with_sudo mv "$current_directory/blocks" "$current_directory/chainstate" "$new_directory" + ;; + 3) + echo "Moving naught..." + ;; + *) + echo "Invalid choice, the spell ends here without moving anything." + exit 1 + ;; +esac + +# Update the bitcoin.conf file to point to the new directory +awk -v path="$new_directory" '!/datadir/ {print} /datadir/ {$3 = path; print}' "$bitcoin_conf" | tee "$bitcoin_conf" >/dev/null || { retry_with_sudo awk -v path="$new_directory" '!/datadir/ {print} /datadir/ {$3 = path; print}' "$bitcoin_conf" | sudo tee "$bitcoin_conf" >/dev/null; } + +echo "The Bitcoin data chamber now resides in: $new_directory" \ No newline at end of file diff --git a/spells/menu/configure-bitcoin b/spells/menu/configure-bitcoin new file mode 100755 index 0000000..8063af2 --- /dev/null +++ b/spells/menu/configure-bitcoin @@ -0,0 +1,78 @@ +#!/bin/sh + +# This spell alters the location of thy Bitcoin data chamber on your system. + +# Check if a command needs to be re-invoked with sudo +retry_with_sudo() { + echo "Permission denied. Invoking the power of the super user..." + sudo "$@" + if [ $? -ne 0 ]; then + echo "Failed to execute with elevated privileges. Make certain thou hast the necessary permissions and try again." + exit 1 + fi +} + +# Check for the bitcoin.conf file +if [ ! -f "$HOME/.bitcoin/bitcoin.conf" ]; then + echo "Unable to locate bitcoin.conf. Canst thou provide its location?" + read bitcoin_conf + if [ ! -f "$bitcoin_conf" ]; then + echo "bitcoin.conf file not found at the provided location. The spell must end here." + exit 1 + fi +else + bitcoin_conf="$HOME/.bitcoin/bitcoin.conf" +fi + +# Get the current Bitcoin directory +current_directory=$(grep "datadir" "$bitcoin_conf" | cut -d'=' -f2) +if [ -z "$current_directory" ]; then + current_directory="$HOME/.bitcoin" +else + # Confirm that the directory actually exists + if [ ! -d "$current_directory" ]; then + echo "The Bitcoin data chamber specified in bitcoin.conf doth not exist. Please ensure the correctness of the 'datadir' value in bitcoin.conf." + exit 1 + fi + echo "Verified the existence of the Bitcoin data chamber at: $current_directory" +fi + +# Ask for the new directory +echo "Enter the new haven for thy Bitcoin data:" +read new_directory + +# Create the new directory if it does not exist +if [ ! -d "$new_directory" ]; then + mkdir "$new_directory" || retry_with_sudo mkdir "$new_directory" +fi + +# Ask user what to move +echo "What wouldst thou like to move to the new Bitcoin data haven?" +echo "1. The entire Bitcoin data chamber" +echo "2. Only the blockchain data" +echo "3. Move naught" + +read move_choice + +case "$move_choice" in + 1) + echo "Moving the entire Bitcoin data chamber..." + mv "$current_directory"/* "$new_directory" || retry_with_sudo mv "$current_directory"/* "$new_directory" + ;; + 2) + echo "Moving only the blockchain data..." + mv "$current_directory/blocks" "$current_directory/chainstate" "$new_directory" || retry_with_sudo mv "$current_directory/blocks" "$current_directory/chainstate" "$new_directory" + ;; + 3) + echo "Moving naught..." + ;; + *) + echo "Invalid choice, the spell ends here without moving anything." + exit 1 + ;; +esac + +# Update the bitcoin.conf file to point to the new directory +awk -v path="$new_directory" '!/datadir/ {print} /datadir/ {$3 = path; print}' "$bitcoin_conf" | tee "$bitcoin_conf" >/dev/null || { retry_with_sudo awk -v path="$new_directory" '!/datadir/ {print} /datadir/ {$3 = path; print}' "$bitcoin_conf" | sudo tee "$bitcoin_conf" >/dev/null; } + +echo "The Bitcoin data chamber now resides in: $new_directory" \ No newline at end of file diff --git a/spells/menu/install-bitcoin b/spells/menu/install-bitcoin new file mode 100644 index 0000000..27ab15d --- /dev/null +++ b/spells/menu/install-bitcoin @@ -0,0 +1,88 @@ +#!/bin/sh + +# This spell installs bitcoin on almost any Linux platform, from source when possible, otherwise from precompiled binary. + +# Set Bitcoin version +BITCOIN_VERSION=23.0 + +# Install Bitcoin from source +install_bitcoin_from_source() { + say "Installing Bitcoin Core from source" + if [ ! -e bitcoin-$BITCOIN_VERSION* ]; then + wget https://bitcoincore.org/bin/bitcoin-core-$BITCOIN_VERSION/bitcoin-$BITCOIN_VERSION.tar.gz + fi + . ./path_to_install_if_needed_script.sh boost + tar -xvf bitcoin-$BITCOIN_VERSION.tar.gz + sleep 1 + cd bitcoin-$BITCOIN_VERSION + chmod +x autogen.sh + ./autogen.sh + ./configure --disable-wallet + make + if [ $? -ne 0 ]; then + return 1 + fi + sudo make install + cd .. + return 0 +} + +# Install Bitcoin ARM precompiled binary +install_bitcoin_arm_binary() { + say "Installing Bitcoin Core ARM precompiled binary" + if [ ! -e bitcoin-$BITCOIN_VERSION* ]; then + wget https://bitcoin.org/bin/bitcoin-core-$BITCOIN_VERSION/bitcoin-$BITCOIN_VERSION-arm-linux-gnueabihf.tar.gz + tar -xzf bitcoin-$BITCOIN_VERSION-arm-linux-gnueabihf.tar.gz + fi + sudo install -m 0755 -o root -g root -t /usr/local/bin bitcoin-$BITCOIN_VERSION/bin/* +} + +# Install Bitcoin x86_64 precompiled binary +install_bitcoin_x86_64_binary() { + say "Installing Bitcoin Core x86_64 precompiled binary" + if [ ! -e bitcoin-$BITCOIN_VERSION* ]; then + wget https://bitcoin.org/bin/bitcoin-core-$BITCOIN_VERSION/bitcoin-$BITCOIN_VERSION-x86_64-linux-gnu.tar.gz + tar -xzf bitcoin-$BITCOIN_VERSION-x86_64-linux-gnu.tar.gz + fi + sudo install -m 0755 -o root -g root -t /usr/local/bin bitcoin-$BITCOIN_VERSION/bin/* +} + +# Install Bitcoin macOS precompiled binary +install_bitcoin_macos_binary() { + say "Installing Bitcoin Core macOS precompiled binary" + if [ ! -e bitcoin-$BITCOIN_VERSION* ]; then + wget https://bitcoin.org/bin/bitcoin-core-$BITCOIN_VERSION/bitcoin-$BITCOIN_VERSION-osx-signed.dmg + hdiutil attach bitcoin-$BITCOIN_VERSION-osx-signed.dmg + cp -R /Volumes/Bitcoin-Core/Bitcoin-Qt.app /Applications/ + hdiutil detach /Volumes/Bitcoin-Core + fi +} + +# Detect the operating system +OS=$(uname -s) + +# Install Bitcoin based on the OS +case $OS in + Darwin) + install_bitcoin_macos_binary + ;; + Linux) + ARCH=$(uname -m) + case $ARCH in + x86_64) + if ! install_bitcoin_from_source; then + install_bitcoin_x86_64_binary + fi + ;; + arm* | aarch64) + install_bitcoin_arm_binary + ;; + *) + say "Unsupported architecture: $ARCH" + exit + esac + ;; + *) + say "Unsupported operating system: $OS" + exit +esac \ No newline at end of file diff --git a/spells/menu/mud-admin b/spells/menu/mud-admin old mode 100644 new mode 100755 diff --git a/spells/menu/repair-bitcoin-permissions b/spells/menu/repair-bitcoin-permissions new file mode 100755 index 0000000..b49fd3b --- /dev/null +++ b/spells/menu/repair-bitcoin-permissions @@ -0,0 +1,42 @@ +#!/bin/sh + +# This script sets the correct permissions on the Bitcoin configuration directory. + +# Set the default Bitcoin configuration directory +default_dir="$HOME/.bitcoin" + +# Check if the default directory exists +if [ -d "$default_dir" ]; then + bitcoin_dir="$default_dir" +else + # Prompt the user to enter the path to the Bitcoin configuration directory + read -p "Enter the path to the Bitcoin configuration directory: " bitcoin_dir +fi + +# Check if the provided directory exists +if [ -d "$bitcoin_dir" ]; then + # Set the default permission mode + default_mode=710 + + # Get the configuration directory mode from bitcoin.conf + config_mode=$(grep -E '^ConfigurationDirectoryMode=' "$bitcoin_dir/bitcoin.conf" | cut -d '=' -f 2) + + # If ConfigurationDirectoryMode is not specified, use the default mode + if [ -z "$config_mode" ]; then + config_mode=$default_mode + fi + + # Get the current permission mode of the directory + current_mode=$(stat -c "%a" "$bitcoin_dir") + + # Compare the current mode with the configured mode + if [ "$current_mode" != "$config_mode" ]; then + # Fix the permission by setting the correct mode + chmod "$config_mode" "$bitcoin_dir" + echo "The permission on the Bitcoin configuration directory has been set to $config_mode." + else + echo "The Bitcoin configuration directory is already configured with the correct permissions." + fi +else + echo "The provided directory does not exist." +fi \ No newline at end of file diff --git a/spells/menu/uninstall-bitcoin b/spells/menu/uninstall-bitcoin new file mode 100644 index 0000000..6296b46 --- /dev/null +++ b/spells/menu/uninstall-bitcoin @@ -0,0 +1,70 @@ +#!/bin/sh + +# Function to check if a package is installed in Debian-based distributions +is_deb_package_installed() { + dpkg-query -W -f='${Status}' "$1" 2>/dev/null | grep -c "ok installed" +} + +# Function to check if a package is installed in Redhat-based distributions +is_rpm_package_installed() { + rpm -q "$1" &> /dev/null +} + +# Function to check if a package is installed in Arch-based distributions +is_arch_package_installed() { + pacman -Q "$1" &> /dev/null +} + +# Uninstall Bitcoin from source +uninstall_bitcoin_from_source() { + say "Uninstalling Bitcoin Core installed from source" + sudo make uninstall +} + +# Uninstall Bitcoin package +uninstall_bitcoin_package() { + say "Uninstalling Bitcoin Core package" + + # Detect the operating system's package manager + if command -v apt &> /dev/null; then + if is_deb_package_installed bitcoin; then + sudo apt-get remove -y bitcoin + fi + elif command -v dnf &> /dev/null; then + if is_rpm_package_installed bitcoin; then + sudo dnf remove -y bitcoin + fi + elif command -v pacman &> /dev/null; then + if is_arch_package_installed bitcoin; then + sudo pacman -Rns bitcoin --noconfirm + fi + elif command -v brew &> /dev/null; then + if brew list --cask bitcoin-core; then + brew uninstall --cask bitcoin-core + fi + else + say "Unsupported package manager" + exit 1 + fi +} + +# Uninstall Bitcoin +uninstall_bitcoin() { + read -p "This will uninstall Bitcoin Core and might delete your wallet data. Are you sure? (Y/n) " yn + case $yn in + [Yy]* ) + uninstall_bitcoin_from_source + uninstall_bitcoin_package + ;; + [Nn]* ) + say "Uninstallation cancelled" + ;; + * ) + say "Invalid input. Please answer with Y (yes) or N (no)" + uninstall_bitcoin + ;; + esac +} + +# Start the uninstallation +uninstall_bitcoin \ No newline at end of file