#!/bin/bash

# Create Emacsclient.app for macOS
# This script creates a proper .app bundle that uses emacsclient

APP_NAME="Emacsclient"
APP_DIR="/Applications/${APP_NAME}.app"
CONTENTS_DIR="${APP_DIR}/Contents"
MACOS_DIR="${CONTENTS_DIR}/MacOS"
RESOURCES_DIR="${CONTENTS_DIR}/Resources"

# Adjust these paths based on your system
EMACS_BIN="/usr/local/bin/emacs"
EMACSCLIENT_BIN="/usr/local/bin/emacsclient"

# For Apple Silicon Macs with Homebrew, use:
# EMACS_BIN="/opt/homebrew/bin/emacs"
# EMACSCLIENT_BIN="/opt/homebrew/bin/emacsclient"

echo "Creating ${APP_NAME}.app..."

# Create directory structure
mkdir -p "${MACOS_DIR}"
mkdir -p "${RESOURCES_DIR}"

# Create the launcher script
cat > "${MACOS_DIR}/${APP_NAME}" << EOF
#!/bin/bash

# Check if Emacs server is running
if ! ${EMACSCLIENT_BIN} -e "(boundp 'server-process)" 2>/dev/null | grep -q "t"; then
    # Start Emacs daemon if not running
    ${EMACS_BIN} --daemon
    # Wait a moment for daemon to start
    sleep 1
fi

# Open emacsclient with a new frame
# -c creates a new frame
# -n returns immediately without waiting
exec ${EMACSCLIENT_BIN} -c -n "\$@"
EOF

# Make the launcher executable
chmod +x "${MACOS_DIR}/${APP_NAME}"

# Create Info.plist
cat > "${CONTENTS_DIR}/Info.plist" << 'EOF'
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0">
<dict>
    <key>CFBundleExecutable</key>
    <string>Emacsclient</string>
    <key>CFBundleIconFile</key>
    <string>Emacs.icns</string>
    <key>CFBundleIdentifier</key>
    <string>org.gnu.Emacsclient</string>
    <key>CFBundleName</key>
    <string>Emacsclient</string>
    <key>CFBundlePackageType</key>
    <string>APPL</string>
    <key>CFBundleShortVersionString</key>
    <string>1.0</string>
    <key>CFBundleVersion</key>
    <string>1.0</string>
    <key>LSMinimumSystemVersion</key>
    <string>10.10</string>
    <key>NSHighResolutionCapable</key>
    <true/>
    <key>CFBundleDocumentTypes</key>
    <array>
        <dict>
            <key>CFBundleTypeExtensions</key>
            <array>
                <string>*</string>
            </array>
            <key>CFBundleTypeName</key>
            <string>All Files</string>
            <key>CFBundleTypeRole</key>
            <string>Editor</string>
        </dict>
    </array>
</dict>
</plist>
EOF

# Copy icon from existing Emacs.app if available
if [ -f "/Applications/Emacs.app/Contents/Resources/Emacs.icns" ]; then
    cp "/Applications/Emacs.app/Contents/Resources/Emacs.icns" "${RESOURCES_DIR}/"
    echo "Icon copied from Emacs.app"
elif [ -f "/opt/homebrew/Caskroom/emacs/*/Emacs.app/Contents/Resources/Emacs.icns" ]; then
    cp /opt/homebrew/Caskroom/emacs/*/Emacs.app/Contents/Resources/Emacs.icns "${RESOURCES_DIR}/"
    echo "Icon copied from Homebrew Emacs.app"
else
    echo "Warning: Could not find Emacs.icns icon file"
fi

echo "Successfully created ${APP_DIR}"
echo ""
echo "You can now:"
echo "1. Double-click Emacsclient in /Applications to launch"
echo "2. Add it to your Dock"
echo "3. Set it as default handler for text files"
echo "4. Open files with: open -a Emacsclient file.txt"
