#!/bin/bash

# Build and deploy script for build.valuecurve.co
# Usage: ./init.sh [build|deploy|all]

set -e

SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
APP_DIR="$SCRIPT_DIR/apps/homepage"
SERVER="root@46.224.190.110"
REMOTE_PATH="/var/www/build.valuecurve.co"

# Colors for output
GREEN='\033[0;32m'
YELLOW='\033[1;33m'
RED='\033[0;31m'
NC='\033[0m' # No Color

build() {
    echo -e "${YELLOW}Building homepage...${NC}"
    cd "$APP_DIR"
    npm run build
    echo -e "${GREEN}Build complete!${NC}"
}

deploy() {
    echo -e "${YELLOW}Deploying to $SERVER...${NC}"

    if [ ! -d "$APP_DIR/build" ]; then
        echo -e "${RED}Error: Build folder not found. Run './init.sh build' first.${NC}"
        exit 1
    fi

    scp -r "$APP_DIR/build/"* "$SERVER:$REMOTE_PATH/"
    echo -e "${GREEN}Deployment complete!${NC}"
    echo -e "Site live at: https://build.valuecurve.co"
}

case "${1:-all}" in
    build)
        build
        ;;
    deploy)
        deploy
        ;;
    all)
        build
        deploy
        ;;
    *)
        echo "Usage: ./init.sh [build|deploy|all]"
        echo "  build  - Build the SvelteKit app"
        echo "  deploy - Deploy build folder to server"
        echo "  all    - Build and deploy (default)"
        exit 1
        ;;
esac
