#!/bin/bash

# 捕获 Ctrl + C 终止整个脚本的运行
function onCtrlC () {
    exec 3>&2          # 3 is now a copy of 2
    exec 2> /dev/null  # 2 now points to /dev/null
    kill ${bg_pids} ${progress_pid} >/dev/null 2>&1
    sleep 1            # sleep to wait for process to die
    exec 2>&3          # restore stderr to saved
    exec 3>&-          # close saved version
    echo
    echo -e "${c_bir}Ctrl+C is captured, exiting...\n${c_e}"
    exit 100
}


function infos() {
    # 判断要查询的域名是否在平台，domain.list文件每小时更新一次 -- task.sh
    res=`cat $data/domain.list | grep -w "$domain"`
    if [[ $res == '' ]]; then
        echo -e "${c_br}该域名[$domain]未在天翼平台配置，一个小时内新增的域名无法查询，退出...${c_e}"
        echo -e "${c_br}只接收一个参数，请输入要查询的域名\ne.g. infos www.ctyun.cn\n${c_e}"
        exit 247
    fi

    # ----------------------------------------------------------------------------------------
    # 获取域名信息 - part 1
    curl "https://dcp.ctcdn.cn/traffic-control-api/v2/access/list?access_version_id=1&domain=$domain" -H 'Authorization: Bearer eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJ1c2VyaWQiOjYzfQ.XZ_nNtRWoBRcPBM-bHAG_ciohkNh69n4AVHwV755r6Q' -H 'content-type: application/json;charset=UTF-8' -vo domain_info_1.log > domain_info_1.response 2>&1

    # 判断响应是否200
    cat $trash/domain_info_1.response | grep -Eq 'HTTP/(1.1|2) 200 OK'
    [[ $? -ne 0 ]] && { echo -e "${c_br}获取域名-part 1信息失败，退出...${c_e}"; exit 246; }

    python3 /usr/local/script/fanmf11/get_infos.py --domain_info_1 domain_info_1.log $domain
    # python3 /home/fanmf11/fanmf11/get_infos.py --domain_info_1 domain_info_1.log $domain
    r_code=$?
    if [[ $r_code -eq 205 ]]; then
        exit 205
    elif [[ $r_code -eq 201 ]]; then
        accessid=`cat info.log | awk -F ':' '$1==4 {print $2}'`
        curl "https://dcp.ctcdn.cn/traffic-control-api/v2/access/parseDetail?access_id=$accessid" -H 'Authorization: Bearer eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJ1c2VyaWQiOjYzfQ.XZ_nNtRWoBRcPBM-bHAG_ciohkNh69n4AVHwV755r6Q' -H 'content-type: application/json;charset=UTF-8' -vo domain_map_info.log > domain_map_info.response 2>&1
        # 判断响应是否200
        cat $trash/domain_map_info.response | grep -Eq 'HTTP/(1.1|2) 200 OK'
        [[ $? -ne 0 ]] && { echo -e "${c_br}获取域名-map信息失败，退出...${c_e}"; exit 206; }
        python3 /usr/local/script/fanmf11/get_infos.py --domain_map_info domain_map_info.log 0

    elif [[ $r_code -ne 0 ]]; then
        echo -e "${c_br}处理域名-part 1信息失败，退出...${c_e}"
        exit 242
    fi

    # ----------------------------------------------------------------------------------------
    # 获取域名信息 - part 2
    account=`cat info.log | awk -F ':' '$1==1 {print $2}'`
    accid=`cat info.log | awk -F ':' '$1==3 {print $2}'`

    curl 'https://bs.ctcdn.cn/api/v3/clientInfo/searchClientInfo' -H 'Authorization: Bearer eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJ1c2VyaWQiOjI1NH0.9Zw66R0R85avd92qzO-3KQ4DJ2zYXL4ght5bH41iTfA' -H 'content-type: application/json;charset=UTF-8' -X POST -d '{"clientInfo":[{"key":"clientCnname", "value": "'$account'"}],"columnList":["openTime", "accountType", "accountResource",  "accountEmail"]}' -vo domain_info_2.log > domain_info_2.response 2>&1

    # 判断响应是否200
    cat $trash/domain_info_2.response | grep -Eq 'HTTP/(1.1|2) 200 OK'
    [[ $? -ne 0 ]] && { echo -e "${c_br}获取域名信息-part 2失败，退出...${c_e}"; exit 245; }

    python3 /usr/local/script/fanmf11/get_infos.py --domain_info_2 domain_info_2.log $accid
    # python3 /home/fanmf11/fanmf11/get_infos.py --domain_info_2 domain_info_2.log $accid
    [[ $? -ne 0 ]] && { echo -e "${c_br}处理域名-part 2信息失败，退出...${c_e}"; exit 241; }

    # ----------------------------------------------------------------------------------------
    # 获取域名信息 - part 3
    curl "http://bs.ctcdn.cn/api/v3/manageDomain/list?partner=&sales_channel=&status=&productCode=&domain=$domain" -H 'Authorization: Bearer eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJ1c2VyaWQiOjI1NH0.9Zw66R0R85avd92qzO-3KQ4DJ2zYXL4ght5bH41iTfA' -H 'content-type: application/json;charset=UTF-8' -vo domain_info_3.log > domain_info_3.response 2>&1

    # 判断响应是否200
    cat $trash/domain_info_3.response | grep -Eq 'HTTP/(1.1|2) 200 OK'
    [[ $? -ne 0 ]] && { echo -e "${c_br}获取域名信息-part 3失败，退出...${c_e}"; exit 244; }

    python3 /usr/local/script/fanmf11/get_infos.py --domain_info_3 domain_info_3.log $accid
    # python3 /home/fanmf11/fanmf11/get_infos.py --domain_info_3 domain_info_3.log $accid
    [[ $? -ne 0 ]] && { echo -e "${c_br}处理域名-part 3信息失败，退出...${c_e}"; exit 240; }

    # ----------------------------------------------------------------------------------------
    # 获取父方案信息
    curl 'https://lasc-new.ctcdn.cn/v1/domain/internal/parent_draft' -H 'Authorization: Bearer eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJ1c2VyaWQiOjE4NH0.1Pgfnky-grT0MN2ic04PqMf32BdnF6iN_PMk3kNOzjY' -H 'content-type: application/json;charset=UTF-8' -vo parent.log  > parent.response 2>&1

    # 判断响应是否200
    cat $trash/parent.response | grep -Eq 'HTTP/(1.1|2) 200 OK'
    [[ $? -ne 0 ]] && { echo -e "${c_br}获取父方案信息失败，退出...${c_e}"; exit 234; }

    # ----------------------------------------------------------------------------------------
    # 获取资源池信息
    curl 'http://rap.ctcdn.cn/v2/rapApi/resourcePoolToResourceGroup' -H 'Authorization: Bearer eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJ1c2VyaWQiOjE1N30.IXVEAglOYm8bUInW4uXqDugBnd6POouBK8q4z_HItns' -H 'content-type: application/json;charset=UTF-8' -vo respool.log  > respool.response 2>&1

    # 判断响应是否200
    cat $trash/respool.response | grep -Eq 'HTTP/(1.1|2) 200 OK'
    [[ $? -ne 0 ]] && { echo -e "${c_br}获取资源池信息失败，退出...${c_e}"; exit 233; }

    # ----------------------------------------------------------------------------------------
    # 获取域名信息 - part 4
    curl "https://confmanager.ctcdn.cn/v1/domain/conf/query_union?precise_query=1&domain=$domain" -H 'Authorization: Bearer eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJ1c2VyaWQiOjQxNn0.KEFh_yg3J1y8sL7s3X_8jIR8GGq88A89b7J5YhVYVf8' -H 'content-type: application/json;charset=UTF-8' -vo domain_info_4.log > domain_info_4.response 2>&1

    # 判断响应是否200
    cat $trash/domain_info_4.response | grep -Eq 'HTTP/(1.1|2) 200 OK'
    [[ $? -ne 0 ]] && { echo -e "${c_br}获取域名信息-part 4失败，退出...${c_e}"; exit 243; }

    python3 /usr/local/script/fanmf11/get_infos.py --domain_info_4 domain_info_4.log $accid
    # python3 /home/fanmf11/fanmf11/get_infos.py --domain_info_4 domain_info_4.log $accid
    r_code=$?
    if [[ $r_code -eq 204 ]]; then
        # ----------------------------------------------------------------------------------------
        # 获取域名信息 - part 5
        curl "https://lasc-new.ctcdn.cn/v1/domain/conf/op_query_union?page_size=20&page=1&domain=$domain" -H 'Authorization: Bearer eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJ1c2VyaWQiOjE4NH0.1Pgfnky-grT0MN2ic04PqMf32BdnF6iN_PMk3kNOzjY' -H 'content-type: application/json;charset=UTF-8' -vo domain_info_5.log > domain_info_5.response 2>&1

        # 判断响应是否200
        cat $trash/domain_info_5.response | grep -Eq 'HTTP/(1.1|2) 200 OK'
        [[ $? -ne 0 ]] && { echo -e "${c_br}获取域名信息-part 5失败，退出...${c_e}"; exit 235; }

        python3 /usr/local/script/fanmf11/get_infos.py --domain_info_5 domain_info_5.log $accid
        # python3 /home/fanmf11/fanmf11/get_infos.py --domain_info_5 domain_info_5.log $accid
        [[ $? -ne 0 ]] && { echo -e "${c_br}处理域名-part 5信息失败，退出...${c_e}"; exit 237; }

        # ----------------------------------------------------------------------------------------
        # 获取域名信息 - part 6
        domain_id=`cat info.log | awk -F ':' '$1==4 {print $2}'`
        curl "https://lasc-new.ctcdn.cn/v1/domain/internal/domain_config?domain_id=$domain_id" -H 'Authorization: Bearer eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJ1c2VyaWQiOjE4NH0.1Pgfnky-grT0MN2ic04PqMf32BdnF6iN_PMk3kNOzjY' -H 'content-type: application/json;charset=UTF-8' -vo domain_info_6.log > domain_info_6.response 2>&1

        # 判断响应是否200
        cat $trash/domain_info_6.response | grep -Eq 'HTTP/(1.1|2) 200 OK'
        [[ $? -ne 0 ]] && { echo -e "${c_br}获取域名信息-part 6失败，退出...${c_e}"; exit 238; }

        python3 /usr/local/script/fanmf11/get_infos.py --domain_info_6 domain_info_6.log $accid
        # python3 /home/fanmf11/fanmf11/get_infos.py --domain_info_6 domain_info_6.log $accid
        [[ $? -ne 0 ]] && { echo -e "${c_br}处理域名-part 6信息失败，退出...${c_e}"; exit 236; }

    elif [[ $r_code -ne 0 ]]; then
        echo -e "${c_br}处理域名-part 4信息失败，退出...${c_e}"
        exit 239
    else
        exit 0
    fi
}

