#!/bin/bash set -e GITHUB_API="https://api.github.com/repos/Lameguy64/PSn00bSDK/releases/latest" GITHUB_RELEASES="https://github.com/Lameguy64/PSn00bSDK/releases" DEFAULT_INSTALL_DIR="/opt/psn00bsdk" VERSION_FILE=".version" RED='\033[0;31m' GREEN='\033[0;32m' YELLOW='\033[1;33m' BLUE='\033[0;34m' CYAN='\033[0;36m' NC='\033[0m' print_info() { echo -e "${GREEN}[INFO]${NC} $1" } print_warn() { echo -e "${YELLOW}[WARN]${NC} $1" } print_error() { echo -e "${RED}[ERROR]${NC} $1" } print_menu() { echo -e "${CYAN}$1${NC}" } check_dependencies() { local missing_deps=() for cmd in wget unzip curl jq; do if ! command -v $cmd &> /dev/null; then missing_deps+=($cmd) fi done if [ ${#missing_deps[@]} -ne 0 ]; then print_error "Missing required dependencies: ${missing_deps[*]}" print_info "Install with: sudo apt install ${missing_deps[*]} (Ubuntu/Debian)" print_info " sudo yum install ${missing_deps[*]} (RHEL/CentOS)" print_info " sudo pacman -S ${missing_deps[*]} (Arch Linux)" exit 1 fi } get_latest_version() { local latest_tag=$(curl -s "$GITHUB_API" | grep -o '"tag_name": *"[^"]*"' | cut -d'"' -f4) if [ -z "$latest_tag" ]; then print_error "Failed to fetch latest version from GitHub" return 1 fi echo "$latest_tag" } check_installed_version() { local install_dir="$1" if [ ! -f "$install_dir/$VERSION_FILE" ]; then echo "" return fi cat "$install_dir/$VERSION_FILE" } check_updates() { local install_dir="$1" if [ ! -d "$install_dir" ]; then print_warn "PSn00bSDK is not installed in: $install_dir" read -p "Enter installation directory: " install_dir if [ ! -d "$install_dir" ]; then print_error "Directory not found: $install_dir" return 1 fi fi print_info "Checking for latest release from GitHub..." local installed_version=$(check_installed_version "$install_dir") local latest_version=$(get_latest_version 2>/dev/null) if [ $? -ne 0 ] || [ -z "$latest_version" ]; then print_error "Failed to get latest version from GitHub" return 1 fi local latest_version_num=$(echo "$latest_version" | sed 's/^v//') echo "" echo "==========================================" echo " Version Check Results" echo "==========================================" echo "" echo "Installed version: ${installed_version:-${RED}Not detected${NC}}" echo "Latest version: ${latest_version_num}" echo "" if [ "$installed_version" = "$latest_version_num" ]; then print_info "You are running the latest version!" elif [ -z "$installed_version" ]; then print_warn "Could not detect installed version" else print_info "A newer version is available!" echo "" print_menu "Download the latest version from:" echo " $GITHUB_RELEASES" fi echo "==========================================" echo "" } install_sdk() { local install_dir="$1" local version="$2" if [ -z "$version" ]; then print_info "Checking for latest release from GitHub..." local latest_version=$(get_latest_version 2>/dev/null) if [ $? -ne 0 ] || [ -z "$latest_version" ]; then print_error "Failed to determine version to install" return 1 fi version=$(echo "$latest_version" | sed 's/^v//') print_info "Installing latest version: $version" fi local file_name="PSn00bSDK-${version}-Linux.zip" local download_url="https://github.com/Lameguy64/PSn00bSDK/releases/download/v${version}/${file_name}" local temp_dir=$(mktemp -d) if [ -d "$install_dir" ]; then print_warn "Installation directory already exists: $install_dir" read -p "Do you want to overwrite it? (y/N): " -n 1 -r echo if [[ ! $REPLY =~ ^[Yy]$ ]]; then print_error "Installation cancelled." rm -rf "$temp_dir" return 1 fi sudo rm -rf "$install_dir" fi print_info "Downloading PSn00bSDK v${version}..." print_info "URL: $download_url" if ! wget -O "${temp_dir}/${file_name}" "$download_url"; then print_error "Failed to download PSn00bSDK" rm -rf "$temp_dir" return 1 fi print_info "Extracting SDK to temporary directory..." unzip -q "${temp_dir}/${file_name}" -d "$temp_dir" local extracted_dir=$(find "$temp_dir" -maxdepth 1 -type d -name "PSn00bSDK-*" | head -n1) if [ -z "$extracted_dir" ]; then print_error "Failed to find extracted SDK directory" rm -rf "$temp_dir" return 1 fi print_info "Installing PSn00bSDK to: $install_dir" sudo mkdir -p "$install_dir" sudo cp -r "$extracted_dir"/* "$install_dir"/ print_info "Creating version file..." echo "$version" | sudo tee "$install_dir/$VERSION_FILE" > /dev/null print_info "Setting permissions..." sudo chown -R root:root "$install_dir" sudo chmod -R 755 "$install_dir" sudo chmod +x "$install_dir"/bin/* rm -rf "$temp_dir" print_info "Installation completed successfully!" return 0 } setup_environment() { local install_dir="$1" local profile_file="" local bashrc="$HOME/.bashrc" local zshrc="$HOME/.zshrc" if [ -n "$ZSH_VERSION" ]; then profile_file="$zshrc" elif [ -n "$BASH_VERSION" ]; then profile_file="$bashrc" else profile_file="$bashrc" fi local bin_dir="$install_dir/bin" local libs_dir="$install_dir/lib/libpsn00b" print_info "Configuring environment variables in: $profile_file" local env_block="# PSn00bSDK Environment Variables" if grep -q "$env_block" "$profile_file" 2>/dev/null; then print_warn "Environment variables already configured in $profile_file" print_info "Removing old configuration..." sed -i "/$env_block/,/# End PSn00bSDK/d" "$profile_file" fi { echo "" echo "$env_block" echo "export PATH="$bin_dir:$PATH"" echo "export PSN00BSDK_LIBS="$libs_dir"" echo "# End PSn00bSDK" } >> "$profile_file" print_info "Environment variables added to $profile_file" print_info "Please run: source $profile_file" } verify_installation() { local install_dir="$1" print_info "Verifying installation..." if [ ! -d "$install_dir/bin" ]; then print_error "bin directory not found in $install_dir" return 1 fi if [ ! -d "$install_dir/lib/libpsn00b" ]; then print_error "libpsn00b directory not found in $install_dir" return 1 fi if [ ! -f "$install_dir/bin/mipsel-none-elf-gcc" ]; then print_error "mipsel-none-elf-gcc not found" return 1 fi print_info "All checks passed!" return 0 } print_post_install() { local install_dir="$1" echo "" echo "==========================================" echo "PSn00bSDK Installation Complete!" echo "==========================================" echo "" echo "Installation directory: $install_dir" echo "" echo "To complete setup, please:" echo " 1. Source your shell configuration:" echo " source ~/.bashrc" echo " or" echo " source ~/.zshrc" echo "" echo " 2. Verify installation by running:" echo " mipsel-none-elf-gcc --version" echo " mkpsxiso --help" echo "" echo " 3. Create your first project using the template:" echo " cp -r $install_dir/share/psn00bsdk/template ~/my_project" echo " cd ~/my_project" echo " cmake --preset default ." echo " cmake --build ./build" echo "" echo "For more information, see:" echo " $install_dir/share/psn00bsdk/doc/README.md" echo "==========================================" } uninstall_sdk() { local install_dir="$1" if [ ! -d "$install_dir" ]; then print_warn "Installation directory not found: $install_dir" read -p "Enter installation directory path: " install_dir if [ ! -d "$install_dir" ]; then print_error "Directory not found: $install_dir" return 1 fi fi print_warn "This will remove PSn00bSDK from: $install_dir" read -p "Are you sure? (y/N): " -n 1 -r echo if [[ ! $REPLY =~ ^[Yy]$ ]]; then print_error "Uninstallation cancelled." return 1 fi sudo rm -rf "$install_dir" print_info "PSn00bSDK removed successfully!" return 0 } remove_environment() { local profile_file="" local bashrc="$HOME/.bashrc" local zshrc="$HOME/.zshrc" if [ -n "$ZSH_VERSION" ]; then profile_file="$zshrc" elif [ -n "$BASH_VERSION" ]; then profile_file="$bashrc" else profile_file="$bashrc" fi if [ -f "$profile_file" ]; then if grep -q "PSn00bSDK Environment Variables" "$profile_file" 2>/dev/null; then print_info "Removing PSn00bSDK environment variables from: $profile_file" sed -i "/# PSn00bSDK Environment Variables/,/# End PSn00bSDK/d" "$profile_file" print_info "Environment variables removed." else print_warn "No PSn00bSDK environment variables found in $profile_file" fi fi } show_menu() { echo "" echo "==========================================" echo " PSn00bSDK Manager" echo "==========================================" echo "" echo "Please select an action:" echo "" print_menu "1) Install PSn00bSDK" print_menu "2) Uninstall PSn00bSDK" print_menu "3) Check for updates" print_menu "4) Exit" echo "" read -p "Enter your choice [1-4]: " choice echo "" case $choice in 1) echo "" read -p "Enter version to install (leave blank for latest, e.g., 0.24): " version echo "" read -p "Enter installation directory [$DEFAULT_INSTALL_DIR]: " install_dir install_dir="${install_dir:-$DEFAULT_INSTALL_DIR}" if [[ ! $install_dir = /* ]]; then install_dir="$(pwd)/$install_dir" fi if install_sdk "$install_dir" "$version"; then setup_environment "$install_dir" verify_installation "$install_dir" print_post_install "$install_dir" fi ;; 2) echo "" read -p "Enter installation directory [$DEFAULT_INSTALL_DIR]: " install_dir install_dir="${install_dir:-$DEFAULT_INSTALL_DIR}" if uninstall_sdk "$install_dir"; then remove_environment echo "" print_info "Uninstallation complete!" print_info "Please run: source ~/.bashrc (or ~/.zshrc) to update your shell" fi ;; 3) echo "" read -p "Enter installation directory [$DEFAULT_INSTALL_DIR]: " install_dir install_dir="${install_dir:-$DEFAULT_INSTALL_DIR}" check_updates "$install_dir" ;; 4) echo "Exiting..." exit 0 ;; *) print_error "Invalid choice. Please enter a number between 1 and 4." ;; esac } main() { check_dependencies while true; do show_menu echo "" read -p "Press Enter to continue or 'q' to quit: " -n 1 -r echo if [[ $REPLY =~ ^[Qq]$ ]]; then echo "Exiting..." exit 0 fi done } if [[ "${BASH_SOURCE[0]}" == "${0}" ]]; then main fi