#!/bin/bash
#This is a script to install Bitcoin Core

#Global Functions.
is_package_installed() {
    if dpkg -l "$1" 2>/dev/null | grep -q "^ii"; then
        echo "$1 is installed."
        return 0 # Package is installed
    else
        echo "$1 is not installed."
        return 1 # Package is not installed
    fi
}
prompt_yes_no() {
    local question="$1"
    local default_choice="${2:-yes}"

    while true; do
        read -p "$question (y/n) [default: $default_choice]: " user_choice
        case $user_choice in
        [Yy])
            echo "yes"
            return
            ;;
        [Nn])
            echo "no"
            return
            ;;
        "")
            # If the user just presses Enter, return the default choice
            echo "$default_choice"
            return
            ;;
        *)
            echo "Invalid choice. Please enter 'y' for yes or 'n' for no."
            ;;
        esac
    done
}

# Network stuff

# Installs TOR
install_tor() {
    # Check if TOR is already installed
    if is_package_installed "tor"; then
        echo "TOR is already installed. Moving on..."
        sleep 1
    else
        # Confirm TOR installation
        if [ "$(prompt_yes_no 'Do you want to install TOR?')" == "yes" ]; then
            echo "Adding the TOR repository..."
            sleep 0.5
            if ! echo "deb https://deb.torproject.org/torproject.org $(lsb_release -cs) main" >>/etc/apt/sources.list.d/tor.list; then
                echo "Failed to add the TOR repository." >&2
                exit 1
            fi

            echo "Importing the TOR project's GPG key..."
            sleep 1
            if ! wget -qO - https://deb.torproject.org/torproject.org/A3C4F0F979CAA22CDBA8F512EE8CBC9E886DDD89.asc | gpg --dearmor --output /etc/apt/trusted.gpg.d/tor.gpg; then
                echo "Failed to import the GPG key for TOR." >&2
                exit 1
            fi

            echo "Updating package lists...half a sec."
            sleep 0.5
            if ! apt-get update -qq; then
                echo "Failed to update package lists with the new repository." >&2
                exit 1
            fi

            echo "Installing TOR. Hodl tight..."
            sleep 1
            if ! apt-get install -y -qq tor; then
                echo "Failed to install TOR." >&2
                exit 1
            fi

            echo "Installing additional dependencies for TOR..."
            sleep 1
            if ! apt-get install -y -qq torsocks tor-geoipdb; then
                echo "Failed to install additional dependencies for TOR." >&2
                exit 1
            fi

            echo "Adding the user 'bitcoin' to the 'debian-tor' group to allow TOR access..."
            sleep 1
            sudo groupadd -f debian-tor
            sudo usermod -a -G debian-tor bitcoin
            echo "Setting correct permissions for the TOR configuration directory..."
            sleep 1
            if ! chown -R debian-tor:debian-tor /var/lib/tor; then
                echo "Failed to set permissions for the TOR configuration directory." >&2
                exit 1
            fi

            echo "Modifying the TOR config file (torrc) to work with core...."
            sleep 1
            if ! echo -e "ControlPort 9051\nCookieAuthentication 1\nCookieAuthFileGroupReadable 1\nLog notice stdout\nSOCKSPort 9050" >>/etc/tor/torrc; then
                echo "Failed to custom configurations to torrc file." >&2
                exit 1
            fi

            echo "Restarting TOR so the changes take effect..."
            sleep 1
            systemctl restart tor
            echo "TOR has been installed and configured for core to use. Moving on..."
            sleep 3
        else
            echo "TOR installation skipped. Moving on..." >&2
            sleep 2
        fi
    fi
}

