#!/bin/bash 

#cd `dirname $0`
cd `dirname $0/.. >/dev/null 2>&1`
pwd_Home=`pwd`
Ng_Home=/usr/local/nginx

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

check(){
    if [ -d $pwd_Home/www ];then
        echo '····开始部署前端服务'
    elif [ -f $pwd_Home/www.zip ];then
        echo '····解压前端包'
        unzip www.zip  >/dev/null 2>&1
    else
        echo -e "\033[41;30m www包不存在，请把前端包放到$pwd_Home下\033[0m"
        exit 1
    fi
}

install(){
    cp -r $pwd_Home/www $Ng_Home/
    chmod -R 777 $Ng_Home/www/
    echo "····创建nginx配置文件"

cat > $Ng_Home/conf/conf.d/wechat.conf << EOF
location /front {
    alias /usr/local/nginx/www;
    expires 30d;
}
location /wechat {
    proxy_pass http://127.0.0.1:9996/;
}

EOF
    echo "····加载nginx配置文件"
    $Ng_Home/sbin/nginx -t
    $Ng_Home/sbin/nginx -s reload

}

info(){
    echo -e "\033[42;30m 【前端文件位置 $Ng_Home/www】\033[0m"
    echo -e "\033[42;30m 【Nginx安装在$Ng_Home/conf/conf.d/wechat.conf】\033[0m"
    echo -e "\033[42;30m 【把此地址通过外网域名放出去，才可以往下进行】\033[0m"
}

echo "【1】检测文件"
check
echo "【2】开始安装"
install
info
echo "【3】完成测试"
echo "····浏览器访问:http://IP/front"
