#!/bin/bash

# 当运行时， 可以传入额外的参数
##   ./runParams.sh [-h] [-c] [--remove] [-p proxy] [-f] [--version vx.y.z] [-l file]

#########color code#############
RED="31m"    # Error message
GREEN="32m"  # Success message
YELLOW="33m" # Warning message
BLUE="36m"   # Info message

###############color echo func#################
colorEcho() {
    echo -e "\033[${1}${@:2}\033[0m" 1>&2
}

Help() {
    cat - 1>&2 <<EOF
./addNginxProxy.sh [-tcp t] <FRONT_PROXY_PORT> <REVERSE_PROXY_IP> <REVERSE_PROXY_PORT>
./addNginxProxy.sh [-http] <REVERSE_PROXY_SERVER_NAME> <FRONT_PROXY_PORT> <REVERSE_PROXY_IP> <REVERSE_PROXY_PORT>
./addNginxProxy.sh [-h -help]
    -tcp/t <FRONT_PROXY_PORT>代表本机的端口 <REVERSE_PROXY_IP>代表反向代理的ip地址 <REVERSE_PROXY_PORT>代表反向代理的端口
    -http <REVERSE_PROXY_SERVER_NAME>代表转发的url <REVERSE_PROXY_IP>代表反向代理的ip地址 <REVERSE_PROXY_PORT>代表反向代理的端口
    -h/help  显示本说明
EOF
}

addPortForward() {
    #判断FRONT_PROXY_PORT是否被占用
    #判断REVERSE_PROXY_IP是否符合IP的规格
    #判断REVERSE_PROXY_PORT是否被占用
    ## 端口转发模板文件简单，直接写入此处！！
    cat > /etc/nginx/conf.d/stream/${FRONT_PROXY_PORT}_${REVERSE_PROXY_IP}::${REVERSE_PROXY_PORT}.conf <<EOF
server {
    listen ${FRONT_PROXY_PORT};
    proxy_pass ${REVERSE_PROXY_IP}:${REVERSE_PROXY_PORT};
}
EOF
    if [[ -a /etc/nginx/conf.d/stream/${FRONT_PROXY_PORT}_${REVERSE_PROXY_IP}::${REVERSE_PROXY_PORT}.conf ]]; then
        colorEcho ${GREEN} "端口转发配置成功！！"
        nginx -t && nginx -s reload 
        if [[ $? -eq 0 ]];then
            LOCAL_INTERNAL_IP=$(ip addr show eth0 | grep 'inet ' | awk '{print $2}' | cut -f1 -d'/')
            colorEcho ${GREEN} "nignx已成功重启"
            colorEcho ${BLUE} "端口转发为：   ${LOCAL_INTERNAL_IP}:${FRONT_PROXY_PORT} >>>> ${REVERSE_PROXY_IP}:${REVERSE_PROXY_PORT}"
        else
            colorEcho ${RED} "nginx重启失败，请检查是否端口已被使用！"
        fi
    else
        colorEcho ${RED} "端口转发文件添加失败，请手动添加！！"
        return 34
    fi
    return 0
}

addReverProxy() {
    #判断FRONT_PROXY_PORT是否被占用
    #判断REVERSE_PROXY_IP是否符合IP的规格
    #判断REVERSE_PROXY_PORT是否被占用
    # 读取反向代理的模板然后写入相应的特定反向代理文件
    if [[ -a /etc/nginx/conf.d/reverse_proxy_demo ]] 
    then
        cat > /etc/nginx/conf.d/${REVERSE_PROXY_SERVER_NAME}_${FRONT_PROXY_PORT}-${REVERSE_PROXY_IP}::${REVERSE_PROXY_PORT}.conf < /etc/nginx/conf.d/reverse_proxy_demo
    else
        colorEcho ${RED} "反向代理的模板文件丢失!!! 无法添加反向代理！"
        return 45
    fi
    
    if [[ -a /etc/nginx/conf.d/${REVERSE_PROXY_SERVER_NAME}_${FRONT_PROXY_PORT}-${REVERSE_PROXY_IP}::${REVERSE_PROXY_PORT}.conf ]]; then
        sed -i -e "s/\${REVERSE_PROXY_SERVER_NAME}/${REVERSE_PROXY_SERVER_NAME}/g" \
            -e "s/\${FRONT_PROXY_PORT}/${FRONT_PROXY_PORT}/g" \
            -e "s/\${REVERSE_PROXY_IP}/${REVERSE_PROXY_IP}/g" \
            -e "s/\${REVERSE_PROXY_PORT}/${REVERSE_PROXY_PORT}/g" \
        /etc/nginx/conf.d/${REVERSE_PROXY_SERVER_NAME}_${FRONT_PROXY_PORT}-${REVERSE_PROXY_IP}::${REVERSE_PROXY_PORT}.conf
        colorEcho ${GREEN} "反向代理文件添加成功！！"
        nginx -t && nginx -s reload
        if [[ $? -eq 0 ]];then
            colorEcho ${GREEN} "nignx已成功重启！！"
            colorEcho ${BLUE} "反向代理为：   ${REVERSE_PROXY_SERVER_NAME}:${FRONT_PROXY_PORT} >>>> ${REVERSE_PROXY_IP}:${REVERSE_PROXY_PORT}"
        else
            colorEcho ${RED} "nginx重启失败，请检查是否端口已被使用！"
        fi
    else
        colorEcho ${RED} "反向代理配置文件添加失败，请手动添加！！"
        return 34
    fi
    return 0
}

#########################

if [[ $# > 0 ]]
then
case "$1" in
-tcp | -t)
    if [[ $# == 4 ]]; then
        FRONT_PROXY_PORT="${2}"
        REVERSE_PROXY_IP="${3}"
        REVERSE_PROXY_PORT="${4}"
        addPortForward
    else
        colorEcho ${RED} "输入的参数有误，请重新输入！"
        return 123
    fi
    ;;
-h | --help)
    Help
    ;;
-http)
    if [[ $# == 5 ]]; then
        REVERSE_PROXY_SERVER_NAME="${2}"
        FRONT_PROXY_PORT="${3}"
        REVERSE_PROXY_IP="${4}"
        REVERSE_PROXY_PORT="${5}"
        addReverProxy
    else
        colorEcho ${RED} "输入的参数有误，请重新输入！"
        return 123
    fi
    ;;
*)
    # unknown option
    colorEcho ${RED} "输入的参数不正确！"
    Help
    return 1
    ;;
esac
fi
