#!/bin/bash
# Pipsity Connector installer for MetaTrader 5 (Mac).
# Downloads PipsityConnector.ex5 and puts it in MetaTrader 5's Experts folder.
# It changes nothing else. Safe to run again (updates the connector).
# If macOS says it can't be opened: right-click this file → Open → Open.
echo ""
echo "  Pipsity Connector for MetaTrader 5"
echo "  ----------------------------------"
echo ""
TMP="$(mktemp -t PipsityConnector).ex5"
if ! curl -fsSL "https://tj-mt5.pipsity.workers.dev/dl/PipsityConnector.ex5" -o "$TMP"; then
  echo "  Could not download the connector. Check your internet / VPN and run this again."
  read -n 1 -s -r -p "  Press any key to close."; exit 1
fi
# MetaTrader 5 for Mac keeps its files inside a Wine folder in your Library.
FOUND=0
while IFS= read -r -d '' d; do
  cp -f "$TMP" "$d/PipsityConnector.ex5" && echo "  Installed in: $d" && FOUND=1
done < <(find "$HOME/Library/Application Support" -maxdepth 9 -type d -path "*MQL5/Experts" -print0 2>/dev/null)
rm -f "$TMP"
if [ "$FOUND" = 0 ]; then
  echo "  No MetaTrader 5 found. Open MetaTrader 5 once, close it, and run this again."
  echo "  (Or in MetaTrader: File > Open Data Folder > MQL5 > Experts, and copy the file there.)"
  read -n 1 -s -r -p "  Press any key to close."; exit 1
fi
echo ""
echo "  Done! Now:"
echo "   1. Restart MetaTrader 5 (or right-click Expert Advisors in the Navigator > Refresh)."
echo "   2. Drag PipsityConnector from the Navigator onto any one chart and click OK."
echo "   3. Type the code it shows into the journal (menu > Auto-sync MetaTrader)."
echo ""
read -n 1 -s -r -p "  Press any key to close."
