#!/bin/sh

# Copyright (C) 2010-2013 Alibaba Group Holding Limited


ngx_soext='.so'
CC=gcc
CFLAGS=' -pipe  -O -W -Wall -Wpointer-arith -Wno-unused-parameter -g -O2 -g  -DNDK_SET_VAR -fPIC'
CPP='gcc -E'
LINK='$(CC) -rdynamic  -fPIC  -shared '


CORE_LIBS='-Wl,-E  -lpthread -ldl -lcrypt -lpcre -lssl -lcrypto -ldl -lz -lxml2 -lxslt -lexslt -lgd -llua -lm -lGeoIP'
CORE_LINK=''

NGX_LD_OPT=''
NGX_PREFIX=/usr/share/nginx

NGX_RPATH=NO

ngx_regex_dirsep='\/'
ngx_cont=' \
	'
ngx_cc='$(CC) -c $(CFLAGS) $(CORE_INCS) $(HTTP_INCS)'
ngx_tab=' \
		'
ngx_objout='-o '
NGX_AUX=''
ngx_long_regex_cont=' \\\
	'
ngx_dirsep='/'
ngx_binext=''
ngx_long_start=''
ngx_long_cont=' \
	'
ngx_rcc=''
ngx_spacer='
'
ngx_objext='o'
ngx_regex_cont=' \\\
	'
ngx_include_opt='-I '
ngx_binout='-o '
NGX_DSO_PATH=modules/
NGX_AUTOCONF_ERR=objs/autoconf.err
NGX_AUTOTEST=objs/autotest
NGX_AUTO_CONFIG_H=objs/ngx_auto_config.h
NGX_CONFIGURE=" --user=nginx --group=nginx --prefix=/usr/share/nginx --sbin-path=/usr/sbin/tengine --conf-path=/etc/nginx/tengine.conf --error-log-path=/var/log/nginx/error.log --http-log-path=/var/log/nginx/access.log --http-client-body-temp-path=/var/lib/nginx/tmp/client_body --http-proxy-temp-path=/var/lib/nginx/tmp/proxy --http-fastcgi-temp-path=/var/lib/nginx/tmp/fastcgi --pid-path=/var/run/tengine.pid --lock-path=/var/lock/subsys/tengine --with-http_ssl_module --with-http_realip_module --with-http_addition_module --with-http_sub_module --with-http_dav_module --with-http_flv_module --with-http_gzip_static_module --with-http_stub_status_module --with-http_geoip_module --with-http_slice_module --with-http_secure_link_module --with-http_degradation_module --with-http_sysguard_module --with-http_concat_module --with-http_mp4_module --with-mail --with-mail_ssl_module --with-debug --with-ipv6 --with-http_xslt_module --with-http_image_filter_module --with-http_random_index_module --with-http_sysguard_module=shared --with-http_charset_filter_module=shared --with-http_userid_filter_module=shared --with-http_footer_filter_module=shared --with-http_referer_module=shared --with-http_rewrite_module=shared --with-http_fastcgi_module=shared --with-http_uwsgi_module=shared --with-http_scgi_module=shared --with-http_memcached_module=shared --with-http_limit_conn_module=shared --with-http_limit_req_module=shared --with-http_empty_gif_module=shared --with-http_browser_module=shared --with-http_user_agent_module=shared --with-http_upstream_ip_hash_module=shared --with-http_upstream_least_conn_module=shared --with-http_upstream_session_sticky_module=shared --with-http_split_clients_module=shared --with-http_lua_module --with-cc-opt='-O2 -g' --with-file-aio --with-syslog"

