#! /bin/sh
# debian12下，如果脚本错误，先执行下这个命令：ln -sf bash /bin/sh
# mkdir -p /home/server/switch/install/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 install start, linux system version: '$linux' ======'
    # 判定当前系统
    if [ "$linux" = "Unknown" ]; then
        error 'Unknown linux exit.'
        exit
    fi
    install_main
    notice '====== Freeswitch install end ======'
}

install_main () {
    if [ -d $fs_path ];then
        error "install failed! freeswitch already exist, delete it [$fs_path]".
        exit
    fi

    P=`pwd`
    if [ $P != "$run_path" ];then
        error "Please execute the script in the ["$run_path"] directory!"
        exit
    fi

    # 1. 安装基础引用库
    install_third_lib

    # 2. 安装freeswitch
    index_log 'install freeswitch'
    execute_shell "tar -xzvf freeswitch.tar.gz"
    tar -zxvf freeswitch.tar.gz &> unzipfreeswitch.txt
    if [ $? -ne 0 ]; then
        error "install failed! unzip freeswitch.tar.gz error, detail see unzipfreeswitch.txt."
        exit
    fi
    execute_shell "mv freeswitch $install_path"

    # 检查第三方库是否安装
    if [ "$linux" = "debian" ]; then
        mkdir -p /usr/lib/x86_64-linux-gnu/lua
        install_local_lib libpcre.so.3 libodbc.so.2 libspeexdsp.so.1 libspeex.so.1 libltdl.so.7 
        install_local_lib libtiff.so.6 libjpeg.so.62 libdeflate.so.0 libwebp.so.7 libLerc.so.4 libjbig.so.0
        install_local_lib liblua5.2.so.0 libx264.so.164
        install_local_lib libshout.so.3 libFLAC.so.8 libvorbis.so.0 libvorbisenc.so.2 libopus.so.0 libogg.so.0 libmpg123.so.0 libmp3lame.so.0
        install_local_lib libsndfile.so.1 libssl.so.1.1 libcrypto.so.1.1 libtheora.so.0

        if [ ! -d "/usr/lib/x86_64-linux-gnu/lua/5.2" ]; then
            unzip $run_path/5.2.zip
            mv 5.2 /usr/lib/x86_64-linux-gnu/lua
        fi
    fi

    execute_shell "ln -sf $fs_path/bin/freeswitch /usr/bin/"
    execute_shell "ln -sf $fs_path/bin/fs_cli /usr/bin/"
    # 3. 检查安装情况
    R=$($fs_path/bin/freeswitch -version)
    if [ ! -z "$R" ]; then
        notice "Freeswitch install succeed, $R!"
    else
        error "Freeswitch install failed!"
    fi
}

install_third_lib () {
   index_log ${FUNCNAME[0]}
    if [ "$linux" = "centos" ]; then
        yum_install libtiff-devel.x86_64 yasm libpng12 unzip nasm
        yum_install libshout-devel lame-devel libmpg123-devel
        yum_install git alsa-lib-devel autoconf automake bison broadvoice-devel bzip2 curl-devel
        yum_install libdb4-devel e2fsprogs-devel erlang flite-devel g722_1-devel gcc-c++ gdbm-devel
        yum_install gnutls-devel ilbc2-devel ldns-devel libcodec2-devel libcurl-devel libedit-devel
        yum_install libidn-devel libjpeg-devel libmemcached-devel libogg-devel libsilk-devel
        yum_install libsndfile-devel libtheora-devel libtiff-devel libtool libuuid-devel libvorbis-devel
        yum_install libxml2-devel lua-devel lzo-devel mongo-c-driver-devel ncurses-devel net-snmp-devel
        yum_install openssl-devel pcre-devel perl perl-ExtUtils-Embed pkgconfig portaudio-devel
        yum_install postgresql-devel python-devel python-devel soundtouch-devel speex-devel sqlite-devel
        yum_install unbound-devel unixODBC-devel wget which yasm zlib-devel libshout-devel libmpg123-devel
        yum_install lame-devel readline-devel libpq-dev
        yum_install pcre pcre-devel
        yum_install openssl11 openssl11-devel
        yum_install librabbitmq-devel
        yum_install mysql-connector-odbc
        yum_install liblua5.2 liblua5.2-dev lua-curl-dev lua-cjson-dev lua-socket-dev
        yum_install libatomic
    else
        apt_install gnupg2 wget autoconf lsb-release libtool libtool-bin libtiff-dev uuid-dev pkg-config openssl libssl-dev sqlite3 libsqlite3-dev
        apt_install libcurl4 openssl-dev libspeexdsp-dev libldns-dev libedit-dev yasm nasm ffmpeg libswscale-dev libavformat-dev
        apt_install libopus-dev libpq-dev libmariadb-dev unixodbc unixodbc-dev libsndfile1-dev
        apt_install libreadline-dev libpcre3-dev python perl python-dev
        apt_install libshout3-dev libmpg123-dev libmp3lame-dev
        apt_install librabbitmq-dev libjpeg-dev 
        apt_install liblua5.2 liblua5.2-dev lua-curl-dev lua-cjson-dev lua-socket-dev
    fi
}

install_local_lib(){
    for i in $@; do
        if [ ! -f "/lib/x86_64-linux-gnu/$i" ]; then
            index_log "install local lib $i"
            ln -sf $fs_path/lib/$i /lib/x86_64-linux-gnu/
        fi
    done
}

yum_install () {
    # 遍历参数
    for i in $@; do
        index_log "install third lib $i"
        # 检查是否已安装
        yum install -y $i
    done
}

apt_install () {
    # 遍历参数
    for i in $@; do
        index_log "install third lib $i"
        # 检查是否已安装
        apt-get install -y $i
    done
}

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/install_`date +%Y-%m-%d`.log
}

shell_main
