| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180 |
- #!/bin/bash
- # main.sh - Main entry point for USM installer
- # This file will be the entry point of the compiled installer
- # Default configuration
- TARGET_DIR="/opt/usm"
- ASSUME_YES="false"
- VERBOSE="false"
- SCRIPT_VERSION="@VERSION@"
- PM_TYPE=""
- ORIGINAL_ARGS=("$@")
- # Parse command line arguments
- parse_args() {
- while [[ $# -gt 0 ]]; do
- case "$1" in
- --target|-t)
- TARGET_DIR="$2"
- shift 2
- ;;
- --target=*)
- TARGET_DIR="${1#*=}"
- shift
- ;;
- -y|--yes|--assume-yes)
- ASSUME_YES="true"
- shift
- ;;
- --verbose|-V)
- VERBOSE="true"
- shift
- ;;
- --help|-h)
- show_help
- exit 0
- ;;
- --version|-v)
- echo "USM Installer v${SCRIPT_VERSION}"
- exit 0
- ;;
- *)
- echo "Unknown option: $1"
- show_help
- exit 1
- ;;
- esac
- done
- }
- # Show help message
- show_help() {
- cat << EOF
- USM Installer v${SCRIPT_VERSION}
- Usage: $(basename "$0") [OPTIONS]
- Options:
- -t, --target DIR Install to DIR instead of /opt/usm
- -y, --assume-yes Answer yes to all prompts automatically
- -V, --verbose Show verbose build output
- -h, --help Show this help message
- -v, --version Show version information
- Examples:
- $(basename "$0") # Install to /opt/usm
- $(basename "$0") -t /opt/myusm # Install to /opt/myusm
- $(basename "$0") -y # Non-interactive install
- $(basename "$0") -V # Verbose output
- EOF
- }
- # Check for root privileges and offer to re-run with sudo
- check_root() {
- if is_root; then
- return 0
- fi
-
- local sudo_cmd=$(get_sudo)
-
- if [[ -z "$sudo_cmd" ]]; then
- show_error_and_exit "This script requires root privileges. Please run with sudo or as root."
- fi
-
- log_error "This script requires root privileges."
-
- if confirm "Would you like to re-run the script with $sudo_cmd?" "y"; then
- log_info "Re-running with $sudo_cmd..."
- exec $sudo_cmd "$0" "${ORIGINAL_ARGS[@]}"
- else
- show_error_and_exit "Root privileges are required. Installation cancelled."
- fi
- }
- # Check if target directory exists and handle reinstallation
- check_existing_installation() {
- if [[ -d "$TARGET_DIR" ]]; then
- log_warn "Target directory '$TARGET_DIR' already exists."
-
- if [[ "$ASSUME_YES" == "true" ]]; then
- log_info "Removing existing installation (auto-confirmed with -y)..."
- rm -rf "$TARGET_DIR"
- return 0
- fi
-
- if confirm "Do you want to delete it and reinstall?" "n"; then
- log_info "Removing existing installation..."
- rm -rf "$TARGET_DIR"
- else
- echo "Installation cancelled."
- exit 0
- fi
- fi
- }
- # Main installation function
- run_installation() {
- # Set up temporary directory
- setup_temp_dir
- local extract_dir="$TEMP_DIR/sources"
- ensure_dir "$extract_dir"
-
- # Extract bundled sources
- log_step "Extracting bundled sources..."
- if ! extract_payload "$extract_dir"; then
- show_error_and_exit "Failed to extract bundled sources"
- fi
-
- # Install system dependencies
- log_step "Checking system dependencies..."
- if ! pm_install_missing_deps; then
- show_error_and_exit "Failed to install system dependencies. Installation halted."
- fi
-
- # Build and install all components
- log_step "Building and installing components..."
- if ! build_all "$extract_dir" "$TARGET_DIR"; then
- show_error_and_exit "Failed to build components. Installation halted."
- fi
-
- # Install shim
- install_shim "$TARGET_DIR"
-
- # Show completion message
- show_completion "$TARGET_DIR"
- }
- # Main entry point
- main() {
- parse_args "$@"
-
- # Print banner
- print_banner
-
- # Check for root privileges
- check_root
-
- # Detect package manager
- if ! detect_package_manager; then
- show_error_and_exit "Could not detect a supported package manager (apt or dnf)"
- fi
-
- log_info "Detected package manager: ${PM_TYPE}"
-
- # Get missing dependencies for display
- local missing_deps=$(pm_get_missing_deps)
- local deps_count=$(count_missing_deps)
-
- # Show installation summary and get confirmation
- if ! show_installation_summary "$TARGET_DIR" "$PM_TYPE" "$deps_count" "$missing_deps"; then
- echo "Installation cancelled."
- exit 0
- fi
-
- # Check if target directory exists and handle reinstallation
- check_existing_installation
-
- # Run the installation
- run_installation
- }
|