DSO_ALL_INCS=' -I src/core  -I src/event  -I src/event/modules  -I src/os/unix  -I src/proc  -I src/http/modules/lua/api  -I /usr/include/libxml2  -I objs  -I src/http  -I src/http/modules  -I src/mail'
DSO_CORE_DEPS=' src/core/nginx.h  src/core/ngx_config.h  src/core/ngx_core.h  src/core/ngx_log.h  src/core/ngx_palloc.h  src/core/ngx_array.h  src/core/ngx_list.h  src/core/ngx_hash.h  src/core/ngx_buf.h  src/core/ngx_queue.h  src/core/ngx_string.h  src/core/ngx_parse.h  src/core/ngx_inet.h  src/core/ngx_file.h  src/core/ngx_crc.h  src/core/ngx_crc32.h  src/core/ngx_murmurhash.h  src/core/ngx_md5.h  src/core/ngx_sha1.h  src/core/ngx_rbtree.h  src/core/ngx_trie.h  src/core/ngx_segment_tree.h  src/core/ngx_radix_tree.h  src/core/ngx_slab.h  src/core/ngx_times.h  src/core/ngx_shmtx.h  src/core/ngx_connection.h  src/core/ngx_cycle.h  src/core/ngx_conf_file.h  src/core/ngx_resolver.h  src/core/ngx_open_file_cache.h  src/core/ngx_crypt.h  src/event/ngx_event.h  src/event/ngx_event_timer.h  src/event/ngx_event_posted.h  src/event/ngx_event_busy_lock.h  src/event/ngx_event_connect.h  src/event/ngx_event_pipe.h  src/os/unix/ngx_time.h  src/os/unix/ngx_errno.h  src/os/unix/ngx_alloc.h  src/os/unix/ngx_files.h  src/os/unix/ngx_channel.h  src/os/unix/ngx_shmem.h  src/os/unix/ngx_process.h  src/os/unix/ngx_setaffinity.h  src/os/unix/ngx_setproctitle.h  src/os/unix/ngx_atomic.h  src/os/unix/ngx_gcc_atomic_x86.h  src/os/unix/ngx_thread.h  src/os/unix/ngx_socket.h  src/os/unix/ngx_os.h  src/os/unix/ngx_user.h  src/os/unix/ngx_pipe.h  src/os/unix/ngx_sysinfo.h  src/os/unix/ngx_process_cycle.h  src/os/unix/ngx_linux_config.h  src/os/unix/ngx_linux.h  src/os/unix/ngx_syslog.h  src/proc/ngx_proc.h  src/event/ngx_event_openssl.h  src/core/ngx_regex.h  objs/ngx_auto_config.h'
DSO_CORE_INCS=' -I src/core  -I src/event  -I src/event/modules  -I src/os/unix  -I src/proc  -I src/http/modules/lua/api  -I /usr/include/libxml2  -I objs'
DSO_HTTP_DEPS=' src/http/ngx_http.h  src/http/ngx_http_request.h  src/http/ngx_http_config.h  src/http/ngx_http_core_module.h  src/http/ngx_http_cache.h  src/http/ngx_http_variables.h  src/http/ngx_http_script.h  src/http/ngx_http_upstream.h  src/http/ngx_http_upstream_round_robin.h  src/http/ngx_http_busy_lock.h  src/http/modules/ngx_http_ssi_filter_module.h  src/http/modules/ngx_http_ssl_module.h  src/http/modules/lua/ddebug.h  src/http/modules/lua/ngx_http_lua_script.h  src/http/modules/lua/ngx_http_lua_log.h  src/http/modules/lua/ngx_http_lua_subrequest.h  src/http/modules/lua/ngx_http_lua_ndk.h  src/http/modules/lua/ngx_http_lua_control.h  src/http/modules/lua/ngx_http_lua_time.h  src/http/modules/lua/ngx_http_lua_string.h  src/http/modules/lua/ngx_http_lua_misc.h  src/http/modules/lua/ngx_http_lua_variable.h  src/http/modules/lua/ngx_http_lua_output.h  src/http/modules/lua/ngx_http_lua_headers.h  src/http/modules/lua/ngx_http_lua_uri.h  src/http/modules/lua/ngx_http_lua_req_body.h  src/http/modules/lua/ngx_http_lua_args.h  src/http/modules/lua/ngx_http_lua_ctx.h  src/http/modules/lua/ngx_http_lua_regex.h  src/http/modules/lua/ngx_http_lua_common.h  src/http/modules/lua/ngx_http_lua_directive.h  src/http/modules/lua/ngx_http_lua_headers_out.h  src/http/modules/lua/ngx_http_lua_headers_in.h  src/http/modules/lua/ngx_http_lua_consts.h  src/http/modules/lua/ngx_http_lua_exception.h  src/http/modules/lua/ngx_http_lua_util.h  src/http/modules/lua/ngx_http_lua_cache.h  src/http/modules/lua/ngx_http_lua_contentby.h  src/http/modules/lua/ngx_http_lua_rewriteby.h  src/http/modules/lua/ngx_http_lua_accessby.h  src/http/modules/lua/ngx_http_lua_setby.h  src/http/modules/lua/ngx_http_lua_capturefilter.h  src/http/modules/lua/ngx_http_lua_clfactory.h  src/http/modules/lua/ngx_http_lua_pcrefix.h  src/http/modules/lua/ngx_http_lua_headerfilterby.h  src/http/modules/lua/ngx_http_lua_shdict.h  src/http/modules/lua/ngx_http_lua_socket_tcp.h  src/http/modules/lua/api/ngx_http_lua_api.h  src/http/modules/lua/ngx_http_lua_logby.h  src/http/modules/lua/ngx_http_lua_sleep.h  src/http/modules/lua/ngx_http_lua_coroutine.h  src/http/modules/lua/ngx_http_lua_bodyfilterby.h  src/http/modules/lua/ngx_http_lua_initby.h  src/http/modules/lua/ngx_http_lua_socket_udp.h  src/http/modules/lua/ngx_http_lua_req_method.h  src/http/modules/lua/ngx_http_lua_phase.h  src/http/modules/lua/ngx_http_lua_probe.h  src/http/modules/lua/ngx_http_lua_uthread.h'
DSO_HTTP_INCS=' -I src/http  -I src/http/modules'


