main.sh 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156
  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. # Main installation function
  83. run_installation() {
  84. # Set up temporary directory
  85. setup_temp_dir
  86. local extract_dir="$TEMP_DIR/sources"
  87. ensure_dir "$extract_dir"
  88. # Extract bundled sources
  89. log_step "Extracting bundled sources..."
  90. if ! extract_payload "$extract_dir"; then
  91. show_error_and_exit "Failed to extract bundled sources"
  92. fi
  93. # Install system dependencies
  94. log_step "Checking system dependencies..."
  95. if ! pm_install_missing_deps; then
  96. show_error_and_exit "Failed to install system dependencies. Installation halted."
  97. fi
  98. # Build and install all components
  99. log_step "Building and installing components..."
  100. if ! build_all "$extract_dir" "$TARGET_DIR"; then
  101. show_error_and_exit "Failed to build components. Installation halted."
  102. fi
  103. # Install shim
  104. install_shim "$TARGET_DIR"
  105. # Show completion message
  106. show_completion "$TARGET_DIR"
  107. }
  108. # Main entry point
  109. main() {
  110. parse_args "$@"
  111. # Print banner
  112. print_banner
  113. # Check for root privileges
  114. check_root
  115. # Detect package manager
  116. if ! detect_package_manager; then
  117. show_error_and_exit "Could not detect a supported package manager (apt or dnf)"
  118. fi
  119. log_info "Detected package manager: ${PM_TYPE}"
  120. # Get missing dependencies for display
  121. local missing_deps=$(pm_get_missing_deps)
  122. local deps_count=$(count_missing_deps)
  123. # Show installation summary and get confirmation
  124. if ! show_installation_summary "$TARGET_DIR" "$PM_TYPE" "$deps_count" "$missing_deps"; then
  125. echo "Installation cancelled."
  126. exit 0
  127. fi
  128. # Run the installation
  129. run_installation
  130. }