main.sh 4.5 KB

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