ALL_INCS=
CORE_INCS=
CORE_DEPS=
HTTP_INCS=
HTTP_DEPS=

NGX_SOURCE_HOME='/home/abuild/rpmbuild/BUILD/tengine-1.4.6'

opt=
help=no

for option
do
    opt="$opt `echo $option | sed -e \"s/\(--[^=]*=\)\(.* .*\)/\1'\2'/\"`"

    case "$option" in
        -*=*) value=`echo "$option" | sed -e 's/[-_a-zA-Z0-9]*=//'` ;;
           *) value="" ;;
    esac

    case "$option" in
        --help)                          help=yes                       ;;
        -h)                              help=yes                       ;;

        --dst=)                          NGX_DSO_PREFIX="!"             ;;
        --dst=*)                         NGX_DSO_PREFIX="$value"        ;;
        -d=*)                            NGX_DSO_PREFIX="$value"        ;;

        --add-module=*)                  NGX_DSO_ADDONS="$NGX_DSO_ADDONS $value" ;;
        -a=*)                            NGX_DSO_ADDONS="$NGX_DSO_ADDONS $value" ;;

        --nginx-source=*)                NGX_SOURCE_HOME="$value"       ;;
        -s=*)                            NGX_SOURCE_HOME="$value"       ;;

        *)
            echo "$0: error: invalid option \"$option\""
            exit 1
        ;;
    esac
done


if ! test -n "$option" ; then
    help=yes
fi


if [ $help = yes ]; then

cat << END

    -h, --help                display this help and exit
    -d, --dst=PATH            set module installation path
    -a, --add-module=PATH     external module which will be compiled
    -s, --nginx-source=SOURCE set nginx source root

END

    exit 1
fi



# arg1 is addon dir
# arg2 is addon deps
# arg3 is addon src

