#!/bin/bash # detect-platform.sh - Platform detection utility # Returns: win32, linux, or macos # Exit code: 0 for success, 1 for unknown platform set -e # ============================================================================= # Platform Detection # ============================================================================= detect_platform() { local uname_output uname_output=$(uname -s) case "$uname_output" in "Darwin") echo "macos" return 0 ;; "Linux") echo "linux" return 0 ;; "CYGWIN"*|"MINGW"*|"MSYS"*) echo "win32" return 0 ;; *) echo "Error: Unknown platform '$uname_output'" >&2 echo "Supported platforms: Darwin (macOS), Linux, CYGWIN/MINGW/MSYS (Windows)" >&2 return 1 ;; esac } # ============================================================================= # Additional Platform Information # ============================================================================= detect_architecture() { local uname_machine uname_machine=$(uname -m) case "$uname_machine" in "x86_64"|"amd64") echo "x64" return 0 ;; "i386"|"i686") echo "x86" return 0 ;; "arm64"|"aarch64") echo "arm64" return 0 ;; *) echo "Error: Unknown architecture '$uname_machine'" >&2 echo "Supported architectures: x86_64, i386, i686, arm64, aarch64" >&2 return 1 ;; esac } get_platform_info() { local platform arch platform=$(detect_platform) if [[ $? -ne 0 ]]; then return 1 fi arch=$(detect_architecture) if [[ $? -ne 0 ]]; then return 1 fi echo "Platform: $platform" echo "Architecture: $arch" echo "Full info: $(uname -a)" return 0 } # ============================================================================= # Main execution # ============================================================================= main() { local command="${1:-detect}" case "$command" in "detect") detect_platform ;; "arch") detect_architecture ;; "info") get_platform_info ;; "help"|"-h"|"--help") echo "Usage: $0 [detect|arch|info|help]" echo "" echo "Commands:" echo " detect - Detect platform (macos, linux, win32)" echo " arch - Detect architecture (x64, x86, arm64)" echo " info - Show detailed platform information" echo " help - Show this help message" echo "" echo "Examples:" echo " $0 detect # Returns: macos" echo " $0 arch # Returns: x64" echo " $0 info # Returns: Platform: macos, Architecture: x64, etc." ;; *) echo "Error: Unknown command '$command'" >&2 echo "Run '$0 help' for usage information" >&2 return 1 ;; esac } # Only run main if script is executed directly (not sourced) if [[ "${BASH_SOURCE[0]}" == "${0}" ]]; then main "$@" fi