# Installs I2P
install_i2p() {
    # Check if I2P is already installed
    if is_package_installed "i2pd"; then
        echo "I2P is already installed."
        return
    fi

    # Confirm I2P installation
    if [ "$(prompt_yes_no 'Do you want to install I2P?')" == "yes" ]; then
        # Ensure apt-transport-https is installed before adding repositories
        if ! is_package_installed "apt-transport-https"; then
            echo "Installing apt-transport-https..."
            if ! apt-get install -y -qq apt-transport-https; then
                echo "Failed to install apt-transport-https." >&2
                exit 1
            fi
        else
            echo "apt-transport-https is already installed."
        fi

        # Adding I2P repository
        echo "Adding I2P repository..."
        if ! wget -qq -O - https://repo.i2pd.xyz/.help/add_repo | sudo bash -s -; then
            echo "Failed to add the I2P repository." >&2
            exit 1
        fi

        # Updating package lists
        echo "Updating package lists..."
        if ! apt-get update -qq; then
            echo "Failed to update package lists." >&2
            exit 1
        fi

        # Installing I2P
        echo "Installing I2P..."
        if ! apt-get install -y -qq i2pd; then
            echo "Failed to install I2P." >&2
            exit 1
        fi

        # Enabling and starting the I2P service
        echo "Enabling the I2P service to start on boot..."
        if ! systemctl enable i2pd; then
            echo "Failed to enable the I2P service to start on boot." >&2
            exit 1
        fi

        echo "Starting the I2P service..."
        if ! systemctl start i2pd; then
            echo "Failed to start the I2P service." >&2
            exit 1
        fi

        echo "I2P has been installed and is configured to start on boot. Moving on..."
    else
        echo "I2P installation skipped. Moving on..."
    fi
    sleep 3
}

# Bitocin Stuff

# Checks if Bitcoin Core is installed
is_bitcoin_core_installed() {
    command -v bitcoind &>/dev/null
}

# Installs Core's dependencies
install_bitcoin_core_dependencies() {
    echo "Checking if Core is already installed..."
    if is_bitcoin_core_installed; then
        echo "Core is already installed. Skipping to config."
        sleep 2
        return
    else
        echo "Bitcoin Core dependencies not found. No worries..."
    fi

    echo "Installing required repositories for Core..."
    sleep 3
    if ! is_package_installed "git"; then
        echo "Installing git..."
        if ! apt-get install -y -qq git; then
            echo "Failed to install git." >&2
            exit 1
        fi
    else
        echo "git is already installed."
    fi

    if ! is_package_installed "curl"; then
        echo "Installing curl..."
        if ! apt-get install -y -qq curl; then
            echo "Failed to install curl." >&2
            exit 1
        fi
    else
        echo "curl is already installed."
    fi

    local bitcoin_core_dependencies=("build-essential" "libtool" "autotools-dev" "automake" "pkg-config" "bsdmainutils" "python3" "libssl-dev" "libevent-dev" "libboost-system-dev" "libboost-filesystem-dev" "libboost-test-dev" "libboost-thread-dev" "libboost-all-dev" "libzmq3-dev")

    # Iterate through the dependencies and install them
    for dep in "${bitcoin_core_dependencies[@]}"; do
        if ! is_package_installed "$dep"; then
            echo "Installing $dep..."
            if ! apt-get install -y -qq "$dep"; then
                echo "Failed to install $dep." >&2
                exit 1
            fi
        else
            echo "$dep is already installed."
        fi
    done

    echo "Core dependencies have been successfully installed. Moving on..."
    sleep 2
}

