#!/bin/bash

# Function to print usage
usage() {
    echo "Usage: $0 [--aio|--base|--midware|--app|--uninstall]"
    echo "  --aio       Install and start all components in sequence"
    echo "  --base      Install base components"
    echo "  --midware   Start MySQL and Nginx"
    echo "  --app       Build and start application"
    echo "  --uninstall Uninstall acctlink"
    exit 1
}

# Function to detect architecture
detect_arch() {
    case $(uname -m) in
        x86_64)  echo "amd64" ;;
        aarch64) echo "arm64" ;;
        *) echo "unsupported" ;;
    esac
}

# Function to install base components
install_base() {
    ARCH=$(detect_arch)
    echo "Installing base components..."
    
    mkdir -p /data/acctlink/ && \
    wget -4 -O /data/acctlink/acctlink_$ARCH https://mirrors.infvie.org/account-docking/acctlink/acctlink_$ARCH && \
    cd /data/acctlink/ && \
    chmod +x acctlink_$ARCH && \
    [ -L "/usr/bin/acctlink_$ARCH" ] || ln -s /data/acctlink/acctlink_$ARCH /usr/bin/acctlink_$ARCH && \
    ./acctlink_$ARCH deps install && \
    ./acctlink_$ARCH install
    
    if [ $? -eq 0 ]; then
        echo "Base components installed successfully"
    else
        echo "Failed to install base components"
        exit 1
    fi
}

# Function to start middleware
start_middleware() {
    ARCH=$(detect_arch)
    echo "Starting middleware services..."
    
    cd /data/acctlink/ && \
    ./acctlink_$ARCH app create && \
    ./acctlink_$ARCH app start mysql && \
    ./acctlink_$ARCH app start nginx && \
    ./acctlink_$ARCH app ps && \
    ./acctlink_$ARCH report
    
    if [ $? -eq 0 ]; then
        echo "Middleware services started successfully"
    else
        echo "Failed to start middleware services"
        exit 1
    fi
}

# Function to build and start application
start_application() {
    ARCH=$(detect_arch)
    echo "Building and starting application..."
    
    cd /data/acctlink/ && \
    ./acctlink_$ARCH app build && \
    ./acctlink_$ARCH app start app && \
    ./acctlink_$ARCH app ps
    
    if [ $? -eq 0 ]; then
        echo "Application started successfully"
    else
        echo "Failed to start application"
        exit 1
    fi
}

# Function to uninstall acctlink
uninstall_acctlink() {
    ARCH=$(detect_arch)
    echo "Uninstalling acctlink..."
    
    cd /data/acctlink/ && \
    ./acctlink_$ARCH uninstall
    
    if [ $? -eq 0 ]; then
        echo "Acctlink uninstalled successfully"
    else
        echo "Failed to uninstall acctlink"
        exit 1
    fi
}

# Function to install and start all components
install_all() {
    echo "Starting All-In-One installation..."
    
    # Step 1: Install base components
    install_base
    
    # Step 2: Start middleware
    start_middleware
    
    # Step 3: Build and start application
    start_application
    
    echo "All-In-One installation completed successfully"
}

# Main script
if [ $# -eq 0 ]; then
    usage
fi

case "$1" in
    --base)
        install_base
        ;;
    --midware)
        start_middleware
        ;;
    --app)
        start_application
        ;;
    --uninstall)
        uninstall_acctlink
        ;;
    --aio)
        install_all
        ;;
    *)
        usage
        ;;
esac 