#!/usr/bin/env bash
# 文件: httpServiceStart.sh
# 功能: 临时启动HTTP服务，跨节点传输文件

# 终端颜色定义
txtbld=$(tput bold)             # Bold
bldred=${txtbld}$(tput setaf 1) # red
bldgre=${txtbld}$(tput setaf 2) # green
bldylw=${txtbld}$(tput setaf 3) # yellow
txtrst=$(tput sgr0)             # Reset
err=${bldred}ERROR${txtrst}
info=${bldgre}INFO${txtrst}
warn=${bldylw}WARNING${txtrst}

LISTEN_PORT=${1:-8848}
PYTHON_VERSION=$(python --version 2>&1)

if [[ $PYTHON_VERSION == "Python 2"* ]]
then
    printf "$info: HTTP服务启动,端口为 [ ${bldgre}${LISTEN_PORT}${txtrst} ]\n"
    python -m SimpleHTTPServer ${LISTEN_PORT} >/dev/null 2>/dev/null || printf "$err: 启动失败,请检查[${LISTEN_PORT}]端口是否被占用\n"

elif [[ $PYTHON_VERSION == "Python 3"* ]]
then

    printf "$info: HTTP服务启动,端口为 [ ${bldgre}${LISTEN_PORT}${txtrst} ]\n"
    python -m simple.http ${LISTEN_PORT} || printf "$err: 服务启动失败,请检查端口是否被占用"
else
    printf "$err: Python 环境异常,请检查!\n"
    exit 67
fi