# map.sh用如下函数获取解析组信息
function map() {
    # ----------------------------------------------------------------------------------------
    # 获取域名信息 - part 1 - 其中包括解析组信息，但有可能是重叠域名
    curl "https://dcp.ctcdn.cn/traffic-control-api/v2/access/list?access_version_id=1&domain=$domain" -H 'Authorization: Bearer eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJ1c2VyaWQiOjYzfQ.XZ_nNtRWoBRcPBM-bHAG_ciohkNh69n4AVHwV755r6Q' -H 'content-type: application/json;charset=UTF-8' -vo map_info.log > map_info.response 2>&1

    # 判断响应是否200
    cat map_info.response | grep -Eq 'HTTP/(1.1|2) 200 OK'
    [[ $? -ne 0 ]] && { echo -e "${c_br}获取域名MAP信息失败，退出...${c_e}"; exit 232; }

    # 处理接口获取的信息，拿到正确的解析组
    python3 /usr/local/script/fanmf11/get_infos.py --map_info map_info.log $domain
    # python3 /home/fanmf11/fanmf11/get_infos.py --map_info map_info.log $domain
    r_code=$?
    if [[ $r_code -eq 205 ]]; then
        exit 205
    elif [[ $r_code -eq 201 ]]; then
        accessid=`cat info.log | awk -F ':' '$1==4 {print $2}'`
        curl "https://dcp.ctcdn.cn/traffic-control-api/v2/access/parseDetail?access_id=$accessid" -H 'Authorization: Bearer eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJ1c2VyaWQiOjYzfQ.XZ_nNtRWoBRcPBM-bHAG_ciohkNh69n4AVHwV755r6Q' -H 'content-type: application/json;charset=UTF-8' -vo domain_map_info.log > domain_map_info.response 2>&1
        # 判断响应是否200
        cat $trash/domain_map_info.response | grep -Eq 'HTTP/(1.1|2) 200 OK'
        [[ $? -ne 0 ]] && { echo -e "${c_br}获取域名-map信息失败，退出...${c_e}"; exit 206; }
        python3 /usr/local/script/fanmf11/get_infos.py --domain_map_info domain_map_info.log 1

    elif [[ $r_code -ne 0 ]]; then
        echo -e "${c_br}处理域名MAP信息失败，退出...${c_e}"
        exit 231
    fi
}

