|
| 1 | +#!/bin/bash |
| 2 | + |
| 3 | +# Kolosal macOS App Launcher Script |
| 4 | +# This script is executed when the user double-clicks the Kolosal.app |
| 5 | +# It sets up PATH access and provides user-friendly setup |
| 6 | + |
| 7 | +# Get the directory where this script is located (Contents/MacOS) |
| 8 | +SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)" |
| 9 | +APP_CONTENTS="$(dirname "$SCRIPT_DIR")" |
| 10 | +APP_BUNDLE="$(dirname "$APP_CONTENTS")" |
| 11 | + |
| 12 | +# Paths to the executables |
| 13 | +KOLOSAL_EXECUTABLE="$SCRIPT_DIR/kolosal" |
| 14 | +KOLOSAL_SERVER_EXECUTABLE="$SCRIPT_DIR/kolosal-server" |
| 15 | + |
| 16 | +# Function to create symlinks in /usr/local/bin |
| 17 | +create_symlinks() { |
| 18 | + local target_dir="/usr/local/bin" |
| 19 | + |
| 20 | + # Check if /usr/local/bin exists and is writable, or if we can create it |
| 21 | + if [[ ! -d "$target_dir" ]]; then |
| 22 | + # Use osascript to run mkdir with admin privileges |
| 23 | + if ! osascript -e "do shell script \"mkdir -p '$target_dir'\" with administrator privileges" 2>/dev/null; then |
| 24 | + echo "Failed to create $target_dir directory" |
| 25 | + return 1 |
| 26 | + fi |
| 27 | + fi |
| 28 | + |
| 29 | + # Create both symlinks in a single osascript call to avoid double authentication |
| 30 | + if [[ -f "$KOLOSAL_EXECUTABLE" && -f "$KOLOSAL_SERVER_EXECUTABLE" ]]; then |
| 31 | + local combined_command="ln -sf '$KOLOSAL_EXECUTABLE' '$target_dir/kolosal' && ln -sf '$KOLOSAL_SERVER_EXECUTABLE' '$target_dir/kolosal-server'" |
| 32 | + if ! osascript -e "do shell script \"$combined_command\" with administrator privileges" 2>/dev/null; then |
| 33 | + echo "Failed to create symlinks" |
| 34 | + return 1 |
| 35 | + fi |
| 36 | + echo "✓ Created symlink: $target_dir/kolosal" |
| 37 | + echo "✓ Created symlink: $target_dir/kolosal-server" |
| 38 | + else |
| 39 | + echo "Error: One or both executables not found" |
| 40 | + return 1 |
| 41 | + fi |
| 42 | + |
| 43 | + return 0 |
| 44 | +} |
| 45 | + |
| 46 | +# Function to show notification |
| 47 | +show_notification() { |
| 48 | + local title="$1" |
| 49 | + local message="$2" |
| 50 | + |
| 51 | + osascript -e "display notification \"$message\" with title \"$title\"" 2>/dev/null || true |
| 52 | +} |
| 53 | + |
| 54 | +# Function to check if symlinks already exist and are correct |
| 55 | +check_symlinks() { |
| 56 | + local target_dir="/usr/local/bin" |
| 57 | + local kolosal_ok=false |
| 58 | + local server_ok=false |
| 59 | + |
| 60 | + # Check kolosal symlink |
| 61 | + if [[ -L "$target_dir/kolosal" ]]; then |
| 62 | + local current_target="$(readlink "$target_dir/kolosal")" |
| 63 | + if [[ "$current_target" == "$KOLOSAL_EXECUTABLE" ]]; then |
| 64 | + kolosal_ok=true |
| 65 | + fi |
| 66 | + fi |
| 67 | + |
| 68 | + # Check kolosal-server symlink |
| 69 | + if [[ -L "$target_dir/kolosal-server" ]]; then |
| 70 | + local current_target="$(readlink "$target_dir/kolosal-server")" |
| 71 | + if [[ "$current_target" == "$KOLOSAL_SERVER_EXECUTABLE" ]]; then |
| 72 | + server_ok=true |
| 73 | + fi |
| 74 | + fi |
| 75 | + |
| 76 | + # Return true only if both are correctly set up |
| 77 | + if [[ "$kolosal_ok" == true && "$server_ok" == true ]]; then |
| 78 | + return 0 |
| 79 | + fi |
| 80 | + |
| 81 | + return 1 |
| 82 | +} |
| 83 | + |
| 84 | +# Function to show setup dialog |
| 85 | +show_setup_dialog() { |
| 86 | + local message="Welcome to Kolosal CLI!\n\nTo use Kolosal commands (kolosal, kolosal-server) from any Terminal window, we can set up command line access.\n\nThis will:\n• Create symlinks in /usr/local/bin\n• Require administrator privileges\n• Make commands available globally\n\nAlternatively, you can skip this and use the executables directly from the app bundle." |
| 87 | + |
| 88 | + local response |
| 89 | + response=$(osascript -e "display dialog \"$message\" buttons {\"Skip\", \"Set Up Command Line Access\"} default button \"Set Up Command Line Access\" with title \"Kolosal Setup\" with icon note" 2>/dev/null) |
| 90 | + |
| 91 | + if [[ "$response" == *"Set Up Command Line Access"* ]]; then |
| 92 | + return 0 |
| 93 | + else |
| 94 | + return 1 |
| 95 | + fi |
| 96 | +} |
| 97 | + |
| 98 | +# Function to show alternative setup instructions |
| 99 | +show_manual_setup() { |
| 100 | + local manual_path="export PATH=\"$SCRIPT_DIR:\$PATH\"" |
| 101 | + local alias_kolosal="alias kolosal='$KOLOSAL_EXECUTABLE'" |
| 102 | + local alias_server="alias kolosal-server='$KOLOSAL_SERVER_EXECUTABLE'" |
| 103 | + |
| 104 | + local message="Command line setup was not completed.\n\nTo use Kolosal from Terminal manually, you can:\n\n1. Add to your shell profile (~/.zshrc or ~/.bash_profile):\n $manual_path\n\n2. Or create aliases:\n $alias_kolosal\n $alias_server\n\n3. Or run directly:\n $KOLOSAL_EXECUTABLE\n $KOLOSAL_SERVER_EXECUTABLE" |
| 105 | + |
| 106 | + osascript -e "display dialog \"$message\" buttons {\"Copy Path to Clipboard\", \"OK\"} default button \"OK\" with title \"Kolosal Manual Setup\"" 2>/dev/null |
| 107 | + |
| 108 | + if [[ $? -eq 0 ]]; then |
| 109 | + # Check if user clicked "Copy Path to Clipboard" |
| 110 | + local button_response |
| 111 | + button_response=$(osascript -e "display dialog \"$message\" buttons {\"Copy Path to Clipboard\", \"OK\"} default button \"OK\" with title \"Kolosal Manual Setup\"" 2>/dev/null) |
| 112 | + if [[ "$button_response" == *"Copy Path to Clipboard"* ]]; then |
| 113 | + echo "$SCRIPT_DIR" | pbcopy |
| 114 | + show_notification "Kolosal" "Path copied to clipboard!" |
| 115 | + fi |
| 116 | + fi |
| 117 | +} |
| 118 | + |
| 119 | +# Function to show success message and open terminal |
| 120 | +show_success() { |
| 121 | + local message="✅ Kolosal command line access is now set up!\n\nYou can now use these commands in any Terminal window:\n• kolosal --help\n• kolosal-server --help\n\nWould you like to open Terminal to try it out?" |
| 122 | + |
| 123 | + local response |
| 124 | + response=$(osascript -e "display dialog \"$message\" buttons {\"Later\", \"Open Terminal\"} default button \"Open Terminal\" with title \"Kolosal Setup Complete\"" 2>/dev/null) |
| 125 | + |
| 126 | + if [[ "$response" == *"Open Terminal"* ]]; then |
| 127 | + # Open terminal with helpful commands |
| 128 | + osascript -e 'tell application "Terminal" |
| 129 | + activate |
| 130 | + do script "echo \"🎉 Kolosal is now available! Try these commands:\"; echo \" kolosal --help\"; echo \" kolosal-server --help\"; echo \"\"" |
| 131 | + end tell' 2>/dev/null || true |
| 132 | + fi |
| 133 | +} |
| 134 | + |
| 135 | +# Main execution |
| 136 | +main() { |
| 137 | + # Check if this is being run from within the app bundle |
| 138 | + if [[ ! -f "$KOLOSAL_EXECUTABLE" ]]; then |
| 139 | + echo "Error: kolosal executable not found at $KOLOSAL_EXECUTABLE" |
| 140 | + show_notification "Kolosal Error" "Installation appears to be corrupted" |
| 141 | + exit 1 |
| 142 | + fi |
| 143 | + |
| 144 | + # Check if symlinks are already set up correctly |
| 145 | + if check_symlinks; then |
| 146 | + echo "✅ Kolosal commands are already accessible from Terminal" |
| 147 | + show_notification "Kolosal" "Commands are ready to use in Terminal" |
| 148 | + |
| 149 | + # Show reminder message |
| 150 | + local message="Kolosal commands are already set up and ready to use!\n\n• kolosal --help\n• kolosal-server --help\n\nWould you like to open Terminal?" |
| 151 | + local response |
| 152 | + response=$(osascript -e "display dialog \"$message\" buttons {\"No\", \"Open Terminal\"} default button \"Open Terminal\" with title \"Kolosal Ready\"" 2>/dev/null) |
| 153 | + |
| 154 | + if [[ "$response" == *"Open Terminal"* ]]; then |
| 155 | + osascript -e 'tell application "Terminal" |
| 156 | + activate |
| 157 | + do script "echo \"Kolosal commands are ready:\"; echo \" kolosal --help\"; echo \" kolosal-server --help\"; echo \"\"" |
| 158 | + end tell' 2>/dev/null || true |
| 159 | + fi |
| 160 | + else |
| 161 | + # Show setup dialog |
| 162 | + if show_setup_dialog; then |
| 163 | + echo "Setting up command line access..." |
| 164 | + |
| 165 | + if create_symlinks; then |
| 166 | + echo "✅ Successfully created command line access!" |
| 167 | + show_notification "Kolosal" "Setup complete! Commands available in Terminal" |
| 168 | + show_success |
| 169 | + else |
| 170 | + echo "❌ Failed to set up command line access" |
| 171 | + show_notification "Kolosal" "Setup failed - you may need administrator privileges" |
| 172 | + show_manual_setup |
| 173 | + fi |
| 174 | + else |
| 175 | + echo "User chose to skip automatic setup" |
| 176 | + show_notification "Kolosal" "App bundle is ready to use" |
| 177 | + show_manual_setup |
| 178 | + fi |
| 179 | + fi |
| 180 | +} |
| 181 | + |
| 182 | +# Run the main function |
| 183 | +main "$@" |
0 commit comments