main.sh 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180
  1. #!/bin/bash
  2. # main.sh - Main entry point for USM installer
  3. # This file will be the entry point of the compiled installer
  4. # Default configuration
  5. TARGET_DIR="/opt/usm"
  6. ASSUME_YES="false"
  7. VERBOSE="false"
  8. SCRIPT_VERSION="@VERSION@"
  9. PM_TYPE=""
  10. ORIGINAL_ARGS=("$@")
  11. # Parse command line arguments
  12. parse_args() {
  13. while [[ $# -gt 0 ]]; do
  14. case "$1" in
  15. --target|-t)
  16. TARGET_DIR="$2"
  17. shift 2
  18. ;;
  19. --target=*)
  20. TARGET_DIR="${1#*=}"
  21. shift
  22. ;;
  23. -y|--yes|--assume-yes)
  24. ASSUME_YES="true"
  25. shift
  26. ;;
  27. --verbose|-V)
  28. VERBOSE="true"
  29. shift
  30. ;;
  31. --help|-h)
  32. show_help
  33. exit 0
  34. ;;
  35. --version|-v)
  36. echo "USM Installer v${SCRIPT_VERSION}"
  37. exit 0
  38. ;;
  39. *)
  40. echo "Unknown option: $1"
  41. show_help
  42. exit 1
  43. ;;
  44. esac
  45. done
  46. }
  47. # Show help message
  48. show_help() {
  49. cat << EOF
  50. USM Installer v${SCRIPT_VERSION}
  51. Usage: $(basename "$0") [OPTIONS]
  52. Options:
  53. -t, --target DIR Install to DIR instead of /opt/usm
  54. -y, --assume-yes Answer yes to all prompts automatically
  55. -V, --verbose Show verbose build output
  56. -h, --help Show this help message
  57. -v, --version Show version information
  58. Examples:
  59. $(basename "$0") # Install to /opt/usm
  60. $(basename "$0") -t /opt/myusm # Install to /opt/myusm
  61. $(basename "$0") -y # Non-interactive install
  62. $(basename "$0") -V # Verbose output
  63. EOF
  64. }
  65. # Check for root privileges and offer to re-run with sudo
  66. check_root() {
  67. if is_root; then
  68. return 0
  69. fi
  70. local sudo_cmd=$(get_sudo)
  71. if [[ -z "$sudo_cmd" ]]; then
  72. show_error_and_exit "This script requires root privileges. Please run with sudo or as root."
  73. fi
  74. log_error "This script requires root privileges."
  75. if confirm "Would you like to re-run the script with $sudo_cmd?" "y"; then
  76. log_info "Re-running with $sudo_cmd..."
  77. exec $sudo_cmd "$0" "${ORIGINAL_ARGS[@]}"
  78. else
  79. show_error_and_exit "Root privileges are required. Installation cancelled."
  80. fi
  81. }
  82. # Check if target directory exists and handle reinstallation
  83. check_existing_installation() {
  84. if [[ -d "$TARGET_DIR" ]]; then
  85. log_warn "Target directory '$TARGET_DIR' already exists."
  86. if [[ "$ASSUME_YES" == "true" ]]; then
  87. log_info "Removing existing installation (auto-confirmed with -y)..."
  88. rm -rf "$TARGET_DIR"
  89. return 0
  90. fi
  91. if confirm "Do you want to delete it and reinstall?" "n"; then
  92. log_info "Removing existing installation..."
  93. rm -rf "$TARGET_DIR"
  94. else
  95. echo "Installation cancelled."
  96. exit 0
  97. fi
  98. fi
  99. }
  100. # Main installation function
  101. run_installation() {
  102. # Set up temporary directory
  103. setup_temp_dir
  104. local extract_dir="$TEMP_DIR/sources"
  105. ensure_dir "$extract_dir"
  106. # Extract bundled sources
  107. log_step "Extracting bundled sources..."
  108. if ! extract_payload "$extract_dir"; then
  109. show_error_and_exit "Failed to extract bundled sources"
  110. fi
  111. # Install system dependencies
  112. log_step "Checking system dependencies..."
  113. if ! pm_install_missing_deps; then
  114. show_error_and_exit "Failed to install system dependencies. Installation halted."
  115. fi
  116. # Build and install all components
  117. log_step "Building and installing components..."
  118. if ! build_all "$extract_dir" "$TARGET_DIR"; then
  119. show_error_and_exit "Failed to build components. Installation halted."
  120. fi
  121. # Install shim
  122. install_shim "$TARGET_DIR"
  123. # Show completion message
  124. show_completion "$TARGET_DIR"
  125. }
  126. # Main entry point
  127. main() {
  128. parse_args "$@"
  129. # Print banner
  130. print_banner
  131. # Check for root privileges
  132. check_root
  133. # Detect package manager
  134. if ! detect_package_manager; then
  135. show_error_and_exit "Could not detect a supported package manager (apt or dnf)"
  136. fi
  137. log_info "Detected package manager: ${PM_TYPE}"
  138. # Get missing dependencies for display
  139. local missing_deps=$(pm_get_missing_deps)
  140. local deps_count=$(count_missing_deps)
  141. # Show installation summary and get confirmation
  142. if ! show_installation_summary "$TARGET_DIR" "$PM_TYPE" "$deps_count" "$missing_deps"; then
  143. echo "Installation cancelled."
  144. exit 0
  145. fi
  146. # Check if target directory exists and handle reinstallation
  147. check_existing_installation
  148. # Run the installation
  149. run_installation
  150. }