function parent() {
    # ----------------------------------------------------------------------------------------
    # 获取父方案信息
    curl 'https://lasc-new.ctcdn.cn/v1/domain/internal/parent_draft' -H 'Authorization: Bearer eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJ1c2VyaWQiOjE4NH0.1Pgfnky-grT0MN2ic04PqMf32BdnF6iN_PMk3kNOzjY' -H 'content-type: application/json;charset=UTF-8' -vo parent.log  > parent.response 2>&1

    # 判断响应是否200
    cat $trash/parent.response | grep -Eq 'HTTP/(1.1|2) 200 OK'
    [[ $? -ne 0 ]] && { echo -e "${c_br}获取父方案信息失败，退出...${c_e}"; exit 234; }

    # ----------------------------------------------------------------------------------------
    # 获取域名信息 - part 4
    accid=`cat ../$parent_dir/info.log | awk -F ':' '$1==3 {print $2}'`
    curl "https://confmanager.ctcdn.cn/v1/domain/conf/query_union?precise_query=1&domain=$domain" -H 'Authorization: Bearer eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJ1c2VyaWQiOjQxNn0.KEFh_yg3J1y8sL7s3X_8jIR8GGq88A89b7J5YhVYVf8' -H 'content-type: application/json;charset=UTF-8' -vo domain_info_4.log > domain_info_4.response 2>&1

    # 判断响应是否200
    cat $trash/domain_info_4.response | grep -Eq 'HTTP/(1.1|2) 200 OK'
    [[ $? -ne 0 ]] && { echo -e "${c_br}获取域名信息-part 4失败，退出...${c_e}"; exit 243; }

    python3 /usr/local/script/fanmf11/get_infos.py --parent_info_4 domain_info_4.log $accid
    # python3 /home/fanmf11/fanmf11/get_infos.py --domain_info_4 domain_info_4.log $accid
    r_code=$?
    if [[ $r_code -eq 204 ]]; then
        # ----------------------------------------------------------------------------------------
        # 获取域名信息 - part 5
        curl "https://lasc-new.ctcdn.cn/v1/domain/conf/op_query_union?page_size=20&page=1&domain=$domain" -H 'Authorization: Bearer eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJ1c2VyaWQiOjE4NH0.1Pgfnky-grT0MN2ic04PqMf32BdnF6iN_PMk3kNOzjY' -H 'content-type: application/json;charset=UTF-8' -vo domain_info_5.log > domain_info_5.response 2>&1

        # 判断响应是否200
        cat $trash/domain_info_5.response | grep -Eq 'HTTP/(1.1|2) 200 OK'
        [[ $? -ne 0 ]] && { echo -e "${c_br}获取域名信息-part 5失败，退出...${c_e}"; exit 235; }

        python3 /usr/local/script/fanmf11/get_infos.py --parent_info_5 domain_info_5.log $accid
        # python3 /home/fanmf11/fanmf11/get_infos.py --domain_info_5 domain_info_5.log $accid
        [[ $? -ne 0 ]] && { echo -e "${c_br}处理域名-part 5信息失败，退出...${c_e}"; exit 237; }

        # ----------------------------------------------------------------------------------------
        # 获取域名信息 - part 6
        domain_id=`cat info.log | awk -F ':' '$1==2 {print $2}'`
        curl "https://lasc-new.ctcdn.cn/v1/domain/internal/domain_config?domain_id=$domain_id" -H 'Authorization: Bearer eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJ1c2VyaWQiOjE4NH0.1Pgfnky-grT0MN2ic04PqMf32BdnF6iN_PMk3kNOzjY' -H 'content-type: application/json;charset=UTF-8' -vo domain_info_6.log > domain_info_6.response 2>&1

        # 判断响应是否200
        cat $trash/domain_info_6.response | grep -Eq 'HTTP/(1.1|2) 200 OK'
        [[ $? -ne 0 ]] && { echo -e "${c_br}获取域名信息-part 6失败，退出...${c_e}"; exit 238; }

        python3 /usr/local/script/fanmf11/get_infos.py --parent_info_6 domain_info_6.log $accid
        # python3 /home/fanmf11/fanmf11/get_infos.py --domain_info_6 domain_info_6.log $accid
        [[ $? -ne 0 ]] && { echo -e "${c_br}处理域名-part 6信息失败，退出...${c_e}"; exit 236; }

    elif [[ $r_code -ne 0 ]]; then
        echo -e "${c_br}处理域名-part 4信息失败，退出...${c_e}"
        exit 239
    else
        :
    fi
}

