#!/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 } # Handle Ctrl-C #trap "printf '\n'; exit 2" 2 # 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" 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 ask_yn "Would you like to reset the bitcoin.conf file to defaults?" "n" reset_conf=$? if [ "$reset_conf" = 0 ]; 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 ask_yn "Wouldst thou like to enable pruning? Pruning allows thee to reduce disk usage by discarding some transaction history." "y" prune=$? if [ "$prune" = 0 ]; then modify_bitcoin_conf "prune" "1" valid_number=false while [ "$valid_number" = false ]; do printf "What is the maximum disk space thou art willing to dedicate to Bitcoin in GB? (Type a number) [default: 500]: " read -r max_disk if [ -z "$max_disk" ]; then max_disk=500 valid_number=true elif validate_number "$max_disk"; then valid_number=true else printf "Invalid input. Please enter a valid number.\n" fi done # Convert GB to MB max_mem=$((max_disk * 1024)) modify_bitcoin_conf "dbcache" "$max_mem" else modify_bitcoin_conf "prune" "0" fi # Network selection ask_yn "Art thou operating on the main Bitcoin network?" "y" mainnet=$? if [ "$mainnet" = 0 ]; 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" ask_yn "Wouldst thou like to store the blockchain in the default location ($default_data_dir)?" "y" alt_storage=$? if [ "$alt_storage" = 1 ]; then while true; do printf "Please type the alternate path thou wouldst like to use for blockchain storage: " read -r alt_path if validate_path "$alt_path"; then break else printf "That's not a valid UNIX path string. " fi done modify_bitcoin_conf "datadir" "$alt_path" fi printf "Bitcoin configuration has been updated.\n"