# Download and install Bitcoin Core
download_and_install_bitcoin_core() {
    local node_folder="/home/bitcoin/node"

    # Create the node folder if it doesn't exist
    echo "Creating the node folder..."
    sleep 1
    mkdir -p "$node_folder"

    # Fetch the latest version number from the GitHub API
    echo "Fetching the latest version of Bitcoin Core..."
    sleep 1
    latest_version=$(curl -s https://api.github.com/repos/bitcoin/bitcoin/releases/latest | jq -r '.tag_name' | sed 's/^v//')

    if [[ -z "$latest_version" ]]; then
        echo "Failed to fetch the latest version of Bitcoin Core. Aborting the installation." >&2
        exit 1
    fi

    # Download the Bitcoin Core source code as a .tar.gz file
    echo "Downloading Bitcoin Core source code..."
    sleep 1
    local bitcoin_core_url="https://bitcoincore.org/bin/bitcoin-core-${latest_version}/bitcoin-${latest_version}.tar.gz"
    local source_code_archive="${node_folder}/bitcoin-$latest_version.tar.gz"

    if ! wget -nc -q "$bitcoin_core_url" -P "$node_folder"; then
        echo "ERROR: Failed to download the Bitcoin Core source code." >&2
        exit 1
    fi

    # Download the SHA256SUMS file
    echo "Downloading Bitcoin Core SHA256SUMS file..."
    sleep 2
    local sha256sums_url="https://bitcoincore.org/bin/bitcoin-core-$latest_version/SHA256SUMS"
    local sha256sums_file="${node_folder}/SHA256SUMS"

    if ! wget -nc -q "$sha256sums_url" -O "$sha256sums_file"; then
        echo "ERROR: Failed to download the SHA256SUMS file." >&2
        exit 1
    fi

    # Verify the cryptographic checksum of the downloaded .tar.gz file
    verify_checksum "$source_code_archive" "$latest_version"

    # Extract the downloaded source code
    echo "Extracting Bitcoin Core source code..."
    sleep 1
    tar -xzf "$source_code_archive" -C "$node_folder"

    # Navigate into the Bitcoin Core directory
    echo "Entering the Bitcoin Core directory so it can be compiled..."
    sleep 1
    if ! cd "$node_folder/bitcoin-$latest_version"; then
        echo "Failed to enter the Bitcoin Core directory. Aborting the installation." >&2
        exit 1
    fi

    # Build and install Bitcoin Core
    echo "Building Bitcoin Core. This is compiling Bitcoin core from scratch. It will take a while, so go touch some grass or something."
    echo "I'm going to let the output run here so you can see progress being made. Starting in a sec."
    sleep 3
    ./autogen.sh
    ./configure
    make

    # Check if 'make install' was successful
    if ! make install; then
        echo "Failed to install Bitcoin Core. Aborting the installation." >&2
        exit 1
    fi

    echo "Bitcoin Core compiled successfully! Woo! Moving on to configuration"
    sleep 3
}

# Verifies the cryptographic checksum of a file
verify_checksum() {
    local file="$1"
    local expected_checksum="$2"
    local current_dir="$(pwd)" # Store the current directory

    # Change directory to the folder where the file is located
    local file_dir="$(dirname "$file")"
    cd "$file_dir"

    # Download the SHA256SUMS file without renaming
    echo "Verifying the cryptographic checksum..."
    sleep 1

    if sha256sum --ignore-missing --check SHA256SUMS; then
        echo -e "\e[32m+--------------------------------+\e[0m"
        echo -e "\e[32m|  \e[1mVerification Successful!\e[0m \e[32m |\e[0m"
        echo -e "\e[32m+--------------------------------+\e[0m"
        sleep 3
    else
        echo -e "\e[1;31mERROR: Cryptographic checksum verification failed.\e[0m" >&2
        exit 1
    fi

    # Return to the original directory
    cd "$current_dir"
}

# Copies Core's binary to /usr/local/bin and checks permissions
copy_bitcoin_core_binary() {
    local node_folder="/home/bitcoin/node"
    local latest_version="$1"
    local expected_location="/usr/local/bin"
    if [ -x "${expected_location}/bitcoind" ] && [ -x "${expected_location}/bitcoin-cli" ]; then
        echo "Bitcoin Core binaries are already installed in ${expected_location}. Skipping copying and permissions check."
        return
    fi
    echo "Copying Bitcoin Core binary to ${expected_location}..."
    sleep 1
    cp "$node_folder/bitcoin-$latest_version/src/bitcoind" "${expected_location}"
    cp "$node_folder/bitcoin-$latest_version/src/bitcoin-cli" "${expected_location}"
    chown bitcoin:bitcoin "${expected_location}/bitcoind"
    chown bitcoin:bitcoin "${expected_location}/bitcoin-cli"
    chmod 755 "${expected_location}/bitcoind"
    chmod 755 "${expected_location}/bitcoin-cli"
    echo "Bitcoin Core binary has been copied to ${expected_location} and proper permissions have been set."
    sleep 1
}

create_bitcoin_conf() {
    local bitcoin_conf_dir="/home/bitcoin/.bitcoin"
    local bitcoin_conf_file="$bitcoin_conf_dir/bitcoin.conf"
    local network="$1"

    # Ensure that the /home/bitcoin/.bitcoin directory exists
    if [ ! -d "$bitcoin_conf_dir" ]; then
        echo "Creating $bitcoin_conf_dir directory..."
        mkdir -p "$bitcoin_conf_dir" || {
            echo "Failed to create directory $bitcoin_conf_dir"
            exit 1
        }
        chown bitcoin:bitcoin "$bitcoin_conf_dir" || {
            echo "Failed to change ownership of $bitcoin_conf_dir" >&2
            exit 1
        }
        chmod 700 "$bitcoin_conf_dir" || {
            echo "Failed to set permissions for $bitcoin_conf_dir" >&2
            exit 1
        }
        echo "$bitcoin_conf_dir directory created successfully."
    fi

    # Create or modify the bitcoin.conf file
    if [ ! -f "$bitcoin_conf_file" ]; then
        echo "Creating $bitcoin_conf_file with a base configuration..."
        # Base Configuration (Hybrid Mode)
        cat >"$bitcoin_conf_file" <<EOF
                # Base Configuration
                datadir=/home/bitcoin/.bitcoin
                rpccookiefile=/home/bitcoin/.bitcoin/.cookie
                coinstatsindex=1
                daemon=1
                server=1
                rpcbind=127.0.0.1
                rpcport=8332
                rpcallowip=127.0.0.1
                rpcallowip=::1
                rpcuser=bitcoin
                rpcpassword=craigwrightisnotsatoshi
                daemonwait=1
                dbcache=600
                maxmempool=500
                txindex=1
                permitbaremultisig=0
EOF
        chown bitcoin:bitcoin "$bitcoin_conf_file"
        chmod 600 "$bitcoin_conf_file"
        echo "Base configuration file created."
    fi

    # Additional configuration based on network mode
    case "$network" in
    "tor")
        # TOR configuration
        cat <<EOF >>"$bitcoin_conf_file"
                # [Network]
                debug=tor
                proxy=127.0.0.1:9050
                onlynet=onion
EOF
        ;;
    "tor_hybrid")
        # TOR hybrid configuration
        cat <<EOF >>"$bitcoin_conf_file"
                # [Network]
                debug=tor
                proxy=127.0.0.1:9050