generate_make () {
    # mkdir build temp
    cd $1
    dso_binout=$ngx_addon_name
    NGX_DSO_MAKEFILE=objs/Makefile
    NGX_ADDON_DEPS=$2
    NGX_OBJS=objs

    if test -d $NGX_OBJS; then
        rm -rf $NGX_OBJS
    fi

    mkdir -p $NGX_OBJS

    cat << END                                                     > $NGX_DSO_MAKEFILE

CC = $CC
CFLAGS = $CFLAGS
CPP = $CPP
LINK = $LINK


CORE_LIBS = $CORE_LIBS
CORE_LINK = $CORE_LINK
NGX_LD_OPT = $NGX_LD_OPT

END


    cat << END                                                     >> $NGX_DSO_MAKEFILE

ALL_INCS = $ALL_INCS

CORE_DEPS = $CORE_DEPS

CORE_INCS = $CORE_INCS

HTTP_DEPS = $HTTP_DEPS


HTTP_INCS = $HTTP_INCS

ADDON_DEPS = \$(CORE_DEPS) $NGX_ADDON_DEPS

END


    if test -n "$NGX_LD_OPT$CORE_LIBS"; then
        ngx_libs=`echo $NGX_LD_OPT $CORE_LIBS \
            | sed -e "s/\//$ngx_regex_dirsep/g" -e "s/^/$ngx_long_regex_cont/"`
    fi

    for ngx_src in $NGX_ADDON_SRCS
    do
        ngx_obj="objs/src/`basename \`dirname $ngx_src\``"

        test -d $ngx_obj || mkdir -p $ngx_obj

        ngx_obj=`echo $ngx_obj/\`basename $ngx_src\` | sed -e "s/\//$ngx_regex_dirsep/g"`

        ngx_obj=`echo $ngx_obj \
            | sed -e "s#^\(.*\.\)cpp\\$#$ngx_objs_dir\1$ngx_objext#g" \
                  -e "s#^\(.*\.\)cc\\$#$ngx_objs_dir\1$ngx_objext#g" \
                  -e "s#^\(.*\.\)c\\$#$ngx_objs_dir\1$ngx_objext#g" \
                  -e "s#^\(.*\.\)S\\$#$ngx_objs_dir\1$ngx_objext#g"`

        ngx_dso_all_objs="$ngx_dso_all_objs $ngx_obj"

    done

    ngx_deps=`echo $ngx_dso_all_objs $ngx_res $LINK_DEPS \
        | sed -e "s/  *\([^ ][^ ]*\)/$ngx_regex_cont\1/g" \
              -e "s/\//$ngx_regex_dirsep/g"`

    ngx_objs=`echo $ngx_dso_all_objs \
        | sed -e "s/  *\([^ ][^ ]*\)/$ngx_long_regex_cont\1/g" \
              -e "s/\//$ngx_regex_dirsep/g"`

    ngx_link=${CORE_LINK:+`echo $CORE_LINK \
        | sed -e "s/\//$ngx_regex_dirsep%%/g" -e "s/^/$ngx_long_regex_cont/"`}

    cat << END                                                    >> $NGX_DSO_MAKEFILE

$NGX_OBJS${ngx_dirsep}${dso_binout}${ngx_soext}:	$ngx_deps$ngx_spacer
	\$(LINK) ${ngx_long_start}${ngx_binout} $NGX_OBJS${ngx_dirsep}${dso_binout}${ngx_soext}$ngx_long_cont$ngx_objs$ngx_libs$ngx_link
	$ngx_rcc
${ngx_long_end}
END

    for ngx_src in $NGX_ADDON_SRCS
    do
        ngx_obj="objs/src/`basename \`dirname $ngx_src\``"

        test -d $ngx_obj || mkdir -p $ngx_obj

        ngx_obj=`echo $ngx_obj/\`basename $ngx_src\` | sed -e "s#/#$ngx_regex_dirsep#g"`

        ngx_obj=`echo $ngx_obj \
            | sed -e "s#^\(.*\.\)cpp\\$#$ngx_objs_dir\1$ngx_objext#g" \
                  -e "s#^\(.*\.\)cc\\$#$ngx_objs_dir\1$ngx_objext#g" \
                  -e "s#^\(.*\.\)c\\$#$ngx_objs_dir\1$ngx_objext#g" \
                  -e "s#^\(.*\.\)S\\$#$ngx_objs_dir\1$ngx_objext#g"`

        ngx_src=`echo $ngx_src | sed -e "s#/#$ngx_regex_dirsep#g"`


        cat << END                                            >> $NGX_DSO_MAKEFILE

$ngx_obj:	\$(ADDON_DEPS)$ngx_cont$ngx_src
	$ngx_cc$ngx_tab$ngx_objout$ngx_obj$ngx_tab$ngx_src$NGX_AUX

END

     done

    make -f $NGX_DSO_MAKEFILE
    if test -n "$NGX_DSO_PREFIX"; then
        test ! -f $NGX_DSO_PREFIX${dso_binout}${ngx_soext} \
            || unlink $NGX_DSO_PREFIX${dso_binout}${ngx_soext}
        echo copying $NGX_OBJS${ngx_dirsep}${dso_binout}${ngx_soext} to $NGX_DSO_PREFIX
        cp $NGX_OBJS${ngx_dirsep}${dso_binout}${ngx_soext} $NGX_DSO_PREFIX
    else
        test ! -f $NGX_PREFIX${ngx_dirsep}$NGX_DSO_PATH${ngx_dirsep}${dso_binout}${ngx_soext} \
            || unlink $NGX_PREFIX${ngx_dirsep}$NGX_DSO_PATH${ngx_dirsep}${dso_binout}${ngx_soext}
        echo copying $NGX_OBJS${ngx_dirsep}${dso_binout}${ngx_soext} to $NGX_PREFIX${ngx_dirsep}$NGX_DSO_PATH
        cp $NGX_OBJS${ngx_dirsep}${dso_binout}${ngx_soext} $NGX_PREFIX/$NGX_DSO_PATH
    fi
}


