#! /bin/sh
# 1. freeswitch 配置项
server_name='acd136'
rocketmq_server='10.101.12.51:9876,10.101.12.52:9876,10.105.12.53:9876,10.101.12.54:9876,10.101.12.55:9876'
rocketmq_group=$server_name
rocketmq_access='cqtcloudcc'
rocketmq_secret='cqt@2020'

redis_addrs='10.101.12.41:9100,10.101.12.42:9100,10.101.12.43:9100,10.101.12.44:9100,10.101.12.45:9100,10.101.12.46:9100'
redis_psw='C@hjzx531sider'
recovery_addrs1='ws:10.101.12.108:15679'
recovery_addrs2='ws:10.101.12.109:15679'
base_server='http://10.101.12.224:8080/basecomapi/get_service_queue'
curl_server='http://10.101.12.224:8080/cloudcc-queue-control/call-in-ivr'

# 2. shell环境配置项
log_path='/home/server/logs/freeswitch'
run_path='/home/server/switch/install/freeswitch'
install_path=/home/server/switch
fs_path="$install_path/"freeswitch
##########################################################################################
shell_main () {
    mkdir -p $run_path/log
    linux=$(check_linux);
    notice '====== freeswitch init start, linux system version: '$linux' ======'
    # 判定当前系统
    if [ "$linux" == "Unknown" ]; then
        error 'Unknown linux exit.'
        exit
    fi
    install_main
    notice '====== freeswitch init end ======'
}

# 主程序
install_main () {
    P=`pwd`
    if [ $P != "$run_path" ];then
        error "请在["$run_path"]目录下执行脚本"
        exit
    fi

    # 1. 配置文件
    index_log '初始化配置文件'
    execute_shell "rm -rf $fs_path/conf/sip_profiles"
    execute_shell "rm -rf $fs_path/conf/dialplan"
    
    execute_shell "\cp -a $run_path/init/conf $fs_path"
    execute_shell "\cp -a $run_path/init/scripts $fs_path"
    execute_shell "\cp -a $run_path/init/sounds $fs_path"
    sed -i s/#lcc_server_name/$server_name/g $fs_path/conf/vars.xml
    sed -i s/#lcc_rocketmq_server/$rocketmq_server/g $fs_path/conf/vars.xml
    sed -i s/#lcc_rocketmq_group/$rocketmq_group/g $fs_path/conf/vars.xml
    sed -i s/#lcc_rocketmq_access/$rocketmq_access/g $fs_path/conf/vars.xml
    sed -i s/#lcc_rocketmq_secret/$rocketmq_secret/g $fs_path/conf/vars.xml
    sed -i s/#lcc_redis_addrs/$redis_addrs/g $fs_path/conf/vars.xml
    sed -i s/#lcc_redis_psw/$redis_psw/g $fs_path/conf/vars.xml
    sed -i s/#lcc_recovery_addrs1/$recovery_addrs1/g $fs_path/conf/vars.xml
    sed -i s/#lcc_recovery_addrs2/$recovery_addrs2/g $fs_path/conf/vars.xml
    sed -i s!#lcc_base_server!$base_server!g $fs_path/conf/vars.xml
    sed -i s!#lcc_curl_server!$curl_server!g $fs_path/conf/vars.xml

    # 2. lua脚本
    execute_shell "\cp -a $run_path/scripts $fs_path"
    # 3. 创建日志文件夹
    execute_shell "mkdir -p $log_path"
}

check_linux () {
    if [ -f /etc/os-release ]; then
        # Arch Linux, Manjaro, etc.
        echo $(grep '^ID=' /etc/os-release | cut -d= -f2-|cut -d '"' -f2)
    else
        echo "Unknown"
    fi
}

verbose () {
	echo "${green}$@${normal}"
    save_log "[INFO] "$@
}
error () {
	echo "${red}$@${normal}"
    save_log "[ERR] "$@
}
warning () {
	echo "${yellow}$@${normal}"
    save_log "[WARNING] "$@
}

notice () {
	echo "${blue}$@${normal}"
    save_log "[NOTICE] "$@
}

execute_shell() {
    if [ $# -gt 0 ]; then
        verbose "执行命令 ["$@"]"
        echo $@|sh
    fi
}

# check for color support
if test -t 1; then
    # see if it supports colors...
    ncolors=$(tput colors)

    if test -n "$ncolors" && test $ncolors -ge 8; then
        normal="$(tput sgr0)"
        red="$(tput setaf 1)"
        green="$(tput setaf 2)"
        yellow="$(tput setaf 3)"
        blue="$(tput setaf 4)"
    fi
fi

index=1
index_log() {
    notice $index". "$1
    index=`expr $index + 1`
}

save_log(){
    str_tmp="`date +%Y-%m-%d_%H:%M:%S` "
    while [ $# -ge 1 ];do
        str_tmp="$str_tmp $1"
        shift
    done
    echo $str_tmp >>$run_path/log/init_`date +%Y-%m-%d`.log
}

shell_main