#
# Buildtool Generic Script
#

defs() {
    BT_REQUIRE="0.15"

    BT_PKG_NAME="screws"
    BT_PKG_VERSION="0.52"
    BT_PKG_LICENSE="GPLv2"

    BT_PKG_MAINTAINER="pancake@phreaker.net"
    BT_PKG_HOMEPAGE="http://www.nopcode.org/?t=screws"
    BT_PKG_COMMENT="SCRipt Enhaced Web Server"

    BT_PKG_DISTFMTS="tar.gz tar.bz2"
}

docs() {
    bt_doc "PEOPLE" "Who made the program"
    bt_doc "README" "General documentation"
    bt_doc "TODO" "Missing things"
	#bt_doc "doc\/tutorial" "Tutorial"
    #bt_doc "Screws Configuration" "doc/server/configuration"
}

config_init() {
    bt_feature IPV6 'yes' 'Whether to include IPv6 support (yes, no)'
    bt_msg_summary 'IPv6 support: ${BT_FEATURE_IPV6}'

    bt_feature LOGS 'yes' 'Build with logs support (yes, no)'
    bt_msg_summary 'Logs support: ${BT_FEATURE_LOGS}'

    bt_feature SSL 'auto' 'Compile with SSL support (auto, yes, no)'
    bt_msg_summary 'SSL suppport: ${BT_FEATURE_SSL}'

    bt_dir DOC '${BT_PREFIX}/share/doc/screws' 'Documentation directory.'
    bt_dir DIP '${BT_PREFIX}/share/screws/hsml-inc' 'Default include path.'

    # Change the default libexec directory.
    BT_DIR_LIBEXEC='${BT_PREFIX}/libexec/screws'
}

config() {
    bt_check_env_c
    bt_check_lib_howto

    bt_check_prog_sh
    bt_check_progs BT_PROG_PERL   "perl5 perl"
    bt_check_progs BT_PROG_PYTHON "python2.2 python"
    bt_check_progs BT_PROG_TCC    "tcc"

    if [ ${BT_FEATURE_SHARED} != no ] && bt_check_lib "dl"; then
        BT_LIBS+=-ldl
    fi

    if [ ${BT_FEATURE_SSL} != no ]; then
        bt_check_lib_c "crypto"
        bt_check_lib_c "ssl"
    else
        BT_FEATURE_SSL=no
    fi

    bt_check_func "poll"
    # The following two lines are for compatibility with plain build system.
    BT_HAVE_FUNC_POLL="$?"
    bt_subst BT_HAVE_FUNC_POLL

    bt_generate_configh
}

