#!/usr/bin/env bash

if [ ! -f ".env" ]; then
    cp env.example .env
fi

if [ ! -z "$SXZ_HTTP_PORT" ] && [ ! -z "$CONTAINER_NAME" ]; then
    echo "存在全局变量"
    rm .env
    cat >>.env <<EOF
SXZ_HTTP_PORT=$SXZ_HTTP_PORT
CONTAINER_NAME=$CONTAINER_NAME
EOF
    
else
    
    echo "不存在全局变量"
    source .env
fi

echo "http_port: $SXZ_HTTP_PORT"
echo "container_name: $CONTAINER_NAME"


if [ ! "$(docker ps -q -f name=$CONTAINER_NAME))" ]; then
    if [ "$(docker ps -aq -f status=exited -f name=$CONTAINER_NAME))" ]; then
        # cleanup
        docker rm $CONTAINER_NAME -f
    fi
    # run your container
    echo "no container and docker-compose up"
    docker-compose up -d
else
    if [ "$(docker ps -aq -f status=running -f name=$CONTAINER_NAME))" ]; then
        # cleanup
        docker rm $CONTAINER_NAME -f
    fi
    echo "has container and docker-compose up"
    $(which docker-compose) up -d
    
fi

# if [ "$(docker container inspect -f '{{.State.Status}}' $CONTAINER_NAME)" == "running" ]; then
#     docker rm $CONTAINER_NAME -f
#     docker-compose up -d
# else
#     docker-compose up -d
# fi
