Browse Source

revised configure-bitcoin

main
deicidus 2 years ago
parent
commit
30c3378066
  1. 107
      spells/menu/configure-bitcoin

107
spells/menu/configure-bitcoin

@ -1,8 +1,8 @@
#!/bin/sh #!/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() { retry_with_sudo() {
echo "Permission denied. Invoking the power of the super user..." echo "Permission denied. Invoking the power of the super user..."
sudo "$@" sudo "$@"
@ -12,67 +12,64 @@ retry_with_sudo() {
fi fi
} }
# Check for the bitcoin.conf file # Create .bitcoin directory if it does not exist
if [ ! -f "$HOME/.bitcoin/bitcoin.conf" ]; then mkdir -p "$HOME/.bitcoin"
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" bitcoin_conf="$HOME/.bitcoin/bitcoin.conf"
fi reset_conf="N"
# Get the current Bitcoin directory # If bitcoin.conf doesn't exist, create it. If it does exist, ask to reset it.
current_directory=$(grep "datadir" "$bitcoin_conf" | cut -d'=' -f2) if [ ! -f "$bitcoin_conf" ]; then
if [ -z "$current_directory" ]; then touch "$bitcoin_conf"
current_directory="$HOME/.bitcoin" reset_conf="Y"
else else
# Confirm that the directory actually exists printf "Would you like to reset bitcoin.conf to its default settings? (Y/n): "
if [ ! -d "$current_directory" ]; then read -r reset_conf_input
echo "The Bitcoin data chamber specified in bitcoin.conf doth not exist. Please ensure the correctness of the 'datadir' value in bitcoin.conf." reset_conf=${reset_conf_input:-N}
exit 1
fi
echo "Verified the existence of the Bitcoin data chamber at: $current_directory"
fi fi
# Ask for the new directory # Helper function to modify bitcoin.conf using awk
echo "Enter the new haven for thy Bitcoin data:" modify_bitcoin_conf() {
read new_directory 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 # Apply default settings if the file was newly created or if the user chose to reset it.
if [ ! -d "$new_directory" ]; then if [ "$reset_conf" = "Y" ]; then
mkdir "$new_directory" || retry_with_sudo mkdir "$new_directory" 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 fi
# Ask user what to move # Storage amount and pruning question
echo "What wouldst thou like to move to the new Bitcoin data haven?" printf "Wouldst thou like to operate bitcoin in pruned mode? (Y/n): "
echo "1. The entire Bitcoin data chamber" read -r prune
echo "2. Only the blockchain data" prune=${prune:-N}
echo "3. Move naught" if [ "$prune" = "Y" ]; then
modify_bitcoin_conf "prune" "555"
else
echo "Creating archival node, good choice."
modify_bitcoin_conf "prune" "0"
fi
read move_choice # 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"
case "$move_choice" in # Network selection
1) printf "Art thou operating on the main Bitcoin network? (Y/n): "
echo "Moving the entire Bitcoin data chamber..." read -r mainnet
mv "$current_directory"/* "$new_directory" || retry_with_sudo mv "$current_directory"/* "$new_directory" mainnet=${mainnet:-Y}
;; if [ "$mainnet" = "Y" ]; then
2) modify_bitcoin_conf "testnet" "0"
echo "Moving only the blockchain data..." else
mv "$current_directory/blocks" "$current_directory/chainstate" "$new_directory" || retry_with_sudo mv "$current_directory/blocks" "$current_directory/chainstate" "$new_directory" modify_bitcoin_conf "testnet" "1"
;; fi
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" echo "Thy Bitcoin configurations have been set. Fare thee well."
Loading…
Cancel
Save