#!/bin/sh
##
##  mldonkey.sh -- MLdonkey server control script
##

#   read global configuration
. @l_prefix@/etc/mldonkey/mldonkeyrc

#   read local configuration
if [ -f $HOME/.mldonkeyrc ]; then
    . $HOME/.mldonkeyrc
fi

#   command line check
if [ $# -lt 1 ]; then
    echo "** ERROR: Usage: mldonkey {start|cui|gui|stop}" 1>&2
    exit 1
fi

#   send MLdonkey server a query
server_query () {
    query="$1"
    cmd="@l_prefix@/lib/openpkg/curl"
    if [ ".$MLD_ADMIN_USER" != . ]; then
        cmd="$cmd --basic --user \"${MLD_ADMIN_USER}:${MLD_ADMIN_PASS}\""
    fi
    cmd="$cmd \"http://${MLD_HTTP_ADDR}:${MLD_HTTP_PORT}/submit?q=$query\""
    eval "$cmd </dev/null >/dev/null 2>&1"
}

#   dispatch into command
cmd="$1"
shift
case "$cmd" in
    start )
        #   make sure the MLdonkey user run-time environment exists   
        if [ ! -d ${MLD_HOMEDIR} ] ; then
            echo "++ creating MLdonkey home directory \"${MLD_HOMEDIR}\""
            if ! mkdir ${MLD_HOMEDIR}; then
                echo "** ERROR: creation of \"${MLD_HOMEDIR}\" failed!" 1>&2
                exit 1
            fi
        fi

        #   run MLdonkey server
        echo "++ starting MLdonkey server"
        cmd="@l_prefix@/bin/mlnet"
        if [ ".${MLD_RUNASUSER}" != . ]; then
            cmd="$cmd -run_as_user \"${MLD_RUNASUSER}\""
        fi
        cmd="$cmd -log_file \"${MLD_HOMEDIR}/mldonkey.log\""
        cmd="$cmd -client_name \"${MLD_CLIENT_NAME}\""
        cmd="$cmd -client_bind_addr \"${MLD_CLIENT_ADDR}\""
        cmd="$cmd -telnet_port \"${MLD_TELNET_PORT}\""
        cmd="$cmd -telnet_bind_addr \"${MLD_TELNET_ADDR}\""
        cmd="$cmd -gui_port \"${MLD_GUI_PORT}\""
        cmd="$cmd -gui_bind_addr \"${MLD_GUI_ADDR}\""
        cmd="$cmd -chat_port \"${MLD_CHAT_PORT}\""
        cmd="$cmd -chat_bind_addr \"${MLD_CHAT_ADDR}\""
        cmd="$cmd -http_port \"${MLD_HTTP_PORT}\""
        cmd="$cmd -http_bind_addr \"${MLD_HTTP_ADDR}\""
        cmd="$cmd -gift_port \"${MLD_GIFT_PORT}\""
        cmd="$cmd -allowed_ips \"${MLD_ALLOWIPS}\""
        cmd="$cmd -nolimit_ips \"${MLD_NOLIMITIPS}\""
        cmd="$cmd -enable_donkey \"${MLD_ENABLE_DONKEY}\""
        cmd="$cmd -enable_overnet \"${MLD_ENABLE_OVERNET}\""
        cmd="$cmd -enable_bittorrent \"${MLD_ENABLE_BITTORRENT}\""
        cmd="$cmd -enable_opennap \"${MLD_ENABLE_OPENNAP}\""
        cmd="$cmd -enable_soulseek \"${MLD_ENABLE_SOULSEEK}\""
        cmd="$cmd -enable_gnutella \"${MLD_ENABLE_GNUTELLA}\""
        cmd="$cmd -enable_gnutella2 \"${MLD_ENABLE_GNUTELLA2}\""
        cmd="$cmd -enable_fasttrack \"${MLD_ENABLE_FASTTRACK}\""
        cmd="$cmd -enable_directconnect \"${MLD_ENABLE_DIRECTCONNECT}\""
        cmd="$cmd -enable_openft \"${MLD_ENABLE_OPENFT}\""
        cmd="$cmd -enable_fileTP \"${MLD_ENABLE_FILETP}\""
        cmd="$cmd -mldonkey_bin \"@l_prefix@/bin\""
        cmd="$cmd -mldonkey_gui \"@l_prefix@/bin/mlgui\""
        cmd="$cmd -max_hard_download_rate \"${MLD_RATE_LOW_DOWNLOAD}\""
        cmd="$cmd -max_hard_upload_rate \"${MLD_RATE_LOW_UPLOAD}\""
        cmd="$cmd -temp_directory \"${MLD_PATH_TEMP}\""
        if [ ".${MLD_FLAGS}" != . ]; then
            cmd="$cmd ${MLD_FLAGS}"
        fi
        ( cd ${MLD_HOMEDIR} || exit 1
          eval exec $cmd </dev/null >>${MLD_HOMEDIR}/mldonkey.log 2>&1 &
        ) >/dev/null 2>&1
        ;;
    cui )
        #   connect to console user interface
        echo "++ entering MLdonkey console user interface"
        exec telnet ${MLD_TELNET_ADDR} ${MLD_TELNET_PORT}
        ;;
    gui )
        #   run MLdonkey GUI
        if [ ! -f @l_prefix@/bin/mlgui ]; then
            echo "** ERROR: MLdonkey GUI not available" 1>&2
            exit 1
        fi
        echo "++ entering MLdonkey graphical user interface"
        exec @l_prefix@/bin/mlgui ${1+"$@"}
        ;;
    slow )
        #   run MLdonkey in slow mode
        echo "++ running MLdonkey server in slow mode"
        server_query "set+max_hard_download_rate+${MLD_RATE_LOW_DOWNLOAD}"
        server_query "set+max_hard_upload_rate+${MLD_RATE_LOW_UPLOAD}"
        ;;
    fast )
        #   run MLdonkey in fast mode
        echo "++ running MLdonkey server in fast mode"
        server_query "set+max_hard_download_rate+${MLD_RATE_HIGH_DOWNLOAD}"
        server_query "set+max_hard_upload_rate+${MLD_RATE_HIGH_UPLOAD}"
        ;;
    stop )
        #   send MLdonkey server the kill command
        echo "++ stopping MLdonkey server"
        server_query "close_fds"
        server_query "save"
        server_query "kill"
        ;;
    * )
        echo "** ERROR: invalid command \"$cmd\"" 1>&2
        exit 1
        ;;
esac

