#!/bin/sh
# This script 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 if it does not exist
mkdir -p "$HOME/.bitcoin"
bitcoin_conf="$HOME/.bitcoin/bitcoin.conf"
reset_conf="N"
# 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
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
# 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
}
# 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
# 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
# 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"
# 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 "Thy Bitcoin configurations have been set. Fare thee well."