#!/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 if [ ! -t 0 ]; then # Being piped, need to clone the repo PIPED=1 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" if [ $PIPED -eq 1 ]; then echo "Installing NUPST from remote repository..." # Check if git is installed 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 else read -p "Would you like to install git now? (y/N): " 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 fi fi # 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 SCRIPT_DIR="$( cd "$( dirname "${BASH_SOURCE[0]}" )" &> /dev/null && pwd )" fi # Run setup script echo "Running setup script..." 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"