#!/bin/sh
# Surfaw -- Shell Users' Revolutionary Front Against the World wide web
#
# (c) 2000 Julian Assange, all rights reserved.
# (c) 2000 Melbourne Institute for Advanced Study, all rights reserved.
#
# see the file COPYING for further copyright details
#
# @configure_input@

surfaw_version=@VERSION@
surfaw_api_version=1.0
w3_argv0="$0"

# defaults

w3_config_hook () {
    return 1
}

w3_config () {
prefix=@prefix@
def   SURFAW_global_conf	@sysconfdir@/surfaw.conf
def   SURFAW_conf		$HOME/.surfaw.conf

    test -e "$SURFAW_global_conf" || err "couldn't source SURFAW_global_conf ($SURFAW_global_conf)"
    . "$SURFAW_global_conf"
    w3_config_hook
    test -e "$SURFAW_conf" && . "$SURFAW_conf"
}

warn () {
    echo "$w3_argv0 WARNING: ""$@"
}

err () {
    echo "$w3_argv0 ERROR: ""$@"
    exit 1
}

yesno () {
    local value
    eval value=\$${1}
    case "$value" in
	# "yes", "true", "on", or "1"
	[Yy][Ee][Ss]|[Tt][Rr][Uu][Ee]|[Oo][Nn]|1)
	    return 0
        ;;
        # "no", "false", "off", or "0"
        [Nn][Oo]|[Ff][Aa][Ll][Ss][Ee]|[Oo][Ff][Ff]|0)
	    return 1
        ;;

        *)
        err "\"$1\" is not set correctly. Should be (yes|on|1|no|off|0). Is \"$value\""
        ;;
    esac
}

ifyes () {
    yesno "$@" && return 0
    return 1
}

ifno () {
    yesno "$@" && return 1
    return 0
}

ok () {
    yesno "$@"
}

def () {
    local value
    eval value=\$${1}
    test -z "$value" || return 0
    eval ${1}="$2"
    return 0
}

defyn () {
    local value
    eval value=\$${1}
    test -z "$value" && eval ${1}="$2"
    yesno $1
}

setopt  () {
    local value
    eval value=\$${1}
    eval ${1}="$2"
}

setoptyn () {
    local value
    eval value=\$${1}
    eval ${1}="$2"
    yesno "$2"
}

w3_find_prog () {
  local prog old_ifs path_ifs dir
  prog="$1"
  old_ifs="${IFS:-\" \"}"
  IFS=":"
  path_ifs="$PATH"
  for dir in $path_ifs; do
    test -z "$dir" && dir=.
    if test -e "$dir/$prog"; then
	echo "$dir/$prog"
	IFS="$old_ifs"
	return 0
    fi
  done
  IFS="$old_ifs"
  return 1
}

w3_url_escape () {
    echo "$@" | sed 's/%/%25/g;
		     s/+/%2B/g;
		     s/ /+/g;
		     s/"/%22/g;
		     s/#/%23/g;
		     s/\$/%24/g;
		     s/%/%25/g;
		     s/&/%26/g;
		     s/,/%2C/g;
		     s/\./%2E/g;
		     sx/x%2Fx;
		     s/:/%3A/g;
		     s/;/%3B/g;
		     s/</%3C/g;
		     s/=/%3D/g;
		     s/>/%3E/g;
		     s/?/%3F/g;
		     s/@/%40/g;
		     s/\[/%5B/g;
		     s/\\/%74/g;
		     s/\]/%5D/g;
		     s/\^/%5E/g;
		     s/{/%7B/g;
		     s/|/%7C/g;
		     s/}/%7D/g;
		     s/~/%7E/g;
		   '"s/'/%60/g"
}

w3_url_of_arg () {
    if ok SURFAW_escape_url_args; then
	w3_url_escape "$@"
    else
	echo "$@"
    fi
}

true () {
    return 0
}

false () {
    return 1
}

w3_global_usage () {
# style: keep sorted
#-------------------------------------79 cols----------------------------------
    cat <<EOF
Global options:
  -browser=EXECUTABLE		Set browser
                                Default: $SURFAW_browser
				Environment: SURFAW_browser
  -elvi				Display a list of other Surfaw mechanims for
				conquering evil
  -escape-url-args=yes|no       Apply url escaping to arguments
				Default: $SURFAW_escape_url_args
				Environment: SURFAW_escape_url_args
  -p0rn=yes|n0			Yes, yes, harder, deeper, faster, oh baybe
				Default: $SURFAW_p0rn
				Environment: SURFAW_p0rn
  -help				What you're reading now, dude
  -quiet=yes|no			I can't enjoy it with you talking all the time
				Default: $SURFAW_quiet
				Environment: SURFAW_quiet
  -version			Display Surfaw version ($surfaw_version)
Copyright:
  Copyright (c) 2000 Julian Assange <proff@iq.org>
  Copyright (c) 2000 Melbourne Institute for Advanced Study
EOF
}

w3_usage_hook () {
    echo "Usage: $w3_argv0 [options] [args...]"
    w3_global_usage
}

w3_bad_option () {
    err "bad option \"${opt}\". Try -help for usage information"
}

w3_parse_option_hook () {
    return 1
}

w3_parse_option () {
    local opt optarg
    opt="$1"
    case "$opt" in
	    -*=*) optarg=`echo "$opt" | sed 's/[-_a-zA-Z0-9]*=//'`
    esac
    w3_parse_option_hook "$opt" "$optarg" && return 0
    
    case "$opt" in
	-browser=*)	    setopt   SURFAW_browser	    $optarg ;;
	-elvi)		    cat $SURFAW_elvi_list; exit 0           ;;
	-escape-url-args=*) setoptyn SURFAW_escape_url_args $optarg ;;
	-porn=*)	    setoptyn SURFAW_p0rn	    $optarg ;;
	-help | --help)
	       w3_usage_hook
	       exit 1						    ;;
	-quiet=*)	    setoptyn SURFAW_quiet	    $optarg ;;
	-version)	    echo $surfaw_version; exit 0	    ;;
	-*)		    w3_bad_option "$opt"		    ;;
	*)		    return 1				    ;;
    esac
    return 0
}

w3_parse_args () {
    local arg
    w3_args=
    for arg in "$@"; do
	w3_parse_option "$arg" || w3_args="$w3_args $arg"
    done
}

w3_browse_url () {
    if test "$SURFAW_browser_args" = "none"; then
	$SURFAW_browser "$@"
    else
	$SURFAW_browser $SURFAW_browser_args "$@"
    fi
    ifno SURFAW_quiet && echo "Thank you for supporing the Shell Users Revolutionary Front Against the WWW"
}