EOF
        ;;
    "i2p")
        # I2P configuration
        cat <<EOF >>"$bitcoin_conf_file"
                # [Network]
                debug=i2p
                i2psam=127.0.0.1:7656
                onlynet=i2p
                addnode=255fhcp6ajvftnyo7bwz3an3t4a4brhopm3bamyh2iu5r3gnr2rq.b32.i2p:0
                addnode=27yrtht5b5bzom2w5ajb27najuqvuydtzb7bavlak25wkufec5mq.b32.i2p:0
                addnode=2el6enckmfyiwbfcwsygkwksovtynzsigmyv3bzyk7j7qqahooua.b32.i2p:0
                addnode=3gocb7wc4zvbmmebktet7gujccuux4ifk3kqilnxnj5wpdpqx2hq.b32.i2p:0
                addnode=3tns2oov4tnllntotazy6umzkq4fhkco3iu5rnkxtu3pbfzxda7q.b32.i2p:0
                addnode=4fcc23wt3hyjk3csfzcdyjz5pcwg5dzhdqgma6bch2qyiakcbboa.b32.i2p:0
                addnode=4osyqeknhx5qf3a73jeimexwclmt42cju6xdp7icja4ixxguu2hq.b32.i2p:0
                addnode=4umsi4nlmgyp4rckosg4vegd2ysljvid47zu7pqsollkaszcbpqq.b32.i2p:0
                addnode=52v6uo6crlrlhzphslyiqblirux6olgsaa45ixih7sq5np4jujaa.b32.i2p:0
                addnode=6j2ezegd3e2e2x3o3pox335f5vxfthrrigkdrbgfbdjchm5h4awa.b32.i2p:0
