Browse Source

revised configure-bitcoin and ask_yn

main
deicidus 1 year ago
parent
commit
5130628449
  1. 30
      spells/cantrips/ask_Yn
  2. 35
      spells/cantrips/ask_yn
  3. 99
      spells/menu/configure-bitcoin

30
spells/cantrips/ask_Yn

@ -1,30 +0,0 @@
#!/usr/bin/env sh
# Define ask_Yn, which asks a yes or no question and saves the result in the given variable name (defaults to yes)
# Takes one arg, which is the variable name where the result will be saved
# No is 0 and Yes is 1
source say
ask_Yn() {
if [ -n "$2" ]; then
say_inline "$2 (Y/n): "
fi
STTY_CONFIG=$(stty -g) # Save current stty config
stty raw -echo # Disable echo
ANSWER=$(head -c 1) # Scrape terminal input
stty $STTY_CONFIG # Restore stty config
if [ "$ANSWER" != "${ANSWER#[Nn]}" ]; then
say "N"
export "${1}=0"
else
say "Y"
export "${1}=1"
fi
}
# Check if the script is being called from another script
if [ "${BASH_SOURCE[0]}" = "$0" ]; then
# If not, call the function
ask_Yn "$@"
fi

35
spells/cantrips/ask_yn

@ -0,0 +1,35 @@
#!/bin/sh
# Define ask_yn, which asks a yes or no question and outputs the result as 1 for Yes and 0 for No.
# Takes two arguments:
# $1: The question to be asked.
# $2: The default answer, either "y" or "n".
ask_yn() {
question=$1
default_answer=$2
# Capitalize the default option correctly
if [ "$default_answer" = "n" ] || [ "$default_answer" = "N" ]; then
prompt="${question} (y/N): "
else
prompt="${question} (Y/n): "
fi
while true; do
printf "%s" "$prompt"
STTY_CONFIG=$(stty -g) # Save current stty config
stty raw -echo # Disable echo
ANSWER=$(head -c 1) # Scrape terminal input
stty $STTY_CONFIG # Restore stty config
case $ANSWER in
[Yy] ) echo "Y"; exit 1;; # Yes: exit with 1
[Nn] ) echo "N"; exit 0;; # No: exit with 0
"" ) if [ "$default_answer" = "n" ] || [ "$default_answer" = "N" ]; then echo "N"; exit 0; else echo "Y"; exit 1; fi;; # Default: exit with the default value
* ) printf "\nPlease answer Yes or No.\n";;
esac
done
}
ask_yn "$@"

99
spells/menu/configure-bitcoin

@ -1,6 +1,6 @@
#!/bin/sh
# This script facilitates the setting of the configurations for thy Bitcoin system.
# 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() {
@ -12,69 +12,74 @@ retry_with_sudo() {
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"
# Source ask_yn script
. ask_yn
# 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
}
# 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"
# 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"
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
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"
reset_conf=$(ask_yn "Would you like to reset the bitcoin.conf file to defaults?" "n")
if [ $reset_conf -eq 1 ]; then
echo "$defaults" > "$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
# 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"
}
# Maximum memory
# Storage amount and pruning question
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"
prune=$(ask_yn "Wouldst thou like to enable pruning? Pruning allows thee to reduce disk usage by discarding some transaction history." "y")
if [ $prune -eq 1 ]; then
modify_bitcoin_conf "prune" "1"
else
modify_bitcoin_conf "prune" "0"
fi
# Network selection
printf "Art thou operating on the main Bitcoin network? (Y/n): "
read -r mainnet
mainnet=${mainnet:-Y}
if [ "$mainnet" = "Y" ]; then
mainnet=$(ask_yn "Art thou operating on the main Bitcoin network?" "y")
if [ $mainnet -eq 1 ]; then
modify_bitcoin_conf "testnet" "0"
else
modify_bitcoin_conf "testnet" "1"
fi
echo "Thy Bitcoin configurations have been set. Fare thee well."
# 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 -eq 0 ]; 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
# Final confirmation and start
confirm=$(ask_yn "Dost thou wish to initiate Bitcoin with the specified configurations?" "y")
if [ $confirm -eq 1 ]; then
bitcoind -daemon
fi
Loading…
Cancel
Save