DSO_CORE_DEPS=%%CORE_DEPS%%
DSO_HTTP_DEPS=%%HTTP_DEPS%%
DSO_ALL_INCS=%%ALL_INCS%%


ALL_INCS=
CORE_INCS=
HTTP_INCS=
CORE_DEPS=
HTTP_DEPS=

NGX_OBJS=objs

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-include=*)               NGX_INCLUDE_PATH="$value"       ;;
        -s=*)                            NGX_INCLUDE_PATH="$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(absolute path)
    -s, --nginx-include=SOURCE set nginx include path(absolute path)

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

    cat << END                                                     > $NGX_DSO_MAKEFILE

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


CORE_LIBS = $CORE_LIBS
CORE_INCS = $CORE_INCS
HTTP_INCS = $HTTP_INCS

CORE_LINK = $CORE_LINK
NGX_LD_OPT = $NGX_LD_OPT

END


    cat << END                                                     >> $NGX_DSO_MAKEFILE

ALL_INCS = $ALL_INCS

CORE_DEPS = $CORE_DEPS

HTTP_DEPS = $HTTP_DEPS

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\$(ALL_INCS)$ngx_tab$ngx_objout$ngx_obj$ngx_tab$ngx_src$NGX_AUX

END

     done

    make -f $NGX_DSO_MAKEFILE

    if [ "$?" = '0' ]; then
	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
            NGX_DSO_DEST=$NGX_PREFIX${ngx_dirsep}$NGX_DSO_PATH
            [[ $NGX_DSO_PATH == /* ]] && NGX_DSO_DEST=$NGX_DSO_PATH
            test ! -f $NGX_DSO_DEST${ngx_dirsep}${dso_binout}${ngx_soext} \
		|| unlink $NGX_DSO_DEST${ngx_dirsep}${dso_binout}${ngx_soext}
            echo copying $NGX_OBJS${ngx_dirsep}${dso_binout}${ngx_soext} to $NGX_DSO_DEST
            cp $NGX_OBJS${ngx_dirsep}${dso_binout}${ngx_soext} $NGX_DSO_DEST
	fi
    fi
}


if test -n "$NGX_DSO_ADDONS"; then

    echo configuring additional modules

    for dai in $DSO_ALL_INCS
    do
        ALL_INCS="$ALL_INCS $dai"
    done

    ALL_INCS="$ALL_INCS -I$NGX_INCLUDE_PATH"

    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_INCLUDE_PATH/$dcd"
                    ;;
            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_INCLUDE_PATH/$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=

            CORE_LIBS_TEMP=$CORE_LIBS
            CORE_LIBS=

            cd $ngx_addon_dir

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

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

            mkdir -p $NGX_OBJS/auto
            touch $NGX_AUTOCONF_ERR
            touch $NGX_AUTOTEST
            touch $NGX_AUTO_CONFIG_H

            cat << END                                            > $NGX_OBJS/auto/feature
            $ngx_feature_file
END

            cat << END                                            > $NGX_OBJS/auto/have
            $ngx_have_file
END
	    cd $NGX_OBJS
            . $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 -

            cd $ngx_addon_dir
            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
