#!/bin/bash

# PPT Add-in Installation Script for Mac
# This script downloads manifest.xml and installs it to the correct WEF directory
# Double-click this file to run it

set -e  # Exit on any error

# Get the directory where this script is located
SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"

# Open Terminal and run the installation
osascript <<EOF
tell application "Terminal"
    activate
    do script "cd '$SCRIPT_DIR' && echo '========================================' && echo 'PPT Add-in Installation for Mac' && echo '========================================' && echo '' && echo 'Current user: $(whoami)' && echo '' && echo 'Creating WEF directory...' && mkdir -p ~/Library/Containers/com.microsoft.Powerpoint/Data/Documents/wef && echo 'WEF directory created: ~/Library/Containers/com.microsoft.Powerpoint/Data/Documents/wef' && echo '' && echo 'Downloading manifest.xml from network...' && if curl -s -o ~/Library/Containers/com.microsoft.Powerpoint/Data/Documents/wef/manifest.xml 'https://xiaomishuppt.com/manifest.xml'; then echo 'manifest.xml downloaded successfully'; else echo 'Network download failed, trying local file...'; if [ -f '$SCRIPT_DIR/manifest_template.xml' ]; then cp '$SCRIPT_DIR/manifest_template.xml' ~/Library/Containers/com.microsoft.Powerpoint/Data/Documents/wef/manifest.xml; echo 'Using local manifest_template.xml'; else echo 'Error: Cannot get manifest.xml file!'; exit 1; fi; fi && echo '' && if [ -f ~/Library/Containers/com.microsoft.Powerpoint/Data/Documents/wef/manifest.xml ]; then echo 'manifest.xml successfully installed!' && ADDIN_ID=\$(grep -o '<Id>[^<]*</Id>' ~/Library/Containers/com.microsoft.Powerpoint/Data/Documents/wef/manifest.xml | sed 's/<Id>//;s/<\/Id>//') && if [ -n \"\$ADDIN_ID\" ]; then echo \"Add-in ID: \$ADDIN_ID\"; fi; else echo 'Error: manifest.xml was not created!'; exit 1; fi && echo '' && echo '========================================' && echo 'PPT Add-in Installation Complete!' && echo '========================================' && echo 'manifest.xml has been installed to the WEF directory' && echo 'Please restart PowerPoint and load the add-in' && echo '' && echo 'If add-in does not appear, please check:' && echo '1. PowerPoint has been restarted' && echo '2. Developer mode is enabled' && echo '3. Network connection is normal' && echo '4. WEF directory exists: ~/Library/Containers/com.microsoft.Powerpoint/Data/Documents/wef' && echo '========================================' && echo '' && echo 'Press any key to close this window...' && read -n 1 -s"
end tell
EOF

echo "Installation script launched in Terminal. Please follow the instructions in the Terminal window."
