#!/bin/bash

grabber_primary_options () {
	theme_name=cd
	Link_or_Application=Application
	SOURCE_type=folders
}

grabber_secondary_options () {
	Launcher="avatar-factory -al $grabber_name --URL \"$SOURCE_item\" --avatar \"$folder2save/$avatar_filename.desktop\" --name \"$avatar_title\" --title \"$avatar_title\" --icon \"$icons_PATH/$icon_NAME.png\" --DND"
}

get_grabber_picture () {
	google () {(
		web_source=$(wget -U firefox -qO - "http://images.google.com.ar/images?um=1&client=firefox-a&q=$query1+$query2+cover")
		echo "$web_source" | sed 's/imgurl=/\n/g' | grep '^http' | head -n 1 | sed 's/&img.*//'
	)}

	local () {(
		find "$SOURCE_item" -maxdepth 1 -type f \( -iname 'cover.jpg' -or -iname 'cover.png' -or -iname 'folder.jpg' -or -iname 'folder.png' -or  -iname '*front*.jpg' -or -iname '*front*.png' \) -print -quit
		if [[ -z "$grabber_picture" ]]; then
			find "$SOURCE_item" -maxdepth 1 -type f \( -iname '*.jpg' -or -iname '*.png' \) -print -quit
		fi
	)}

	if [[ -z $grabber_sites ]]; then
		grabber_sites=local
	fi

	query1=${SOURCE_item##*/}
	query2=${SOURCE_item%/$query1}	query2=${query2##*/}

	if [[ "" != "$(find "$SOURCE_item" -maxdepth 1 -type f \( -iname '*.mp3' -or -iname '*.aac' -or -iname '*.m4a' -or -iname '*.ogg' -or -iname '*.flac' -or -iname '*.wma' \) -print -quit)" ]]; then
		for i in $grabber_sites ; do
			grabber_picture=$($i | head -n 1)
	
			if [[ -n $grabber_picture ]]; then
				break
			fi		
		done
	else
		grabber_picture=
	fi
}


