Browse Source

revised configure-bitcoin

main
deicidus 1 year ago
parent
commit
30c3378066
  1. 109
      spells/menu/configure-bitcoin

109
spells/menu/configure-bitcoin

@ -1,8 +1,8 @@
#!/bin/sh
# This spell alters the location of thy Bitcoin data chamber on your system.
# This script facilitates the setting of the configurations for thy Bitcoin system.
# Check if a command needs to be re-invoked with sudo
# 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 "$@"
@ -12,67 +12,64 @@ retry_with_sudo() {
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
# Create .bitcoin directory if it does not exist
mkdir -p "$HOME/.bitcoin"
bitcoin_conf="$HOME/.bitcoin/bitcoin.conf"
reset_conf="N"
# Get the current Bitcoin directory
current_directory=$(grep "datadir" "$bitcoin_conf" | cut -d'=' -f2)
if [ -z "$current_directory" ]; then
current_directory="$HOME/.bitcoin"
# If bitcoin.conf doesn't exist, create it. If it does exist, ask to reset it.
if [ ! -f "$bitcoin_conf" ]; then
touch "$bitcoin_conf"
reset_conf="Y"
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"
printf "Would you like to reset bitcoin.conf to its default settings? (Y/n): "
read -r reset_conf_input
reset_conf=${reset_conf_input:-N}
fi
# Ask for the new directory
echo "Enter the new haven for thy Bitcoin data:"
read new_directory
# Helper function to modify bitcoin.conf using awk
modify_bitcoin_conf() {
local key=$1
local value=$2
awk -v key="$key" -v value="$value" '!/^#/ && $1==key {found=1; $3=value} 1; END {if (!found) print key"="value}' "$bitcoin_conf" > "$bitcoin_conf.tmp" && mv "$bitcoin_conf.tmp" "$bitcoin_conf" || retry_with_sudo awk -v key="$key" -v value="$value" '!/^#/ && $1==key {found=1; $3=value} 1; END {if (!found) print key"="value}' "$bitcoin_conf" | sudo tee "$bitcoin_conf" >/dev/null
}
# Create the new directory if it does not exist
if [ ! -d "$new_directory" ]; then
mkdir "$new_directory" || retry_with_sudo mkdir "$new_directory"
# Apply default settings if the file was newly created or if the user chose to reset it.
if [ "$reset_conf" = "Y" ]; then
modify_bitcoin_conf "proxy" "127.0.0.1:9050"
modify_bitcoin_conf "listen" "1"
modify_bitcoin_conf "bind" "127.0.0.1"
modify_bitcoin_conf "disablewallet" "1"
modify_bitcoin_conf "zmqpubrawblock" "tcp://127.0.0.1:28332"
modify_bitcoin_conf "zmqpubrawtx" "tcp://127.0.0.1:28333"
modify_bitcoin_conf "BTC_LOGIN"
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
# Storage amount and pruning question
printf "Wouldst thou like to operate bitcoin in pruned mode? (Y/n): "
read -r prune
prune=${prune:-N}
if [ "$prune" = "Y" ]; then
modify_bitcoin_conf "prune" "555"
else
echo "Creating archival node, good choice."
modify_bitcoin_conf "prune" "0"
fi
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
# Maximum memory
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"
# 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; }
# Network selection
printf "Art thou operating on the main Bitcoin network? (Y/n): "
read -r mainnet
mainnet=${mainnet:-Y}
if [ "$mainnet" = "Y" ]; then
modify_bitcoin_conf "testnet" "0"
else
modify_bitcoin_conf "testnet" "1"
fi
echo "The Bitcoin data chamber now resides in: $new_directory"
echo "Thy Bitcoin configurations have been set. Fare thee well."
Loading…
Cancel
Save