#!/bin/sh
# $Id$
# elvis: translate	-- Translate human languages (various providers)
. surfraw || exit 1

w3_config_hook () {
def   SURFRAW_translate_provider	babelfish
def   SURFRAW_translate_from		"$SURFRAW_lang"
def   SURFRAW_translate_to		"$SURFRAW_lang"
}

w3_usage_hook () {
    cat <<EOF
Usage: $w3_argv0 [options] [words-to-translate]...
Description:
  Surfraw translate human language
Local options:
  -from=LANGUAGE		Source language
				Environment: SURFRAW_translate_from, SURFRAW_lang
  -to=LANGUAGE			Destination language
				Environment: SURFRAW_translate_to, SURFRAW_lang
  -provider=	                Translation service provider
            babelfish		babelfish.yahoo.com (systrans)
	    raging		babelfish.yahoo.com/raging (systrans)
                                Default: $SURFRAW_translate_provider
				Environment: SURFRAW_translate_provider
Languages:
  de				German
  en				English
  es				Spanish
  fr				French
  it				Italian
  nl                            Dutch
  el                            Greek
  ja				Japanese
  ko				Korean
  pt				Portuguese
  ru				Russian
  zh				Simplified Chinese
  zt                            Traditional Chinese

Examples:
  $w3_argv0 			Provider's standard translation page
  $w3_argv0 -provider=raging	Use raging search as a translation provider
  $w3_argv0 -from=fr -to=en la verite vous liberera
Bugs:
  Systrans derived providers only support the following translation
  combinations:
	English to Chinese
	English to French
	English to German
	English to Italian
	English to Japanese
	English to Korean
	English to Portuguese
	English to Spanish
	Chinese to English
	French to English
	French to German
	German to English
	German to French
	Italian to English
	Japanese to English
	Korean to English
	Portuguese to English
	Russian to English
	Spanish to English
EOF
    w3_global_usage
}

w3_parse_option_hook () {
    opt="$1"
    optarg="$2"
    case "$opt" in
	-from=*)	setopt SURFRAW_translate_from		$optarg ;;
	-to=*)		setopt SURFRAW_translate_to		$optarg ;;
	-provider=*)	setopt SURFRAW_translate_provider	$optarg	;;
	*) return 1 ;;
    esac
    return 0
}

w3_config
w3_parse_args "$@"
# w3_args now contains a list of arguments
if test -z "$w3_args"
then
	w3_browse_url "http://babelfish.yahoo.com/"
else
        if [ -z "$SURFRAW_translate_to" ];
	then
	    SURFRAW_translate_to="en"
	fi
        if [ -z "$SURFRAW_translate_from" ];
	then
	    SURFRAW_translate_from="fr"
	fi
	escaped_args=`w3_url_of_arg $w3_args`
	case "$escaped_args" in
		http*) w3_browse_url "http://babelfish.yahoo.com/translate_url?tt=url&url=${escaped_args}&lp=${SURFRAW_translate_from}_${SURFRAW_translate_to}"
		;;
		*) w3_browse_url "http://babelfish.yahoo.com/translate_txt?doit=done&tt=urltext&intl=1&urltext=${escaped_args}&lp=${SURFRAW_translate_from}_${SURFRAW_translate_to}"
        ;;
    esac
fi

exit $?
