#!/bin/sh
# $Id$
# elvis: freedb		-- Search for cd track listings in FreeDB (www.freedb.org)
# ianb@erislabs.net 20030129
# notes at end of script

. surfraw || exit 1

w3_config_hook () {
    # options
    defyn SURFRAW_freedb_artists 0
    defyn SURFRAW_freedb_albums 0
    defyn SURFRAW_freedb_songs 0
    defyn SURFRAW_freedb_rest 0
    defyn SURFRAW_freedb_all 0
    defyn SURFRAW_freedb_id 0

    defyn SURFRAW_freedb_allfields  0
    defyn SURFRAW_freedb_allcats    1
    defyn SURFRAW_freedb_bycat      0

    def SURFRAW_freedb_cats         ""

    def SURFRAW_freedb_page         1
    # internal
    SURFRAW_freedb_default=1
}

w3_usage_hook () {
    cat <<EOF
Usage: $w3_argv0 [options] [search words]...
Description:
  Surfraw search FreeDB (www.freedb.org) for cd track listings
Local options:
  -artists                      Search artists
                                Environment: SURFRAW_cddb_artists
  -albums                       Search albums
                                Environment: SURFRAW_cddb_albums
  -songs                        Search songs
                                Environment: SURFRAW_cddb_songs
  -rest                         Search the rest of the data
                                Environment: SURFRAW_cddb_rest
  -all                          Search all fields
                                Environment: SURFRAW_cddb_all
                                Default: search artists and albums
  -id                           Search by CDDB ID.
  -bycat                        Sort results by category
  -cat=CATEGORY                 Category to search, repeat as needed
                                Options:
                                    all
                                    blues
                                    classical
                                    country
                                    data
                                    folk
                                    jazz
                                    misc
                                    newage
                                    reggae
                                    rock
                                    soundtrack
                                Default: all
  -page=PAGENUM                 Start at page PAGENUM
                                Default: 1
EOF
    w3_global_usage
}

w3_parse_option_hook () {
    opt="$1"
    optarg="$2"
    case "$opt" in
    -ar*)  setopt SURFRAW_freedb_artists  1;setoptyn SURFRAW_freedb_default 0;;
    -alb*) setoptyn SURFRAW_freedb_albums 1;setoptyn SURFRAW_freedb_default 0;;
    -so*)  setoptyn SURFRAW_freedb_songs  1;setoptyn SURFRAW_freedb_default 0;;
    -r*)   setoptyn SURFRAW_freedb_rest   1;setoptyn SURFRAW_freedb_default 0;;
    -all)  setoptyn SURFRAW_freedb_all    1;setoptyn SURFRAW_freedb_default 0;;
    -id)   setoptyn SURFRAW_freedb_id     1;;

    -pag*=*) setopt SURFRAW_freedb_page    $optarg ;;
    -bycat) setoptyn SURFRAW_freedb_bycat  1 ;;

    -cat*=*) if [ "$optarg" = "all" ]; then
            setoptyn SURFRAW_freedb_allcats 1
            SURFRAW_freedb_cats=""
        else 
            setoptyn SURFRAW_freedb_allcats 0
            SURFRAW_freedb_cats="${SURFRAW_freedb_cats}&cats%5B%5D=$optarg"
        fi
        ;;

    *) return 1 ;;
    esac
    return 0
}

w3_config
# disable requoting, doesn't work with this elvi.
SURFRAW_quote_ifs=0
w3_parse_args "$@"
# w3_args now contains a list of arguments
if test -z "$w3_args"; then
    w3_browse_url http://www.freedb.org/
else
    escaped_args=`w3_url_of_arg $w3_args`
    SURFRAW_freedb_queryopts="page=${SURFRAW_freedb_page}&"

    if ifyes SURFRAW_freedb_id; then
	w3_browse_url "http://www.freedb.org/freedb_discid_check.php?discid=${escaped_args}&${SURFRAW_freedb_queryopts}"
    else
	if [ $SURFRAW_freedb_albums  -eq 1 -o \
             $SURFRAW_freedb_all     -eq 1 -o \
             $SURFRAW_freedb_default -eq 1 ]
	then
            SURFRAW_freedb_queryopts="${SURFRAW_freedb_queryopts}fields%5B%5D=title&"
	fi

	if [ $SURFRAW_freedb_artists -eq 1 -o \
             $SURFRAW_freedb_all     -eq 1 -o \
             $SURFRAW_freedb_default -eq 1 ]
	then
            SURFRAW_freedb_queryopts="${SURFRAW_freedb_queryopts}fields%5B%5D=artist&"
	fi

	if [ $SURFRAW_freedb_songs -eq 1 -o \
             $SURFRAW_freedb_all   -eq 1 ]
	then
            SURFRAW_freedb_queryopts="${SURFRAW_freedb_queryopts}fields%5B%5D=track&"
	fi

	if [ $SURFRAW_freedb_rest -eq 1 -o \
             $SURFRAW_freedb_all  -eq 1 ]
	then
            SURFRAW_freedb_queryopts="${SURFRAW_freedb_queryopts}fields%5B%5D=rest&"
	fi

	if [ $SURFRAW_freedb_bycat = 1 ]; then
            SURFRAW_freedb_queryopts="${SURFRAW_freedb_queryopts}grouping=category&"
	else
            SURFRAW_freedb_queryopts="${SURFRAW_freedb_queryopts}grouping=none&"
	fi

	if [ $SURFRAW_freedb_allcats = 1 ]; then
            SURFRAW_freedb_queryopts="${SURFRAW_freedb_queryopts}allcats=YES&"
	else
            SURFRAW_freedb_queryopts="${SURFRAW_freedb_queryopts}allcats=NO${SURFRAW_freedb_cats}"
	fi

	if [ $SURFRAW_freedb_all = 1 ]; then
            SURFRAW_freedb_queryopts="${SURFRAW_freedb_queryopts}allfields=YES&"
	else
            SURFRAW_freedb_queryopts="${SURFRAW_freedb_queryopts}allfields=NO&"
	fi

	w3_browse_url "http://www.freedb.org/freedb_search.php?${SURFRAW_freedb_queryopts}words=${escaped_args}"
    fi
fi

#eg: http://www.freedb.org/freedb_search.php?words=swans&allfields=NO&fields=artist&fields=title&fields=track&fields=rest&allcats=NO&cats=blues&cats=classical&cats=country&cats=data&cats=folk&cats=jazz&cats=misc&cats=newage&cats=reggae&cats=rock&cats=soundtrack&grouping=cats
# minimum: http://www.freedb.org/freedb_search.php?words=swans+are+dead&allfields=YES&allcats=YES&grouping=none
# parameters:
#words=search+words (compulsory)
#allfields=YES | NO (compulsory)
#fields=artist | title | track | rest (can be repeated as often as needed)
#allcats=YES | NO (compulsory)
#cats = blues | classical | country | data | folk | jazz | misc | newage |
#       reggae | rock | soundtrack (can be repeated as often as needed)
#grouping=cats | none (compulsory)