if test -n "$NGX_DSO_ADDONS"; then

    # compile nginx
    cd $NGX_SOURCE_HOME
    ./configure $NGX_CONFIGURE
    cd -

    echo configuring additional modules

    for dai in $DSO_ALL_INCS
    do
        if [ "$dai" = "-I" ]; then
            ALL_INCS="$ALL_INCS $dai"
        elif test -n "$dai"; then
            case ".$dai" in
                ./*)
                    ALL_INCS="$ALL_INCS $dai"
                    ;;

                *)
                    ALL_INCS="$ALL_INCS $NGX_SOURCE_HOME/$dai"
                    ;;
            esac
        fi
    done

    for dcd in $DSO_CORE_DEPS
    do
        if test -n "$dcd"; then
            case ".$dcd" in
                ./*)
                    CORE_DEPS="$CORE_DEPS $dcd"
                    ;;

                *)
                    CORE_DEPS="$CORE_DEPS $NGX_SOURCE_HOME/$dcd"
                    ;;
            esac
        fi
    done

    for dci in $DSO_CORE_INCS
    do
        if [ "$dci" = "-I" ]; then
            CORE_INCS="$CORE_INCS $dci"
        elif test -n "$dci"; then
            case ".$dci" in
                ./*)
                    CORE_INCS="$CORE_INCS $dci"
                    ;;

                *)
                    CORE_INCS="$CORE_INCS $NGX_SOURCE_HOME/$dci"
                    ;;
            esac
        fi
    done

    for dhi in $DSO_HTTP_INCS
    do
        if [ "$dhi" = "-I" ]; then
            HTTP_INCS="$HTTP_INCS $dhi"
        elif test -n "$dhi"; then
            case ".$dhi" in
                ./*)
                    HTTP_INCS="$HTTP_INCS $dhi"
                    ;;

                *)
                    HTTP_INCS="$HTTP_INCS $NGX_SOURCE_HOME/$dhi"
                    ;;
            esac
        fi
    done

    for dhd in $DSO_HTTP_DEPS
    do
        if test -n "$dhd"; then
            case ".$dhd" in
                ./*)
                    HTTP_DEPS="$HTTP_DEPS $dhd"
                    ;;

                *)
                    HTTP_DEPS="$HTTP_DEPS $NGX_SOURCE_HOME/$dhd"
                    ;;
            esac
        fi
    done

    for ngx_addon_dir in $NGX_DSO_ADDONS
    do
        echo "adding module in $ngx_addon_dir"
        if test -f $ngx_addon_dir/config; then
            NGX_ADDON_SRCS=
            NGX_ADDON_DEPS=
            cd $NGX_SOURCE_HOME

            CORE_INCS_TEMP=$CORE_INCS
            HTTP_INCS_TEMP=$HTTP_INCS
            CORE_LIBS_TEMP=$CORE_LIBS
            CORE_INCS=
            HTTP_INCS=
            CORE_LIBS=

            . $ngx_addon_dir/config

            dso_core_incs=`echo $CORE_INCS \
                | sed -e "s/  *\([^ ][^ ]*\)/$ngx_regex_cont$ngx_include_opt\1/g" \
                -e "s/\//$ngx_regex_dirsep/g"`

            dso_http_incs=`echo $HTTP_INCS \
                | sed -e "s/  *\([^ ][^ ]*\)/$ngx_regex_cont$ngx_include_opt\1/g" \
                -e "s/\//$ngx_regex_dirsep/g"`

            dso_core_libs=`echo $CORE_LIBS \
                | sed -e "s/\//$ngx_regex_dirsep/g"`

            if test -n "$dso_core_incs"; then
                CORE_INCS="$CORE_INCS_TEMP -I $dso_core_incs"
            else
                CORE_INCS=$CORE_INCS_TEMP
            fi

            if test -n "$dso_http_incs"; then
                HTTP_INCS="$HTTP_INCS_TEMP -I $dso_http_incs"
            else
                HTTP_INCS=$HTTP_INCS_TEMP
            fi

            if test -n "$dso_core_libs"; then
                CORE_LIBS="$CORE_LIBS_TEMP $dso_core_libs"
            else
                CORE_LIBS=$CORE_LIBS_TEMP
            fi

            cd -
            echo " + $ngx_addon_name will be compiled"
            generate_make $ngx_addon_dir $NGX_ADDON_DEPS $NGX_ADDON_SRCS $ngx_addon_name
        else
            echo "$0: error: no $ngx_addon_dir/config was found"
            exit 1
        fi
    done
else
    echo "please specify the module path"
    exit 1
fi

