diff --git a/scripts/check_homebrew.sh b/scripts/check_homebrew.sh deleted file mode 100755 index fd58489e..00000000 --- a/scripts/check_homebrew.sh +++ /dev/null @@ -1,11 +0,0 @@ -#!/bin/bash - -check_homebrew() { - if ! command -v brew &> /dev/null; then - log "${PRIMARY_COLOR}Homebrew not found. Installing Homebrew...${RESET_COLOR}" - /bin/bash -c "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/HEAD/install.sh)" || { - log "${PRIMARY_COLOR}Failed to install Homebrew.${RESET_COLOR}" - exit 1 - } - fi -} diff --git a/scripts/create_launchd_plist.sh b/scripts/create_launchd_plist.sh deleted file mode 100755 index 9964a1fe..00000000 --- a/scripts/create_launchd_plist.sh +++ /dev/null @@ -1,43 +0,0 @@ -#!/bin/bash - -create_launchd_plist() { - create_plist() { - local plist_path=$1 - local label=$2 - local program_args=$3 - local working_dir=$4 - - if [ -f "$plist_path" ]; then - log "${PRIMARY_COLOR}${label} already exists. Skipping creation.${RESET_COLOR}" - else - cat < "$plist_path" - - - - - Label - ${label} - ProgramArguments - - ${program_args} - - WorkingDirectory - ${working_dir} - RunAtLoad - - KeepAlive - - - -EOF - fi - } - USER_HOME=$(eval echo ~$(whoami)) - NVM_DIR="$([ -z "${XDG_CONFIG_HOME-}" ] && printf %s "${USER_HOME}/.nvm" || printf %s "${XDG_CONFIG_HOME}/nvm")" - LAUNCH_AGENTS_DIR="${USER_HOME}/Library/LaunchAgents" - - create_plist "${LAUNCH_AGENTS_DIR}/local.lnd.plist" "local.lnd" "${USER_HOME}/lnd/lnd" "" - create_plist "${LAUNCH_AGENTS_DIR}/local.lightning_pub.plist" "local.lightning_pub" "/bin/bash-csource ${NVM_DIR}/nvm.sh && npm start" "${USER_HOME}/lightning_pub" - - log "${PRIMARY_COLOR}Created launchd plists. Please load them using launchctl.${RESET_COLOR}" -} diff --git a/scripts/handle_macos.sh b/scripts/handle_macos.sh new file mode 100644 index 00000000..c9e43f20 --- /dev/null +++ b/scripts/handle_macos.sh @@ -0,0 +1,148 @@ +#!/bin/bash + +# macOS-specific installation handler +# Called from install.sh when OS=Mac + +handle_macos() { + local REPO_URL="$1" + + export INSTALL_DIR="$HOME/lightning_pub" + export LAUNCH_AGENTS_DIR="$HOME/Library/LaunchAgents" + mkdir -p "$LAUNCH_AGENTS_DIR" + + # Install Homebrew if needed + if ! command -v brew &> /dev/null; then + log "${PRIMARY_COLOR}Installing Homebrew...${RESET_COLOR}" + /bin/bash -c "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/HEAD/install.sh)" || { + log_error "Failed to install Homebrew" 1 + } + fi + + # Install LND + log "${PRIMARY_COLOR}Installing${RESET_COLOR} ${SECONDARY_COLOR}LND${RESET_COLOR}..." + lnd_output=$(install_lnd) + install_result=$? + + if [ $install_result -ne 0 ]; then + log_error "LND installation failed" $install_result + fi + + lnd_status=$(echo "$lnd_output" | grep "LND_STATUS:" | cut -d':' -f2) + + case $lnd_status in + 0) log "LND fresh installation completed successfully." ;; + 1) log "LND upgrade completed successfully." ;; + 2) log "LND is already up-to-date. No action needed." ;; + *) log "WARNING: Unexpected status from install_lnd: $lnd_status" ;; + esac + + # Install Node.js + install_nodejs || log_error "Failed to install Node.js" 1 + + # Install Lightning.Pub + install_lightning_pub "$REPO_URL" || pub_install_status=$? + + case ${pub_install_status:-0} in + 0) + log "Lightning.Pub fresh installation completed successfully." + pub_upgrade_status=0 + ;; + 100) + log "Lightning.Pub upgrade completed successfully." + pub_upgrade_status=100 + ;; + 2) + log "Lightning.Pub is already up-to-date. No action needed." + pub_upgrade_status=2 + ;; + *) + log_error "Lightning.Pub installation failed with exit code $pub_install_status" "$pub_install_status" + ;; + esac + + # Start services using launchd + if [ "$pub_upgrade_status" -eq 0 ] || [ "$pub_upgrade_status" -eq 100 ]; then + log "Starting services..." + if [ "$lnd_status" = "0" ] || [ "$lnd_status" = "1" ]; then + log "Note: LND may take several minutes to sync block headers depending on network conditions." + fi + + create_launchd_plists + + # Start/restart services + for svc in local.lnd local.lightning_pub; do + local plist="$LAUNCH_AGENTS_DIR/${svc}.plist" + if launchctl list 2>/dev/null | grep -q "$svc"; then + launchctl unload "$plist" 2>/dev/null || true + fi + launchctl load "$plist" + done + log "${SECONDARY_COLOR}LND${RESET_COLOR} and ${SECONDARY_COLOR}Lightning.Pub${RESET_COLOR} services started." + + TIMESTAMP_FILE=$(mktemp) + export TIMESTAMP_FILE + get_log_info || log_error "Failed to get log info" 1 + fi + + log "Installation process completed successfully" + + if [ -d "$HOME/lightning_pub" ]; then + mv "$TMP_LOG_FILE" "$HOME/lightning_pub/install.log" + chmod 600 "$HOME/lightning_pub/install.log" + else + rm -f "$TMP_LOG_FILE" + fi +} + +create_launchd_plists() { + local NVM_DIR="$HOME/.nvm" + + # LND plist + if [ ! -f "$LAUNCH_AGENTS_DIR/local.lnd.plist" ]; then + cat > "$LAUNCH_AGENTS_DIR/local.lnd.plist" < + + + + Label + local.lnd + ProgramArguments + + $HOME/lnd/lnd + + RunAtLoad + + KeepAlive + + + +EOF + fi + + # Lightning.Pub plist + if [ ! -f "$LAUNCH_AGENTS_DIR/local.lightning_pub.plist" ]; then + cat > "$LAUNCH_AGENTS_DIR/local.lightning_pub.plist" < + + + + Label + local.lightning_pub + ProgramArguments + + /bin/bash + -c + source $NVM_DIR/nvm.sh && npm start + + WorkingDirectory + $INSTALL_DIR + RunAtLoad + + KeepAlive + + + +EOF + fi +} + diff --git a/scripts/install.sh b/scripts/install.sh index 171d0b1c..b25e79f0 100755 --- a/scripts/install.sh +++ b/scripts/install.sh @@ -11,7 +11,7 @@ log() { echo -e "$(echo "$message" | sed 's/\\e\[[0-9;]*m//g')" >> "$TMP_LOG_FILE" } -SCRIPT_VERSION="0.2.2" +SCRIPT_VERSION="0.3.0" REPO="shocknet/Lightning.Pub" BRANCH="master" @@ -36,6 +36,7 @@ modules=( "install_nodejs" "install_lightning_pub" "start_services" + "handle_macos" "extract_nprofile" ) @@ -91,7 +92,7 @@ log "Detected OS: $OS" log "Detected ARCH: $ARCH" if [ "$OS" = "Mac" ]; then - log_error "macOS is not currently supported by this install script. Please use a Linux-based system." 1 + handle_macos "$REPO_URL" else # Explicit kickoff log for LND so the flow is clear in the install log log "${PRIMARY_COLOR}Installing${RESET_COLOR} ${SECONDARY_COLOR}LND${RESET_COLOR}..." diff --git a/scripts/install_lnd.sh b/scripts/install_lnd.sh index af53650c..7c09c725 100755 --- a/scripts/install_lnd.sh +++ b/scripts/install_lnd.sh @@ -9,15 +9,17 @@ install_lnd() { USER_NAME=$(whoami) log "Checking latest LND version..." - LND_VERSION=$(wget -qO- https://api.github.com/repos/lightningnetwork/lnd/releases/latest | grep -oP '"tag_name": "\K(.*)(?=")') + local api_response=$(wget -qO- https://api.github.com/repos/lightningnetwork/lnd/releases/latest) + LND_VERSION=$(json_value "tag_name" "$api_response") log "Latest LND version: $LND_VERSION" - LND_URL="https://github.com/lightningnetwork/lnd/releases/download/${LND_VERSION}/lnd-${OS}-${ARCH}-${LND_VERSION}.tar.gz" + local LND_OS="$OS"; [ "$OS" = "Mac" ] && LND_OS="darwin" + LND_URL="https://github.com/lightningnetwork/lnd/releases/download/${LND_VERSION}/lnd-${LND_OS}-${ARCH}-${LND_VERSION}.tar.gz" # Check if LND is already installed if [ -d "$USER_HOME/lnd" ]; then log "LND directory found. Checking current version..." - CURRENT_VERSION=$("$USER_HOME/lnd/lnd" --version | grep -oP 'version \K[^\s]+') + CURRENT_VERSION=$("$USER_HOME/lnd/lnd" --version | awk '/version/ {print $3}') log "Current LND version: $CURRENT_VERSION" if [ "$CURRENT_VERSION" == "${LND_VERSION#v}" ]; then @@ -60,12 +62,13 @@ install_lnd() { log "${PRIMARY_COLOR}Stopping${RESET_COLOR} ${SECONDARY_COLOR}LND${RESET_COLOR} user service..." systemctl --user stop lnd fi - else - log "${PRIMARY_COLOR}Please stop ${SECONDARY_COLOR}LND${RESET_COLOR} manually if it is running.${RESET_COLOR}" + elif [ "$OS" = "Mac" ] && launchctl list 2>/dev/null | grep -q "local.lnd"; then + log "${PRIMARY_COLOR}Stopping${RESET_COLOR} ${SECONDARY_COLOR}LND${RESET_COLOR} launchd service..." + launchctl unload "$USER_HOME/Library/LaunchAgents/local.lnd.plist" 2>/dev/null || true fi log "Extracting LND..." - LND_TMP_DIR=$(mktemp -d -p "$USER_HOME") + LND_TMP_DIR=$(mktemp_in "$USER_HOME") tar -xzf "$USER_HOME/lnd.tar.gz" -C "$LND_TMP_DIR" --strip-components=1 > /dev/null || { log "${PRIMARY_COLOR}Failed to extract LND.${RESET_COLOR}" @@ -106,7 +109,7 @@ install_lnd() { # Port conflict resolution. local lnd_port=9735 - if ! is_port_available $lnd_port; then + if [ "$OS" = "Linux" ] && ! is_port_available $lnd_port; then # The port is occupied. We should intervene if our service is either in a failed state # or not active at all (which covers fresh installs and failure loops). if systemctl --user -q is-failed lnd.service 2>/dev/null || ! systemctl --user -q is-active lnd.service 2>/dev/null; then @@ -114,7 +117,7 @@ install_lnd() { lnd_port_new=$(find_available_port $lnd_port) log "Configuring LND to use new port $lnd_port_new." - sed -i '/^listen=/d' $USER_HOME/.lnd/lnd.conf + sed_i '/^listen=/d' $USER_HOME/.lnd/lnd.conf echo "listen=0.0.0.0:$lnd_port_new" >> $USER_HOME/.lnd/lnd.conf log "LND configuration updated. The service will be restarted by the installer." else diff --git a/scripts/install_nodejs.sh b/scripts/install_nodejs.sh index 06601a20..786315aa 100755 --- a/scripts/install_nodejs.sh +++ b/scripts/install_nodejs.sh @@ -13,7 +13,8 @@ install_nodejs() { [ -s "${NVM_DIR}/nvm.sh" ] && \. "${NVM_DIR}/nvm.sh" if ! command -v nvm &> /dev/null; then - NVM_VERSION=$(wget -qO- https://api.github.com/repos/nvm-sh/nvm/releases/latest | grep -oP '"tag_name": "\K(.*)(?=")') + local nvm_api=$(wget -qO- https://api.github.com/repos/nvm-sh/nvm/releases/latest) + NVM_VERSION=$(json_value "tag_name" "$nvm_api") wget -qO- https://raw.githubusercontent.com/nvm-sh/nvm/${NVM_VERSION}/install.sh | bash > /dev/null 2>&1 export NVM_DIR="${NVM_DIR}" [ -s "${NVM_DIR}/nvm.sh" ] && \. "${NVM_DIR}/nvm.sh" diff --git a/scripts/start_services_mac.sh b/scripts/start_services_mac.sh deleted file mode 100755 index 81f817c1..00000000 --- a/scripts/start_services_mac.sh +++ /dev/null @@ -1,17 +0,0 @@ -#!/bin/bash - -start_services_mac() { - create_launchd_plist - launchctl load "${LAUNCH_AGENTS_DIR}/local.lnd.plist" - launchctl load "${LAUNCH_AGENTS_DIR}/local.lightning_pub.plist" - log "${SECONDARY_COLOR}LND${RESET_COLOR} and ${SECONDARY_COLOR}Lightning.Pub${RESET_COLOR} services started using launchd." -} - -handle_macos() { - check_homebrew - install_rsync_mac - install_nodejs - install_lightning_pub - create_launchd_plist - start_services_mac -} diff --git a/scripts/utils.sh b/scripts/utils.sh index 87dc5df6..0d7d9c39 100755 --- a/scripts/utils.sh +++ b/scripts/utils.sh @@ -29,10 +29,43 @@ detect_os_arch() { } check_deps() { - for cmd in wget grep stat tar sha256sum; do + for cmd in wget grep stat tar; do if ! command -v $cmd &> /dev/null; then log "Missing system dependency: $cmd. Install $cmd via your package manager and retry." exit 1 fi done + if ! command -v sha256sum &> /dev/null && ! command -v shasum &> /dev/null; then + log "Missing system dependency: sha256sum or shasum." + exit 1 + fi +} + +# Cross-platform sed in-place +sed_i() { + if [ "$OS" = "Mac" ]; then + sed -i '' "$@" + else + sed -i "$@" + fi +} + +# Cross-platform mktemp with parent directory +mktemp_in() { + local parent="$1" + if [ "$OS" = "Mac" ]; then + mktemp -d "${parent}/tmp.XXXXXX" + else + mktemp -d -p "$parent" + fi +} + +# Extract JSON value (cross-platform) +json_value() { + local key="$1" + if [ "$OS" = "Mac" ]; then + echo "$2" | awk -F"\"${key}\"[[:space:]]*:[[:space:]]*\"" '{print $2}' | awk -F'"' '{print $1}' | head -1 + else + echo "$2" | grep -oP "\"${key}\": \"\\K[^\"]+" + fi } \ No newline at end of file