EOF
        ;;
    "i2p_hybrid")
        # I2P hybrid configuration
        cat <<EOF >>"$bitcoin_conf_file"
                # [Network]
                debug=i2p
                i2psam=127.0.0.1:7656
                addnode=255fhcp6ajvftnyo7bwz3an3t4a4brhopm3bamyh2iu5r3gnr2rq.b32.i2p:0
                addnode=27yrtht5b5bzom2w5ajb27najuqvuydtzb7bavlak25wkufec5mq.b32.i2p:0
                addnode=2el6enckmfyiwbfcwsygkwksovtynzsigmyv3bzyk7j7qqahooua.b32.i2p:0
                addnode=3gocb7wc4zvbmmebktet7gujccuux4ifk3kqilnxnj5wpdpqx2hq.b32.i2p:0
                addnode=3tns2oov4tnllntotazy6umzkq4fhkco3iu5rnkxtu3pbfzxda7q.b32.i2p:0
                addnode=4fcc23wt3hyjk3csfzcdyjz5pcwg5dzhdqgma6bch2qyiakcbboa.b32.i2p:0
                addnode=4osyqeknhx5qf3a73jeimexwclmt42cju6xdp7icja4ixxguu2hq.b32.i2p:0
                addnode=4umsi4nlmgyp4rckosg4vegd2ysljvid47zu7pqsollkaszcbpqq.b32.i2p:0
                addnode=52v6uo6crlrlhzphslyiqblirux6olgsaa45ixih7sq5np4jujaa.b32.i2p:0
                addnode=6j2ezegd3e2e2x3o3pox335f5vxfthrrigkdrbgfbdjchm5h4awa.b32.i2p:0
EOF
        ;;
    "both")
        # TOR and I2P configuration
        cat <<EOF >>"$bitcoin_conf_file"
                # [Network]
                debug=tor
                debug=i2p
                proxy=127.0.0.1:9050
                i2psam=127.0.0.1:7656
                onlynet=onion
                onlynet=i2p
                addnode=255fhcp6ajvftnyo7bwz3an3t4a4brhopm3bamyh2iu5r3gnr2rq.b32.i2p:0
                addnode=27yrtht5b5bzom2w5ajb27najuqvuydtzb7bavlak25wkufec5mq.b32.i2p:0
                addnode=2el6enckmfyiwbfcwsygkwksovtynzsigmyv3bzyk7j7qqahooua.b32.i2p:0
                addnode=3gocb7wc4zvbmmebktet7gujccuux4ifk3kqilnxnj5wpdpqx2hq.b32.i2p:0
                addnode=3tns2oov4tnllntotazy6umzkq4fhkco3iu5rnkxtu3pbfzxda7q.b32.i2p:0
                addnode=4fcc23wt3hyjk3csfzcdyjz5pcwg5dzhdqgma6bch2qyiakcbboa.b32.i2p:0
                addnode=4osyqeknhx5qf3a73jeimexwclmt42cju6xdp7icja4ixxguu2hq.b32.i2p:0
                addnode=4umsi4nlmgyp4rckosg4vegd2ysljvid47zu7pqsollkaszcbpqq.b32.i2p:0
                addnode=52v6uo6crlrlhzphslyiqblirux6olgsaa45ixih7sq5np4jujaa.b32.i2p:0
                addnode=6j2ezegd3e2e2x3o3pox335f5vxfthrrigkdrbgfbdjchm5h4awa.b32.i2p:0