logic() {
    bt_target screwsd exec hsml parselogs screwsctl doc

    [ ${BT_FEATURE_SHARED} != no ] && \
        bt_target BasicExec Hello noupdir unicode testmodule

    target_screwsd() {
        BT_DIR_BIN=${BT_DIR_SBIN}
        BT_SUBDIR=${BT_TOPDIR}/src/Server
        BT_TYPE=program

        BT_FLAGS_CPP+='-DPREFIX=\"${BT_PREFIX}\"'
        BT_FLAGS_CPP+='-DVERSION=\"${BT_PKG_VERSION}\"'
        BT_FLAGS_CPP+='-DEXEC=\"${BT_DIR_LIBEXEC}/basic.sh\"'
        BT_FLAGS_CPP+='-DCFGFILE=\"${BT_DIR_ETC}/screws.conf\"'
        BT_FLAGS_CPP+='-DLOGFILE=\"${BT_DIR_VAR}/log/access.log\"'
        BT_FLAGS_CPP+='-DERRFILE=\"${BT_DIR_VAR}/var/error.log\"'

        if [ ${BT_HAVE_FUNC_POLL} = 0 ]; then
            BT_FLAGS_CPP+=-DPOLL=1
        else
            BT_FLAGS_CPP+=-DPOLL=0
        fi

        if [ ${BT_FEATURE_IPV6} != no ]; then
            BT_FLAGS_CPP+=-DIPV6=1
        else
            BT_FLAGS_CPP+=-DIPV6=0
        fi

        if [ ${BT_FEATURE_LOGS} != no ]; then
            BT_FLAGS_CPP+=-DLOGS=1
        else
            BT_FLAGS_CPP+=-DLOGS=0
        fi

        if [ ${BT_FEATURE_SSL} != no ]; then
            BT_FLAGS_CPP+=-DUSESSL=1
        else
            BT_FLAGS_CPP+=-DUSESSL=0
        fi

        if [ ${BT_FEATURE_SHARED} != no ]; then
            BT_FLAGS_CPP+=-DDLOPEN=1
        else
            BT_FLAGS_CPP+=-DDLOPEN=0
        fi

        if [ ${BT_FEATURE_DEVELOPER} = yes ]; then
            BT_FLAGS_CPP+=-DDEBUG=1
        else
            BT_FLAGS_CPP+=-DDEBUG=0
        fi

        BT_SOURCES=Accept.c
        BT_SOURCES+=Args.c
        BT_SOURCES+=Config.c
        BT_SOURCES+=Engine.c
        BT_SOURCES+=Env.c
        BT_SOURCES+=Error.c
        BT_SOURCES+=Fmutex.c
        BT_SOURCES+=IO_buffer.c
        BT_SOURCES+=IO_direct.c
        BT_SOURCES+=IO_poll.c
        BT_SOURCES+=Logs.c
        BT_SOURCES+=Modules.c
        BT_SOURCES+=Request.c
        BT_SOURCES+=Signal.c
        BT_SOURCES+=Ssl.c
        BT_SOURCES+=main.c
    }

    target_exec() {
        BT_SUBDIR=${BT_TOPDIR}/src/Exec
        BT_DEPENDS=linksys
        BT_TYPE=null
    }

    target_exec_install() {
        bt_install_dir ${BT_DIR_LIBEXEC}
        bt_install_program obsd-qos.x ${BT_DIR_LIBEXEC}
        bt_install_program perl.pl ${BT_DIR_LIBEXEC}
        bt_install_dir ${BT_DIR_LIBEXEC}/htdocs
        ( cd htdocs && pax -rw -pp -pm . ${BT_DIR_LIBEXEC}/htdocs )
        bt_install_dir ${BT_DIR_LIBEXEC}/perl
        ( cd htdocs && pax -rw -pp -pm . ${BT_DIR_LIBEXEC}/perl )
        bt_install_dir ${BT_DIR_LIBEXEC}/sh
        ( cd htdocs && pax -rw -pp -pm . ${BT_DIR_LIBEXEC}/sh )
    }

    target_linksys() {
        BT_DIR_BIN=${BT_DIR_LIBEXEC}
        BT_SUBDIR=${BT_TOPDIR}/src/Exec/c
        BT_TYPE=program
        BT_SOURCES=linksys.c
    }

    target_hsml() {
        BT_SUBDIR=${BT_TOPDIR}/src/Hsml
        BT_TYPE=program

        BT_FLAGS_CPP+='-DVERSION=\"1.20\"'
        BT_FLAGS_CPP+='-DPATH_PERL=\"${BT_PROG_PERL}\"'
        BT_FLAGS_CPP+='-DPATH_PYTHON=\"${BT_PROG_PYTHON}\"'
        BT_FLAGS_CPP+='-DPATH_SH=\"${BT_PROG_SH}\"'
        BT_FLAGS_CPP+='-DPATH_LUA=\"${BT_PROG_LUA}\"'
        BT_FLAGS_CPP+='-DDIP=\"${BT_DIR_DIP}\"'

        BT_SOURCES=main.c
        BT_SOURCES+=Exec.c
        BT_SOURCES+=Lang.c
        BT_SOURCES+=Grouping.c
        BT_SOURCES+=Queue.c
        BT_SOURCES+=XmlProg.c
        BT_SOURCES+=SubLang.c
    }

    target_BasicExec() {
        BT_DIR_LIB=${BT_DIR_LIBEXEC}
        BT_SUBDIR=${BT_TOPDIR}/src/Modules
        BT_TYPE=shlib
        BT_TARGET_FILE=BasicExec.so
        BT_SOURCES=BasicExec.c
    }

    target_Hello() {
        BT_DIR_LIB=${BT_DIR_LIBEXEC}
        BT_SUBDIR=${BT_TOPDIR}/src/Modules
        BT_TYPE=shlib
        BT_TARGET_FILE=Hello.so
        BT_SOURCES=Hello.c
    }

    target_noupdir() {
        BT_DIR_LIB=${BT_DIR_LIBEXEC}
        BT_SUBDIR=${BT_TOPDIR}/src/Modules
        BT_TYPE=shlib
        BT_TARGET_FILE=noupdir.so
        BT_SOURCES=noupdir.c
    }

    target_unicode() {
        BT_DIR_LIB=${BT_DIR_LIBEXEC}
        BT_SUBDIR=${BT_TOPDIR}/src/Modules
        BT_TYPE=shlib
        BT_TARGET_FILE=unicode.so
        BT_SOURCES=unicode.c
    }

    target_parselogs() {
        BT_SUBDIR=${BT_TOPDIR}/src/Utils
        BT_TYPE=program
        BT_SOURCES=parselogs.c
    }

    target_testmodule() {
        BT_SUBDIR=${BT_TOPDIR}/src/Utils
        BT_TYPE=program
        BT_SOURCES=testmodule.c
    }

    target_screwsctl() {
        BT_SUBDIR=${BT_TOPDIR}/src/Utils
        BT_TYPE=null
    }

    target_screwsctl_install() {
        bt_install_dir ${BT_DIR_SBIN}
        bt_install_program screwsctl ${BT_DIR_SBIN}
    }

    target_doc() {
        BT_TYPE=null
    }

    target_doc_install() {
        bt_install_dir ${BT_DIR_DOC}
        bt_install_dir ${BT_DIR_EXAMPLES}
        bt_install_dir ${BT_DIR_MAN}/man8
        ( cd doc/server && pax -rw -pp -pm . ${BT_DIR_DOC} )
        bt_install_data doc/images/screws.png ${BT_DIR_DOC}
        bt_install_data doc/manpage/screwsd.8 ${BT_DIR_MAN}/man8/screwsd.8
        bt_install_data conf/screws.conf ${BT_DIR_EXAMPLES}
    }
}
