#!/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"