EOF
        ;;
    "both_hybrid")
        # TOR and I2P hybrid configuration
        cat <<EOF >>"$bitcoin_conf_file"
                # [Network]
                debug=tor
                debug=i2p
                proxy=127.0.0.1:9050
                i2psam=127.0.0.1:7656
                addnode=255fhcp6ajvftnyo7bwz3an3t4a4brhopm3bamyh2iu5r3gnr2rq.b32.i2p:0
                addnode=27yrtht5b5bzom2w5ajb27najuqvuydtzb7bavlak25wkufec5mq.b32.i2p:0
                addnode=2el6enckmfyiwbfcwsygkwksovtynzsigmyv3bzyk7j7qqahooua.b32.i2p:0
                addnode=3gocb7wc4zvbmmebktet7gujccuux4ifk3kqilnxnj5wpdpqx2hq.b32.i2p:0
                addnode=3tns2oov4tnllntotazy6umzkq4fhkco3iu5rnkxtu3pbfzxda7q.b32.i2p:0
                addnode=4fcc23wt3hyjk3csfzcdyjz5pcwg5dzhdqgma6bch2qyiakcbboa.b32.i2p:0
                addnode=4osyqeknhx5qf3a73jeimexwclmt42cju6xdp7icja4ixxguu2hq.b32.i2p:0
                addnode=4umsi4nlmgyp4rckosg4vegd2ysljvid47zu7pqsollkaszcbpqq.b32.i2p:0
                addnode=52v6uo6crlrlhzphslyiqblirux6olgsaa45ixih7sq5np4jujaa.b32.i2p:0
                addnode=6j2ezegd3e2e2x3o3pox335f5vxfthrrigkdrbgfbdjchm5h4awa.b32.i2p:0
EOF
        ;;
    esac
    echo "Bitcoin Core configuration updated for $network mode."
    sleep 3
}

# Plugs Core into systemd
create_bitcoin_core_service() {
    local service_file="/etc/systemd/system/bitcoind.service"
    cat <<EOF >"$service_file"
            [Unit]
            Description=Bitcoin core daemon

            After=network-online.target
            Wants=network-online.target

            [Service]
            User=bitcoin
            Group=bitcoin
            Type=forking
            ExecStart=/usr/local/bin/bitcoind -conf=/home/bitcoin/.bitcoin/bitcoin.conf
            Restart=always
            PrivateTmp=true
            TimeoutStopSec=480s
            TimeoutStartSec=480s
            StartLimitInterval=600s
            StartLimitBurst=5

            [Install]
            WantedBy=multi-user.target
EOF
    echo "Reloading systemd so it can recognize the new service..."
    sleep 1
    if ! systemctl daemon-reload; then
        echo "Error: Failed to reload systemd. Please check systemd." >&2
        exit 1
    fi
}

# Starts and enables Bitcoin Core
start_and_enable_bitcoin_core() {   
    echo "Final permissions check..."
    sudo chown -R bitcoin:bitcoin /home/bitcoin
    sudo chmod -R u+rw,g+rw,o-rwx /home/bitcoin
    sudo chmod u+x /usr/local/bin/bitcoind
    sudo chmod u+x /usr/local/bin/bitcoin-cli
    sudo systemctl enable bitcoind
    sudo systemctl start bitcoind
    echo "Bitcoin Core has been started and added to systemd."
    echo "Now Bitcoin Core will start on boot."
    sleep 3
}

# Final systems check and exit.
check_services() {
    echo "Final check the make sure everything is working..."
    if sudo systemctl is-active --quiet bitcoind; then
        echo "Woot! Core is up and running. Looks like your good to go."
    else
        echo "It looks like something did not work bitcoind is not running."
        echo "Check debug.log in /home/bitcoin/.bitcoin to see logs."
        exit 1
    fi
}

# Main script starts here

