Browse Source

bitcoin menu almost finished

main
deicidus 2 years ago
parent
commit
75b43b5dfc
  1. 8
      spells/cantrips/max-length
  2. 4
      spells/cantrips/menu
  3. 2
      spells/disenchant
  4. 3
      spells/enchant
  5. 7
      spells/jump-to-marker
  6. 106
      spells/menu/bitcoin-menu
  7. 78
      spells/menu/change-bitcoin-directory
  8. 78
      spells/menu/configure-bitcoin
  9. 88
      spells/menu/install-bitcoin
  10. 0
      spells/menu/mud-admin
  11. 42
      spells/menu/repair-bitcoin-permissions
  12. 70
      spells/menu/uninstall-bitcoin

8
spells/cantrips/max-length

@ -20,7 +20,7 @@ max_length() {
fi fi
done done
else else
# Argument is an array # Argument is an array # Todo: This never happens and also doesn't work if it does.
longest=0 longest=0
for arg in "${!1}"; do for arg in "${!1}"; do
arg_length=${#arg} arg_length=${#arg}
@ -35,8 +35,4 @@ max_length() {
echo $longest echo $longest
} }
# Check if the script is being called from another script max_length "$@"
if [ "${BASH_SOURCE[0]}" = "$0" ]; then
# If not, call the function and pass it the command line arguments
max_length "$@"
fi

4
spells/cantrips/menu

@ -138,7 +138,9 @@ test_menu() {
cursor-blink off cursor-blink off
# Display the menu's description # Display the menu's description
echo $description if [ -n "$description" ]; then
printf "$description\n"
fi
# Menu loop # Menu loop
while true; do while true; do

2
spells/disenchant

@ -35,7 +35,7 @@ if [ -z "$1" ]; then
fi fi
# Read the extended attribute keys from the file # 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 # Extract the attribute keys from the output of read-magic
keys=$(echo "$keys" | awk -F: '{print $1}' | tr -d ' ') keys=$(echo "$keys" | awk -F: '{print $1}' | tr -d ' ')
echo $keys echo $keys

3
spells/enchant

@ -16,7 +16,7 @@ attribute=$2
value=$3 value=$3
# Check that the file exists # Check that the file exists
if [ ! -f "$file" ]; then if [ ! -f "$file" ] && [ ! -d "$file" ]; then
echo "Error: The file does not exist." echo "Error: The file does not exist."
exit 1 exit 1
fi fi
@ -40,6 +40,7 @@ set_attribute_value() {
} }
# Set the attribute value using the set_attribute_value function # Set the attribute value using the set_attribute_value function
value=$(printf "%q\n" "$value")
set_attribute_value "$attribute" "$value" "$file" set_attribute_value "$attribute" "$value" "$file"
echo "The file has been enchanted with the attribute '$attribute' and value '$value'." echo "The file has been enchanted with the attribute '$attribute' and value '$value'."

7
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). # 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. # 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 # Check if the "jump" spell is already installed
if ! grep -q "jump" ~/.bashrc; then 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) " 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 else
# If the current directory is not the destination, change the current directory to the destination # If the current directory is not the destination, change the current directory to the destination
cd "$destination" cd "$destination"
# Print a random teleportation message # 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." 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." "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 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.") "You recite an ancient incantation, and a magical portal appears, taking you to the marked location.")
echo "${messages[$RANDOM % ${#messages[@]}]}" echo "${messages[$RANDOM % ${#messages[@]}]}"
# Look around, since the modified 'cd' command won't be available in this subshell context
look
fi fi
else else
# If the marker file does not exist, inform the user that no location has been marked # If the marker file does not exist, inform the user that no location has been marked

106
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

78
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"

78
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"

88
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

0
spells/menu/mud-admin

42
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

70
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
Loading…
Cancel
Save