# 自定义颜色显示
c_br='\e[1;31m'     # bold red
c_bg='\e[1;32m'     # bold green
c_by='\e[1;33m'     # bold yellow
c_bb='\e[1;34m'     # bold blue
c_bp='\e[1;35m'     # bold purple
c_bc='\e[1;36m'     # bold cyan
c_bir='\e[1;3;31m'  # * bold italic red
c_bib='\e[1;3;34m'  # * bold italic cyan
c_bic='\e[1;3;36m'  # bold italic cyan
c_e='\e[0m'         # reset

# 初始化变量
TS=`date +%s%N`
toolbox='/usr/local/script/fanmf11'                # *
data='/usr/local/script/fanmf11/data'              # *
host=`whoami`                                      # * 判断执行用户
trash="/usr/local/script/fanmf11/trash/$host/$TS"  # * 每个用户的临时文件存放处

if [[ -d $trash ]]; then
    echo -e "${c_br}对于同一个用户，同一时间只能运行一个实例，请重新运行...${c_e}"
    exit 245
else
    mkdir -p $trash
    cd $trash && cd ..
    docs=`ls`
    for doc in $docs; do
        [[ -f $doc ]] && rm -rf $doc
    done
    folders=`ls -t`
    while [[ `echo $folders | awk '{print NF}'` -gt 29 ]]; do
        folder=`ls -t | tail -1`
        rm -rf $folder
        folders=`ls -t`
    done
    cd $trash && touch infos
fi

# 捕获Ctrl+C时触发
trap 'onCtrlC' INT

# ----------------------------------------------------------------------------------------
# 判断入参数量是否合法
if [[ $# -eq 1 ]]; then
    domain=$1
    infos
elif [[ $# -eq 3 && $1 == '--map' ]]; then
    domain=$2 && map_dir=$3
    map
    cp map.log ../$map_dir/
    cp info.log ../$map_dir/
elif [[ $# -eq 3 && $1 == '--parent' ]]; then
    domain=$2 && parent_dir=$3
    parent
    cp ./cmap ../$parent_dir/
else
    echo -e "${c_br}只接收一个参数，请输入要查询的域名\ne.g. infos www.ctyun.cn\n${c_e}"
    exit 249
fi

