#!/bin/bash
# Custom Application Installation Script (Production Profile)
# Example taskserv extension

set -euo pipefail

SETTINGS_FILE=${1:-""}
SERVER_POS=${2:-0}
TASKSERV_POS=${3:-0}
CURRENT_DIR=${4:-$(pwd)}

echo "Installing Custom Application (Production Profile)"
echo "Settings: $SETTINGS_FILE"
echo "Server Position: $SERVER_POS"
echo "TaskServ Position: $TASKSERV_POS"

# Source environment if available
if [ -f "$PROVISIONING_WK_ENV_PATH/cmd_env" ]; then
    source "$PROVISIONING_WK_ENV_PATH/cmd_env"
fi

# Example: Deploy production configuration
echo "Deploying production application..."

# Check if kubectl is available
if ! command -v kubectl &> /dev/null; then
    echo "Error: kubectl is required but not installed"
    exit 1
fi

# Check if docker is available
if ! command -v docker &> /dev/null; then
    echo "Error: docker is required but not installed"
    exit 1
fi

# Example deployment commands
cat << 'EOF' | kubectl apply -f -
apiVersion: apps/v1
kind: Deployment
metadata:
  name: custom-app-production
  namespace: production
spec:
  replicas: 3
  selector:
    matchLabels:
      app: custom-app
      env: production
  template:
    metadata:
      labels:
        app: custom-app
        env: production
    spec:
      containers:
      - name: custom-app
        image: registry.internal.com/custom-app:production
        ports:
        - containerPort: 8080
        env:
        - name: ENVIRONMENT
          value: "production"
        - name: DATABASE_URL
          valueFrom:
            secretKeyRef:
              name: custom-app-secrets
              key: database-url
---
apiVersion: v1
kind: Service
metadata:
  name: custom-app-service
  namespace: production
spec:
  selector:
    app: custom-app
    env: production
  ports:
  - port: 80
    targetPort: 8080
  type: LoadBalancer
EOF

echo "Custom Application deployed successfully in production"

# Wait for deployment to be ready
kubectl rollout status deployment/custom-app-production -n production --timeout=300s

echo "Custom Application is ready and running"