#!/bin/bash
# Build Nextcloud CLI tools with compile-time credentials
# Usage: ./build.sh <server-url> <username> <token>

set -e

# Check arguments
if [ $# -ne 3 ]; then
  echo "Usage: $0 <server-url> <username> <token>"
  echo ""
  echo "Example:"
  echo "  $0 https://teamworkapps.com wltbagent@shortcutsolutions.net YOUR_APP_TOKEN"
  exit 1
fi

SERVER_URL="$1"
USERNAME="$2"
TOKEN="$3"

echo "Building Nextcloud CLI tools with credentials..."
echo "Server: $SERVER_URL"
echo "User: $USERNAME"
echo "Token: ${TOKEN:0:10}..."
echo ""

# Build nextcloud-client
echo "Building nextcloud-client"
SCRIPT_DIR="$(cd "$(dirname "$0")" && pwd)"
cd "$SCRIPT_DIR/tools/go/nextcloud-client"
go build -ldflags="-X main.BuildServerURL=$SERVER_URL -X main.BuildUsername=$USERNAME -X main.BuildToken=$TOKEN" -o ~/bin/nextcloud-client .
echo "✓ nextcloud-client built successfully"

# Build nextcloud-contacts
echo "Building nextcloud-contacts..."
cd "$SCRIPT_DIR/tools/go/nextcloud-contacts"
go build -ldflags="-X main.BuildServerURL=$SERVER_URL -X main.BuildUsername=$USERNAME -X main.BuildToken=$TOKEN" -o ~/bin/nextcloud-contacts .
echo "✓ nextcloud-contacts built successfully"

# Build nextcloud-calendar
echo "Building nextcloud-calendar..."
cd "$SCRIPT_DIR/tools/go/nextcloud-calendar"
go build -ldflags="-X main.BuildServerURL=$SERVER_URL -X main.BuildUsername=$USERNAME -X main.BuildToken=$TOKEN" -o ~/bin/nextcloud-calendar .
echo "✓ nextcloud-calendar built successfully"

# Build nextcloud-capabilities
echo "Building nextcloud-capabilities..."
cd "$SCRIPT_DIR/../nextcloud-research/../tools/go/nextcloud-capabilities"
go build -ldflags="-X main.buildServer=$SERVER_URL -X main.buildUsername=$USERNAME -X main.buildAPIKey=$TOKEN" -o ~/bin/nextcloud-capabilities .
echo "✓ nextcloud-capabilities built successfully"

# Build nextcloud-notifications
echo "Building nextcloud-notifications..."
cd "$SCRIPT_DIR/../nextcloud-research/../tools/go/nextcloud-notifications"
go build -ldflags="-X main.buildServer=$SERVER_URL -X main.buildUsername=$USERNAME -X main.buildAPIKey=$TOKEN" -o ~/bin/nextcloud-notifications .
echo "✓ nextcloud-notifications built successfully"

# Build nextcloud-tasks
echo "Building nextcloud-tasks..."
cd "$SCRIPT_DIR/../nextcloud-research/../tools/go/nextcloud-tasks"
go build -ldflags="-X main.buildServer=$SERVER_URL -X main.buildUsername=$USERNAME -X main.buildAPIKey=$TOKEN" -o ~/bin/nextcloud-tasks .
echo "✓ nextcloud-tasks built successfully"

# Build nextcloud-talk
echo "Building nextcloud-talk..."
cd "$SCRIPT_DIR/../nextcloud-research/../tools/go/nextcloud-talk"
go build -ldflags="-X main.buildServer=$SERVER_URL -X main.buildUsername=$USERNAME -X main.buildAPIKey=$TOKEN" -o ~/bin/nextcloud-talk .
echo "✓ nextcloud-talk built successfully"

# Build nextcloud-mail
echo "Building nextcloud-mail..."
cd "$SCRIPT_DIR/tools/go/nextcloud-mail"
go build -ldflags="-X main.BuildIMAPServer=$SERVER_URL -X main.BuildIMAPPort=993 -X main.BuildIMAPUser=$USERNAME -X main.BuildIMAPPassword=$TOKEN -X main.BuildSMTPServer=$SERVER_URL -X main.BuildSMTPPort=465 -X main.BuildSMTPUser=$USERNAME -X main.BuildSMTPPassword=$TOKEN -X main.BuildUseSSL=true -X main.BuildIgnoreCerts=false" -o ~/bin/nextcloud-mail .
echo "✓ nextcloud-mail built successfully"

echo ""
echo "All tools built successfully!"
echo ""
echo "Binaries installed at:"
echo "  ~/bin/nextcloud-client"
echo "  ~/bin/nextcloud-contacts"
echo "  ~/bin/nextcloud-calendar"
echo "  ~/bin/nextcloud-capabilities"
echo "  ~/bin/nextcloud-notifications"
echo "  ~/bin/nextcloud-tasks"
echo "  ~/bin/nextcloud-talk"
echo "  ~/bin/nextcloud-mail"
echo ""
echo "Security Note: These binaries contain credentials in clear text."
echo "Do not distribute them outside your trusted environment."
