nupst/install.sh

297 lines
8.3 KiB
Bash

#!/bin/bash
# NUPST Installer Script
# Downloads and installs NUPST globally on the system
# Can be used directly with curl:
# Without auto-installing dependencies:
# curl -sSL https://code.foss.global/serve.zone/nupst/raw/branch/main/install.sh | sudo bash
# With auto-installing dependencies:
# curl -sSL https://code.foss.global/serve.zone/nupst/raw/branch/main/install.sh | sudo bash -s -- -y
#
# Options:
# -y, --yes Automatically answer yes to all prompts
# -h, --help Show this help message
# Parse command line arguments
AUTO_YES=0
SHOW_HELP=0
for arg in "$@"; do
case $arg in
-y|--yes)
AUTO_YES=1
shift
;;
-h|--help)
SHOW_HELP=1
shift
;;
*)
# Unknown option
;;
esac
done
if [ $SHOW_HELP -eq 1 ]; then
echo "NUPST Installer Script"
echo "Usage: $0 [options]"
echo ""
echo "Options:"
echo " -y, --yes Automatically answer yes to all prompts"
echo " -h, --help Show this help message"
exit 0
fi
# Check if running as root
if [ "$EUID" -ne 0 ]; then
echo "Please run as root (sudo bash install.sh or pipe to sudo bash)"
exit 1
fi
# Detect if script is being piped or run directly
PIPED=0
INTERACTIVE=1
if [ ! -t 0 ]; then
# Being piped, need to clone the repo
PIPED=1
fi
# Check if stdin is a terminal
if [ ! -t 0 ] || [ ! -t 1 ]; then
# Either stdin or stdout is not a terminal, check if -y was provided
if [ $AUTO_YES -ne 1 ]; then
echo "Script detected it's running in a non-interactive environment without -y flag."
echo "Attempting to find a controlling terminal for interactive prompts..."
# Try to use a controlling terminal for user input
if [ -t 1 ]; then
# Stdout is a terminal, use it
exec < /dev/tty 2>/dev/null || INTERACTIVE=0
else
# Try to find controlling terminal
exec < /dev/tty 2>/dev/null || INTERACTIVE=0
fi
if [ $INTERACTIVE -eq 0 ]; then
echo "ERROR: No controlling terminal available for interactive prompts."
echo "For interactive installation (RECOMMENDED):"
echo " curl -sSL https://code.foss.global/serve.zone/nupst/raw/branch/main/install.sh -o nupst-install.sh"
echo " sudo bash nupst-install.sh"
echo ""
echo "For non-interactive installation with automatic dependency installation:"
echo " curl -sSL https://code.foss.global/serve.zone/nupst/raw/branch/main/install.sh | sudo bash -s -- -y"
exit 1
else
echo "Interactive terminal found, continuing with prompts..."
fi
fi
fi
# Helper function to detect OS type
detect_os() {
if [ -f /etc/os-release ]; then
. /etc/os-release
OS=$ID
elif type lsb_release >/dev/null 2>&1; then
OS=$(lsb_release -si | tr '[:upper:]' '[:lower:]')
elif [ -f /etc/lsb-release ]; then
. /etc/lsb-release
OS=$DISTRIB_ID
elif [ -f /etc/debian_version ]; then
OS="debian"
elif [ -f /etc/redhat-release ]; then
if grep -q "CentOS" /etc/redhat-release; then
OS="centos"
elif grep -q "Fedora" /etc/redhat-release; then
OS="fedora"
else
OS="rhel"
fi
else
OS=$(uname -s)
fi
echo $OS
}
# Helper function to install git
install_git() {
OS=$(detect_os)
echo "Detected OS: $OS"
case "$OS" in
ubuntu|debian|pop|mint|elementary|kali|zorin)
echo "Installing git using apt..."
apt-get update && apt-get install -y git
;;
fedora|rhel|centos|almalinux|rocky)
echo "Installing git using dnf/yum..."
if command -v dnf &> /dev/null; then
dnf install -y git
else
yum install -y git
fi
;;
arch|manjaro|endeavouros|garuda)
echo "Installing git using pacman..."
pacman -Sy --noconfirm git
;;
opensuse*|suse|sles)
echo "Installing git using zypper..."
zypper install -y git
;;
alpine)
echo "Installing git using apk..."
apk add git
;;
*)
echo "Unsupported OS: $OS"
echo "Please install git manually and run the installer again."
exit 1
;;
esac
# Check if git was installed successfully
if ! command -v git &> /dev/null; then
echo "Failed to install git. Please install git manually and run the installer again."
exit 1
fi
echo "Git installed successfully."
}
# Define installation directory
INSTALL_DIR="/opt/nupst"
REPO_URL="https://code.foss.global/serve.zone/nupst.git"
# Check if git is installed - needed for both piped and direct execution
if ! command -v git &> /dev/null; then
echo "Git is required but not installed."
if [ $AUTO_YES -eq 1 ]; then
echo "Auto-installing git (-y flag provided)..."
install_git
elif [ $INTERACTIVE -eq 1 ]; then
# If interactive and no -y flag, ask the user
echo "Would you like to install git now? (y/N): "
read -r install_git_prompt
if [[ "$install_git_prompt" =~ ^[Yy]$ ]]; then
install_git
else
echo "Git installation skipped. Please install git manually and run the installer again."
echo "Alternatively, you can run the installer with -y flag to automatically install git:"
echo " sudo bash install.sh -y"
exit 1
fi
else
# Non-interactive mode without -y flag
echo "Error: Git is required but not installed."
echo "In non-interactive mode, use -y flag to auto-install dependencies:"
echo " curl -sSL https://code.foss.global/serve.zone/nupst/raw/branch/main/install.sh | sudo bash -s -- -y"
exit 1
fi
fi
if [ $PIPED -eq 1 ]; then
echo "Installing NUPST from remote repository..."
# Check if installation directory exists
if [ -d "$INSTALL_DIR" ] && [ -d "$INSTALL_DIR/.git" ]; then
echo "Existing installation found at $INSTALL_DIR. Updating..."
cd "$INSTALL_DIR"
# Try to update the repository
git fetch origin
git reset --hard origin/main
if [ $? -ne 0 ]; then
echo "Failed to update repository. Reinstalling..."
cd /
rm -rf "$INSTALL_DIR"
mkdir -p "$INSTALL_DIR"
git clone --depth 1 $REPO_URL "$INSTALL_DIR"
else
echo "Repository updated successfully."
fi
else
# Fresh installation
if [ -d "$INSTALL_DIR" ]; then
echo "Removing previous installation at $INSTALL_DIR..."
rm -rf "$INSTALL_DIR"
fi
# Create installation directory
mkdir -p "$INSTALL_DIR"
# Clone the repository
echo "Cloning NUPST repository to $INSTALL_DIR..."
git clone --depth 1 $REPO_URL "$INSTALL_DIR"
fi
if [ $? -ne 0 ]; then
echo "Failed to clone/update repository. Please check your internet connection."
exit 1
fi
# Set script directory to the cloned repo
SCRIPT_DIR="$INSTALL_DIR"
else
# Running directly from within the repo or downloaded script
SCRIPT_DIR="$( cd "$( dirname "${BASH_SOURCE[0]}" )" &> /dev/null && pwd )"
# When running from a downloaded script in a different location
# we need to clone the repository first
if [ ! -f "$SCRIPT_DIR/setup.sh" ]; then
echo "Running installer from downloaded script outside repository."
echo "Will clone the repository to $INSTALL_DIR..."
# Create installation directory if needed
if [ -d "$INSTALL_DIR" ]; then
echo "Removing previous installation at $INSTALL_DIR..."
rm -rf "$INSTALL_DIR"
fi
mkdir -p "$INSTALL_DIR"
# Clone the repository
echo "Cloning NUPST repository to $INSTALL_DIR..."
git clone --depth 1 $REPO_URL "$INSTALL_DIR"
if [ $? -ne 0 ]; then
echo "Failed to clone repository. Please check your internet connection."
exit 1
fi
# Update script directory to use the cloned repo
SCRIPT_DIR="$INSTALL_DIR"
fi
fi
# Run setup script
echo "Running setup script..."
if [ ! -f "$SCRIPT_DIR/setup.sh" ]; then
echo "ERROR: Setup script not found at $SCRIPT_DIR/setup.sh"
echo "Current directory: $(pwd)"
echo "Script directory: $SCRIPT_DIR"
ls -la "$SCRIPT_DIR"
exit 1
fi
bash "$SCRIPT_DIR/setup.sh"
# Install globally
echo "Installing NUPST globally..."
ln -sf "$SCRIPT_DIR/bin/nupst" /usr/local/bin/nupst
# Installation completed
if [ $PIPED -eq 1 ]; then
echo "NUPST has been installed globally at $INSTALL_DIR"
else
echo "NUPST has been installed globally."
fi
echo "You can now run 'nupst' from anywhere."
echo ""
echo "To get started, try:"
echo " nupst help"
echo " nupst setup # To configure your UPS connection"