#!/bin/bash

# 读取docker.conf文件
source docker.conf

# 设置系统环境变量
export WORKERMAN_CONTAINER_NAME=$container_name
export WORKERMAN_IMAGE_VERSION=$image_version

service_name="webman-jsonrpc"

# 检查docker-compose.yml文件是否存在
if [ ! -f "docker-compose.yml" ]; then
    echo "Error: docker-compose.yml file not found"
    exit 1
fi

# 定义帮助函数
show_help() {
    echo "Usage: ./run [COMMAND] [OPTION]"
    echo ""
    echo "Commands:"
    echo "  start         run once"
    echo "  status        service status(daemon mode only)"
    echo "  reload        reload files(daemon mode only)"
    echo "  restart       restart service(daemon mode only)"
    echo "  stop          stop service and container"
    echo "  init          delete container and rebuild"
    echo "  destroy       stop & remove the container"
    echo "  help          show this help message"
    echo "Options:"
    echo "  -d            run in daemon mode.(start, restart, init)"
}

# 定义安装依赖的函数
install_dependencies() {
    # 执行一次composer install
    docker compose run --rm "$service_name" composer install
}

# 判断容器是否存在并且正在运行
is_container_running() {
    if docker ps -a --format '{{.Names}}' | grep -q "$WORKERMAN_CONTAINER_NAME"; then
        if docker ps --format '{{.Names}}' | grep -q "$WORKERMAN_CONTAINER_NAME"; then
            return 0
        else
            return 1
        fi
    else
        return 2
    fi
}

# 判断是否需要在后台运行
is_daemon_mode() {
    for arg in "$@"; do
        if [[ "$arg" == "-d" ]]; then
            return 1
        fi
    done
    return 0
}

# 解析命令行参数
case "$1" in
start)
    # 判断容器是否存在并且正在运行
    if is_container_running; then
        echo "container $WORKERMAN_CONTAINER_NAME is already running"
        exit 1
    elif [[ $? -eq 1 ]]; then
        # 存在但未运行,直接启动
        if is_daemon_mode "$@"; then
            docker start -a "$WORKERMAN_CONTAINER_NAME"
        else
            docker start "$WORKERMAN_CONTAINER_NAME"
        fi
    else
        # 不存在,执行docker-compose up
        if is_daemon_mode "$@"; then
            docker compose up
        else
            docker compose up -d
        fi
    fi
    ;;
restart)
    # 判断容器是否存在并且正在运行
    if is_container_running; then
        if is_daemon_mode "$@"; then
            docker stop "$WORKERMAN_CONTAINER_NAME"
            docker start -a "$WORKERMAN_CONTAINER_NAME"
        else
            docker restart "$WORKERMAN_CONTAINER_NAME"
        fi
    elif [[ $? -eq 1 ]]; then
        # 存在但未运行,直接启动
        if is_daemon_mode "$@"; then
            docker start -a "$WORKERMAN_CONTAINER_NAME"
        else
            docker start "$WORKERMAN_CONTAINER_NAME"
        fi
    else
        # 不存在,执行docker-compose up
        if is_daemon_mode "$@"; then
            docker compose up
        else
            docker compose up -d
        fi
    fi
    ;;
stop)
    docker stop "$WORKERMAN_CONTAINER_NAME"
    ;;
reload)
    # 判断容器是否存在并且正在运行
    if is_container_running; then
        # 执行start.php reload命令
        docker exec -it "$WORKERMAN_CONTAINER_NAME" php start.php reload
    else
        echo "container $WORKERMAN_CONTAINER_NAME is not running"
        exit 1
    fi
    ;;
status)
    # 判断容器是否存在并且正在运行
    if is_container_running; then
        # 执行start.php status命令
        docker exec -it "$WORKERMAN_CONTAINER_NAME" php start.php status
    else
        echo "container $WORKERMAN_CONTAINER_NAME is not running"
        exit 1
    fi
    ;;
init)
    read -p "Will delete current container. Are you sure? (y/n)" confirm
    if [ "$confirm" != "y" ]; then
        echo "Aborted."
        exit 1
    fi
    docker rm -f "$WORKERMAN_CONTAINER_NAME"
    # 执行docker-compose up
    if is_daemon_mode "$@"; then
        docker compose up
    else
        docker compose up -d
    fi
    ;;
destroy)
    read -p "Are you sure you want to destroy the container? (y/n)" confirm
    if [ "$confirm" = "y" ]; then
        docker rm -f "$WORKERMAN_CONTAINER_NAME"
        echo "Container $WORKERMAN_CONTAINER_NAME has been destroyed."
    else
        echo "Aborted."
        exit 1
    fi
    ;;
help)
    show_help
    ;;
*)
    echo "Error: Invalid command"
    show_help
    exit 1
    ;;
esac