Browse Source

reversed the polarity

main
deicidus 2 years ago
parent
commit
3d07a6ac5c
  1. 20
      mud
  2. 10
      spells/cantrips/validate-path
  3. 106
      spells/menu/bitcoin-menu
  4. 78
      spells/menu/change-bitcoin-directory
  5. 77
      spells/menu/configure-bitcoin
  6. 88
      spells/menu/install-bitcoin
  7. 42
      spells/menu/repair-bitcoin-permissions
  8. 70
      spells/menu/uninstall-bitcoin

20
mud

@ -1,20 +0,0 @@
#!/bin/sh
if ! command -v path-wizard > /dev/null 2>&1; then
read -p "ao-mud is not installed, install now? [y/n]: " choice
if [ "$choice" = "y" ]; then
SCRIPT_DIR=$( cd ${0%/*} && pwd -P )
chmod +x $SCRIPT_DIR/spells/path-wizard
$SCRIPT_DIR/spells/path-wizard add $SCRIPT_DIR/spells
$SCRIPT_DIR/spells/path-wizard add $SCRIPT_DIR/spells/cantrips
$SCRIPT_DIR/spells/path-wizard add $SCRIPT_DIR/spells/menu
echo "Your spellbook has been activated, please open a fresh terminal now."
exit 0
else
echo "Ok, well the mud won't work until you install it by adding the spells directory to your PATH."
exit 1
fi
fi
mud-menu

10
spells/cantrips/validate-path

@ -10,8 +10,14 @@ REGEX3='^.{4097,}$'
if echo "$1" | grep -Eq "$REGEX1" || echo "$1" | grep -Eq "$REGEX2" || echo "$1" | grep -Eq "$REGEX3"; then
# If any regex matches, this is an invalid path
exit 1
if [ -t 1 ]; then # Check if output is a terminal
echo "Valid UNIX path."
fi
exit 0
else
# Otherwise, it's valid
exit 0
if [ -t 1 ]; then # Check if output is a terminal
echo "Invalid UNIX path."
fi
exit 1
fi

106
spells/menu/bitcoin-menu

@ -1,106 +0,0 @@
#!/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"
set_config="Configure Bitcoin%configure-bitcoin"
change_directory="Change Bitcoin Directory%change-bitcoin-directory"
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" "$set_config" "$change_directory" "$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" "$set_config" "$change_directory" "$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

@ -1,78 +0,0 @@
#!/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"

77
spells/menu/configure-bitcoin

@ -1,77 +0,0 @@
#!/bin/sh
# This spell facilitates the setting of the configurations for thy Bitcoin system.
# Function to check if a command needs to be run 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
}
# Create bitcoin directory and configuration file if they do not exist
mkdir -p "$HOME/.bitcoin"
touch "$HOME/.bitcoin/bitcoin.conf"
# Bitcoin defaults
defaults="proxy=127.0.0.1:9050
listen=1
bind=127.0.0.1
disablewallet=1
zmqpubrawblock=tcp://127.0.0.1:28332
zmqpubrawtx=tcp://127.0.0.1:28333
BTC_LOGIN"
bitcoin_conf="$HOME/.bitcoin/bitcoin.conf"
# If the file was just created (is empty), write defaults
if [ ! -s "$bitcoin_conf" ]; then
echo "$defaults" > "$bitcoin_conf"
else
reset_conf=$(ask_yn "Would you like to reset the bitcoin.conf file to defaults?" "n")
if [ "$reset_conf" = "Y" ]; then
echo "$defaults" > "$bitcoin_conf"
fi
fi
# Function to modify bitcoin.conf using awk
modify_bitcoin_conf() {
local key=$1
local value=$2
awk -v key="$key" -v value="$value" 'BEGIN {found=0} !/^#/ && $1==key {found=1; print key"="value; next} {print} END {if (!found) print key"="value}' "$bitcoin_conf" > "$bitcoin_conf.tmp" && mv "$bitcoin_conf.tmp" "$bitcoin_conf" || retry_with_sudo mv "$bitcoin_conf.tmp" "$bitcoin_conf"
}
# Pruning question and storage amount
prune=$(ask_yn "Wouldst thou like to enable pruning? Pruning allows thee to reduce disk usage by discarding some transaction history." "y")
if [ "$prune" = "Y" ]; then
modify_bitcoin_conf "prune" "1"
printf "What is the maximum memory thou art willing to dedicate to Bitcoin in MB? (Type a number) [default: 300]: "
read -r max_mem
max_mem=${max_mem:-300}
modify_bitcoin_conf "dbcache" "$max_mem"
else
modify_bitcoin_conf "prune" "0"
fi
# Network selection
mainnet=$(ask_yn "Art thou operating on the main Bitcoin network?" "y")
if [ "$mainnet" = "Y" ]; then
modify_bitcoin_conf "testnet" "0"
else
modify_bitcoin_conf "testnet" "1"
fi
# Blockchain storage location
default_data_dir="$HOME/.bitcoin/blocks"
modify_bitcoin_conf "datadir" "$default_data_dir"
alt_storage=$(ask_yn "Wouldst thou like to store the blockchain in the default location ($default_data_dir)?" "y")
if [ "$alt_storage" = "N" ]; then
printf "Please type the alternate path thou wouldst like to use for blockchain storage: "
read -r alt_path
modify_bitcoin_conf "datadir" "$alt_path"
fi
printf "Bitcoin configuration has been updated.\n"

88
spells/menu/install-bitcoin

@ -1,88 +0,0 @@
#!/bin/sh
# This spell installs bitcoin on almost any Linux platform, from source when possible, otherwise from precompiled binary.
# Set Bitcoin version
BITCOIN_VERSION=24.1
# 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://bitcoincore.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://bitcoincore.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://bitcoincore.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

42
spells/menu/repair-bitcoin-permissions

@ -1,42 +0,0 @@
#!/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

@ -1,70 +0,0 @@
#!/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