#!/bin/bash
nginx_version="nginx-1.19.4"
openssl_version="openssl-openssl-3.0.0-alpha8"
nginx_prefix="/etc/nginx"
nginx_config="${nginx_prefix}/conf.d/"
nginx_is_installed=""

#判断是否已经安装
check_is_installed(){
    if [ -d $nginx_config ]; then
        nginx_is_installed=1
    else
        nginx_is_installed=0
    fi
}

remove_nginx(){
    systemctl stop nginx
    ${nginx_prefix}/sbin/nginx -s stop
    pkill -9 nginx
    systemctl disable nginx
    rm -rf /etc/systemd/system/nginx.service
    systemctl daemon-reload
    rm -rf ${nginx_prefix}
}

#安装nignx
install_nginx()
{
    green "正在编译和安装nginx。。。。"
    if ! wget -O ${nginx_version}.tar.gz https://nginx.org/download/${nginx_version}.tar.gz; then
        red    "获取nginx失败"
        yellow "按回车键继续或者按ctrl+c终止"
        read -s
    fi
    tar -zxf ${nginx_version}.tar.gz
    if ! wget -O ${openssl_version}.tar.gz https://github.com/openssl/openssl/archive/${openssl_version#*-}.tar.gz; then
        red    "获取openssl失败"
        yellow "按回车键继续或者按ctrl+c终止"
        read -s
    fi

    tar -zxf ${openssl_version}.tar.gz
    cd ${nginx_version}
    sed -i "s/OPTIMIZE[ \t]*=>[ \t]*'-O'/OPTIMIZE          => '-O3'/g" src/http/modules/perl/Makefile.PL
    
    ./configure \
    --prefix=${nginx_prefix} \
    --with-openssl=../$openssl_version \
    --with-openssl-opt="enable-ec_nistp_64_gcc_128 shared threads zlib-dynamic sctp" \
    --with-mail=dynamic \
    --with-mail_ssl_module \
    --with-stream=dynamic \
    --with-stream_ssl_module \
    --with-stream_realip_module \
    --with-stream_geoip_module=dynamic \
    --with-stream_ssl_preread_module \
    --with-http_ssl_module \
    --with-http_v2_module \
    --with-http_realip_module \
    --with-http_addition_module \
    --with-http_xslt_module=dynamic \
    --with-http_image_filter_module=dynamic \
    --with-http_geoip_module=dynamic \
    --with-http_sub_module \
    --with-http_dav_module \
    --with-http_flv_module \
    --with-http_mp4_module \
    --with-http_gunzip_module \
    --with-http_gzip_static_module \
    --with-http_auth_request_module \
    --with-http_random_index_module \
    --with-http_secure_link_module \
    --with-http_degradation_module \
    --with-http_slice_module \
    --with-http_stub_status_module \
    --with-http_perl_module=dynamic \
    --with-pcre \
    --with-libatomic \
    --with-compat \
    --with-cpp_test_module \
    --with-google_perftools_module \
    --with-file-aio \
    --with-threads \
    --with-poll_module \
    --with-select_module \
    --with-cc-opt="-Wno-error -g0 -O3"

# 详细的 nginx-module说明https://docs.nginx.com/nginx/admin-guide/installing-nginx/installing-nginx-open-source/#dependencies

    if ! make; then
        red    "nginx编译失败！"
        yellow "请尝试更换系统，建议使用Ubuntu最新版系统"
        exit 1
    fi

    remove_nginx
    make install
    cd ..
}

config_service_nginx(){
    systemctl disable nginx
    rm -rf /etc/systemd/system/nginx.service
    cat > /etc/systemd/system/nginx.service << EOF
[Unit]
Description=The NGINX HTTP and reverse proxy server
After=syslog.target network-online.target remote-fs.target nss-lookup.target
Wants=network-online.target

[Service]
Type=forking
User=root
ExecStartPre=/bin/rm -rf /dev/shm/nginx_unixsocket
ExecStartPre=/bin/mkdir /dev/shm/nginx_unixsocket
ExecStartPre=/bin/chmod 711 /dev/shm/nginx_unixsocket
ExecStartPre=/bin/rm -rf /dev/shm/nginx_tcmalloc
ExecStartPre=/bin/mkdir /dev/shm/nginx_tcmalloc
ExecStartPre=/bin/chmod 0777 /dev/shm/nginx_tcmalloc
ExecStart=${nginx_prefix}/sbin/nginx
ExecStop=${nginx_prefix}/sbin/nginx -s stop
ExecStopPost=/bin/rm -rf /dev/shm/nginx_tcmalloc
ExecStopPost=/bin/rm -rf /dev/shm/nginx_unixsocket
PrivateTmp=true

[Install]
WantedBy=multi-user.target
EOF
    chmod 0644 /etc/systemd/system/nginx.service
    systemctl daemon-reload
    systemctl enable nginx
}


main(){
    ## 检查nginx是否已经安装


}

main
