#!/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
}
# Default Bitcoin configuration
bitcoin_conf="$HOME/.bitcoin/bitcoin.conf"
default_conf="proxy=127.0.0.1:9050\nlisten=1\nbind=127.0.0.1\ndisablewallet=1\nzmqpubrawblock=tcp://127.0.0.1:28332\nzmqpubrawtx=tcp://127.0.0.1:28333\nBTC_LOGIN"
# Helper function to modify bitcoin.conf using awk
modify_bitcoin_conf() {
local key=$1
local value=$2
awk -v key="$key" -v value="$value" 'BEGIN{FS=OFS="="} !/^#/ && $1==key {$2=value; found=1} 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" 'BEGIN{FS=OFS="="} !/^#/ && $1==key {$2=value; found=1} 1; END {if (!found) print key"="value}' "$bitcoin_conf" | sudo tee "$bitcoin_conf" >/dev/null
}
# Check for the bitcoin.conf file
if [ ! -f "$bitcoin_conf" ]; then
echo "Creating new bitcoin.conf at the default location with default settings..."
mkdir -p "$HOME/.bitcoin"
echo -e "$default_conf" > "$bitcoin_conf"
else
printf "Found existing bitcoin.conf at $bitcoin_conf:\n\n"
cat "$bitcoin_conf"
printf "\n"
printf "Would you like to reset it to default settings? (Y/n): "
read -r reset_conf
reset_conf=${reset_conf:-Y}
if [ "$reset_conf" = "Y" ]; then
echo "Resetting bitcoin.conf to default settings..."
echo -e "$default_conf" > "$bitcoin_conf"
fi
fi
# Pruning
printf "Wouldst thou like to enable pruning? Pruning allows thee to reduce disk usage by discarding some transaction history. (Y/n): "
read -r enable_pruning
enable_pruning=${enable_pruning:-Y}
if [ "$enable_pruning" = "Y" ]; then
modify_bitcoin_conf "prune" "1"
else
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"
# Data directory
printf "Wouldst thou like to store the blockchain in the default location ($HOME/.bitcoin)? (Y/n): "
read -r default_datadir
default_datadir=${default_datadir:-Y}
if [ "$default_datadir" = "N" ]; then
printf "Enter the path to thy desired data directory: "
read -r datadir
modify_bitcoin_conf "datadir" "$datadir"
fi
# 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."