#!/bin/sh

# This script sets the correct permissions on the Bitcoin configuration directory.

# Set the default Bitcoin configuration directory
default_dir="$HOME/.bitcoin"

# Check if the default directory exists
if [ -d "$default_dir" ]; then
  bitcoin_dir="$default_dir"
else
  # Prompt the user to enter the path to the Bitcoin configuration directory
  read -p "Enter the path to the Bitcoin configuration directory: " bitcoin_dir
fi

# Check if the provided directory exists
if [ -d "$bitcoin_dir" ]; then
  # Set the default permission mode
  default_mode=710

  # Get the configuration directory mode from bitcoin.conf
  config_mode=$(grep -E '^ConfigurationDirectoryMode=' "$bitcoin_dir/bitcoin.conf" | cut -d '=' -f 2)

  # If ConfigurationDirectoryMode is not specified, use the default mode
  if [ -z "$config_mode" ]; then
    config_mode=$default_mode
  fi

  # Get the current permission mode of the directory
  current_mode=$(stat -c "%a" "$bitcoin_dir")

  # Compare the current mode with the configured mode
  if [ "$current_mode" != "$config_mode" ]; then
    # Fix the permission by setting the correct mode
    chmod "$config_mode" "$bitcoin_dir"
    echo "The permission on the Bitcoin configuration directory has been set to $config_mode."
  else
    echo "The Bitcoin configuration directory is already configured with the correct permissions."
  fi
else
  echo "The provided directory does not exist."
fi