|
| 1 | +#!/bin/bash |
| 2 | + |
| 3 | +# Function to print messages |
| 4 | +print_message() { |
| 5 | + MESSAGE=$1 |
| 6 | + echo "${MESSAGE}" |
| 7 | +} |
| 8 | + |
| 9 | +# Function to check the OS |
| 10 | +check_os() { |
| 11 | + if [[ "$OSTYPE" == "linux-gnu"* ]]; then |
| 12 | + OS="linux" |
| 13 | + elif [[ "$OSTYPE" == "darwin"* ]]; then |
| 14 | + OS="macos" |
| 15 | + else |
| 16 | + print_message "Unsupported OS: $OSTYPE" |
| 17 | + exit 1 |
| 18 | + fi |
| 19 | + print_message "Detected OS: $OS" |
| 20 | +} |
| 21 | + |
| 22 | +# Function to install a package if it's not already installed |
| 23 | +install_if_not_installed() { |
| 24 | + PACKAGE=$1 |
| 25 | + if [[ "$OS" == "linux" ]]; then |
| 26 | + if ! dpkg -l | grep -q "$PACKAGE"; then |
| 27 | + print_message "Installing $PACKAGE..." |
| 28 | + sudo apt-get install -y "$PACKAGE" > /dev/null 2>&1 |
| 29 | + else |
| 30 | + print_message "$PACKAGE is already installed." |
| 31 | + fi |
| 32 | + elif [[ "$OS" == "macos" ]]; then |
| 33 | + if ! brew list -1 | grep -q "$PACKAGE"; then |
| 34 | + print_message "Installing $PACKAGE..." |
| 35 | + brew install "$PACKAGE" > /dev/null 2>&1 |
| 36 | + else |
| 37 | + print_message "$PACKAGE is already installed." |
| 38 | + fi |
| 39 | + fi |
| 40 | +} |
| 41 | + |
| 42 | +# Function to install dependencies based on the OS |
| 43 | +install_dependencies() { |
| 44 | + if [[ "$OS" == "linux" ]]; then |
| 45 | + print_message "Installing protobuf-compiler..." |
| 46 | + install_if_not_installed "protobuf-compiler" |
| 47 | + |
| 48 | + print_message "Installing build dependencies for Substrate..." |
| 49 | + dependencies=("build-essential" "clang" "libclang-dev" "curl" "libssl-dev" "llvm" "libudev-dev" "pkg-config" "zlib1g-dev" "git") |
| 50 | + for package in "${dependencies[@]}"; do |
| 51 | + install_if_not_installed "$package" |
| 52 | + done |
| 53 | + elif [[ "$OS" == "macos" ]]; then |
| 54 | + print_message "Installing protobuf..." |
| 55 | + brew install protobuf > /dev/null 2>&1 |
| 56 | + |
| 57 | + print_message "Installing build dependencies for Substrate..." |
| 58 | + dependencies=("clang" "cmake" "pkg-config" "openssl" "llvm" "protobuf" "git") |
| 59 | + for package in "${dependencies[@]}"; do |
| 60 | + install_if_not_installed "$package" |
| 61 | + done |
| 62 | + fi |
| 63 | +} |
| 64 | + |
| 65 | +# Function to source the environment |
| 66 | +source_environment() { |
| 67 | + if [[ "$OS" == "linux" ]]; then |
| 68 | + if [ -f "$HOME/.cargo/env" ]; then |
| 69 | + source "$HOME/.cargo/env" |
| 70 | + print_message "Sourced cargo environment for Linux." |
| 71 | + else |
| 72 | + print_message "Cargo environment file not found for Linux." |
| 73 | + fi |
| 74 | + elif [[ "$OS" == "macos" ]]; then |
| 75 | + if [ -f "/usr/local/bin/cargo/env" ]; then |
| 76 | + source "/usr/local/bin/cargo/env" |
| 77 | + print_message "Sourced cargo environment for macOS." |
| 78 | + else |
| 79 | + print_message "Cargo environment file not found for macOS." |
| 80 | + fi |
| 81 | + fi |
| 82 | +} |
| 83 | + |
| 84 | +# Function to install Python3 |
| 85 | +install_python3() { |
| 86 | + print_message "Installing Python3 and pip..." |
| 87 | + install_if_not_installed "python3" |
| 88 | + install_if_not_installed "python3-pip" |
| 89 | + print_message "Installing tqdm for Python..." |
| 90 | + pip3 install tqdm > /dev/null 2>&1 |
| 91 | +} |
| 92 | + |
| 93 | +# Function to insert bootnodes into minervaRaw.json |
| 94 | +insert_bootnodes() { |
| 95 | + python3 <<EOF |
| 96 | +import re |
| 97 | +import time |
| 98 | +from tqdm import tqdm |
| 99 | +
|
| 100 | +def insert_bootnodes(original_file, bootnodes_file): |
| 101 | + try: |
| 102 | + with open(original_file, 'r') as file: |
| 103 | + original_content = file.read() |
| 104 | +
|
| 105 | + with open(bootnodes_file, 'r') as file: |
| 106 | + bootnodes_content = file.read().strip() |
| 107 | +
|
| 108 | + # Progress bar for processing the content |
| 109 | + for _ in tqdm(range(10), desc="🌟 Processing content", ncols=100, ascii=True, bar_format="{l_bar}{bar} | {n_fmt}/{total_fmt}"): |
| 110 | + time.sleep(0.1) # Simulate work being done |
| 111 | +
|
| 112 | + # Find the bootNodes section, clear its contents, and insert the new bootnodes content |
| 113 | + pattern = re.compile(r'("bootNodes"\\s*:\\s*\\[)[^\\]]*?(\\])', re.DOTALL) |
| 114 | + new_content = pattern.sub(r'\\1\n' + bootnodes_content + r'\\2', original_content) |
| 115 | +
|
| 116 | + # Progress bar for writing the new content |
| 117 | + for _ in tqdm(range(10), desc="🌟 Writing new content", ncols=100, ascii=True, bar_format="{l_bar}{bar} | {n_fmt}/{total_fmt}"): |
| 118 | + time.sleep(0.1) # Simulate work being done |
| 119 | +
|
| 120 | + # Write the modified content back to the original file |
| 121 | + with open(original_file, 'w') as file: |
| 122 | + file.write(new_content) |
| 123 | +
|
| 124 | + print(f"Successfully inserted bootnodes into {original_file}") |
| 125 | +
|
| 126 | + except Exception as e: |
| 127 | + print(f"An error occurred: {e}") |
| 128 | +
|
| 129 | +def main(): |
| 130 | + original_file = 'minervaRaw.json' # Path to the original JSON file |
| 131 | + bootnodes_file = 'bootnodes.txt' # Path to the bootnodes file |
| 132 | +
|
| 133 | + # Progress bar for reading files |
| 134 | + for _ in tqdm(range(10), desc="📄 Reading files", ncols=100, ascii=True, bar_format="{l_bar}{bar} | {n_fmt}/{total_fmt}"): |
| 135 | + time.sleep(0.1) # Simulate work being done |
| 136 | +
|
| 137 | + insert_bootnodes(original_file, bootnodes_file) |
| 138 | +
|
| 139 | +if __name__ == "__main__": |
| 140 | + main() |
| 141 | +EOF |
| 142 | +} |
| 143 | + |
| 144 | +# Main script execution |
| 145 | +check_os |
| 146 | +install_dependencies |
| 147 | +source_environment |
| 148 | +install_python3 |
| 149 | +insert_bootnodes |
| 150 | + |
| 151 | +print_message "Installation and setup complete. Please restart your terminal." |
0 commit comments