# Root Check
if [ "$EUID" -ne 0 ]; then
    echo "Please run this script as root. Run wih sudo or use 'su' log into your admin account."
    exit 1
fi

# Welcome Message
cat <<"EOF"
!   .::::::.     ...     :::      .::.:::::::..     :::.     :::.    :::.    :::.    :::.    ...     :::::::-.  .,::::::      ::::::::::.   :::.       .,-:::::   :::  .   
!  ;;;`    `  .;;;;;;;.  ';;,   ,;;;' ;;;;``;;;;    ;;`;;    `;;;;,  `;;;    `;;;;,  `;;; .;;;;;;;.   ;;,   `';,;;;;''''       `;;;```.;;;  ;;`;;    ,;;;'````'   ;;; .;;,.
!  '[==/[[[[,,[[     \[[, \[[  .[[/    [[[,/[[['   ,[[ '[[,    [[[[[. '[[      [[[[[. '[[,[[     \[[, `[[     [[ [[cccc         `]]nnn]]'  ,[[ '[[,  [[[          [[[[[/'  
!    '''    $$$$,     $$$  Y$c.$$"     $$$$$$c    c$$$cc$$$c   $$$ "Y$c$$      $$$ "Y$c$$$$$,     $$$  $$,    $$ $$""""          $$$""    c$$$cc$$$c $$$         _$$$$,    
!   88b    dP"888,_ _,88P   Y88P       888b "88bo, 888   888,  888    Y88      888    Y88"888,_ _,88P  888_,o8P' 888oo,__        888o      888   888,`88bo,__,o, "888"88o, 
!    "YMmMY"   "YMMMMMP"     MP        MMMM   "W"  YMM   ""`   MMM     YM      MMM     YM  "YMMMMMP"   MMMMP"`   """"YUMMM       YMMMb     YMM   ""`   "YUMMMMMP" MMM "MMP"
EOF

echo "Thanks for using Enki's Bitcoin Core install script"
echo "This is part of a set of scripts that help set up various parts of a bitcoin node."
echo "This script will install TOR, I2P and Bitcoin Core on your computer."
echo "It will also help you configure core to use whatever network combo you need."
if [ -t 0 ]; then
    echo "To continue, hit any key."
    read -n 1 -s -r -p ""
fi

echo "Making a log file."
echo "Find the log file in the same directory that this script is running."
sleep 2 
log_file="install_log.txt"
exec > >(tee -a "$log_file") 2>&1

# Check if the user bitcoin already exists and make it if not.
if id "bitcoin" &>/dev/null; then
    echo "User 'bitcoin' already exists." 
    echo "No need to make a user, Skipping..."
    sleep 1
else
    # Create a new user named "bitcoin" and set the password
    echo "Creating a user called bitcoin..."
    sleep 1
    if sudo adduser --disabled-password --gecos "" bitcoin; then
        echo "User 'bitcoin' created successfully."
        echo "Please set the password for the 'bitcoin' user. This will let you log in later."
        if sudo passwd bitcoin; then
            echo "Password for 'bitcoin' set successfully."
        else
            echo "Failed to set the password for 'bitcoin'." >&2
            exit 1
        fi

    else
        echo "Failed to create the 'bitcoin' user." >&2
        exit 1
    fi
fi

# Ensure that /home/bitcoin directory exists and set proper permissions
bitcoin_home="/home/bitcoin"
if [ ! -d "$bitcoin_home" ]; then
    echo "Creating /home/bitcoin directory..."
    if mkdir -p "$bitcoin_home"; then
        echo "Directory /home/bitcoin created successfully."
        echo "Setting ownership and permissions for /home/bitcoin..."
        if chown -R bitcoin:bitcoin "$bitcoin_home"; then
            echo "Ownership and permissions set successfully."
            if chmod 700 "$bitcoin_home"; then
                echo "Permissions set successfully."
            else
                echo "Failed to set permissions for $bitcoin_home." >&2
                exit 1
            fi

        else
            echo "Failed to set ownership for $bitcoin_home." >&2
            exit 1
        fi
    else
        echo "Failed to create directory /home/bitcoin." >&2
        exit 1
    fi
fi

install_tor
install_i2p

# Check if TOR and I2P services are active
use_tor="no"
use_i2p="no"

if systemctl is-active --quiet tor; then
    use_tor="yes"
    echo "TOR is running."
else
    echo "TOR is not running."
fi

if systemctl is-active --quiet i2pd; then
    use_i2p="yes"
    echo "I2P is running."
else
    echo "I2P is not running."
fi

# Check if Bitcoin Core is already installed
if is_bitcoin_core_installed; then
    echo "Bitcoin Core is already installed. Moving on to configuration."
else
    echo "Bitcoin Core is not installed. Installing dependencies and Bitcoin Core"
    install_bitcoin_core_dependencies
    download_and_install_bitcoin_core
    copy_bitcoin_core_binary "$latest_version"
fi

# Prompt the user and configure Bitcoin Core based on installed services
network="hybrid" # Default to hybrid mode

if [ "$use_tor" == "yes" ] && [ "$use_i2p" == "yes" ]; then
    echo "Both TOR and I2P are installed. You can configure your Bitcoin Core to use:"
    echo "- Only TOR and I2P traffic (more private, slower IBD)"
    echo "- TOR, I2P, and IPV4/6 (normal traffic; less private, faster IBD)"
    read -r -p "Hit yes for TOR and I2P only, no for the hybrid configuration [y/n]: " use_both
    if [ "${use_both,,}" == "y" ]; then
        network="both"
    else
        network="both_hybrid"
    fi
elif [ "$use_tor" == "yes" ]; then
    echo "Only TOR is installed. You can configure your Bitcoin Core to use:"
    echo "- Only TOR traffic (more private, slower IBD)"
    echo "- TOR and IPV4/6 (normal traffic; less private, faster IBD)"
    read -r -p "Hit yes for TOR only, no for the hybrid configuration [y/n]: " tor_mode
    if [ "${tor_mode,,}" == "y" ]; then
        network="tor"
    else
        network="tor_hybrid"
    fi
elif [ "$use_i2p" == "yes" ]; then
    echo "Only I2P is installed. You can configure your Bitcoin Core to use:"
    echo "- Only I2P traffic (more private, slower IBD)"
    echo "- I2P and IPV4/6 (normal traffic; less private, faster IBD)"
    read -r -p "Hit yes for I2P only, no for the hybrid configuration [y/n]: " i2p_mode
    if [ "${i2p_mode,,}" == "y" ]; then
        network="i2p"
    else
        network="i2p_hybrid"
    fi
else
    echo "Neither TOR nor I2P is installed. Proceeding with default configuration."
fi

# Create or modify the bitcoin.conf file based on the network choice
create_bitcoin_conf "$network"

# Create systemd service unit for Bitcoin Core
create_bitcoin_core_service

# Reload the systemd daemon to recognize the new service
systemctl daemon-reload

# Start and enable Bitcoin Core service
start_and_enable_bitcoin_core

check_services # Final systems check and exit

# Inform the user that the script has completed successfully
echo "Thanks for running a Bitcoin full node, you're helping to decentralize the network even further!"
echo "You can check your node's sync status with 'bitcoin-cli -getinfo'"
echo "You can check the status of your peer connections with 'bitcoin-cli -netinfo'" 
echo "This will show the amount of peers you are connected to for each network."
echo "Your node is not configured for inbound connections. So don't be alarmed when it shows zero."
echo "Your default RPC password is 'craigwrightisnotsatoshi' you should change this."
echo "You can make a new hashed password here 'https://jlopp.github.io/bitcoin-core-rpc-auth-generator/'"
echo "Change it with 'nano /home/bitcoin/.bitcoin/bitcoin.conf' and look for the rpcauth option."
# Exit the script with a success status code
exit 0