#!/bin/bash
VERSION=0.7

URL="http://totoz.eu"

function xml2list() {
	xsltproc /dev/fd/3 - 3<<XSLT
<?xml version="1.0" encoding="UTF-8" ?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0">
	<xsl:output method="text" indent="yes" />

	<xsl:template match="/totozes">
		<xsl:apply-templates select="totoz" />
		<xsl:apply-templates select="identical" />
		<xsl:apply-templates select="similar" />
	</xsl:template>

	<xsl:template match="identical">
		<xsl:apply-templates select="totoz" />
	</xsl:template>

	<xsl:template match="similar">
		<xsl:apply-templates select="totoz" />
	</xsl:template>

	<xsl:template match="totoz">
		<xsl:choose>
			<xsl:when test="parent::identical">
				<xsl:text>= </xsl:text>
			</xsl:when>
			<xsl:when test="parent::similar">
				<xsl:text>~ </xsl:text>
			</xsl:when>
			<xsl:otherwise>
				<xsl:text>  </xsl:text>
			</xsl:otherwise>
		</xsl:choose>
		<xsl:value-of select="name" /><xsl:text>
</xsl:text>
	</xsl:template>
</xsl:stylesheet>
XSLT
}

function xml2totozinfo() {
	xsltproc /dev/fd/3 - 3<<XSLT
<?xml version="1.0" encoding="UTF-8" ?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0">
	<xsl:output method="text" indent="yes" />

	<xsl:template match="/">
		<xsl:apply-templates select="totoz" />
	</xsl:template>

	<xsl:template match="totoz">
		<xsl:text>[:</xsl:text><xsl:value-of select="name" /><xsl:text>]
</xsl:text>
		<xsl:text>id:       </xsl:text><xsl:value-of select="@id" /><xsl:text>
</xsl:text>
		<xsl:text>author:   </xsl:text><xsl:value-of select="username" /><xsl:text>
</xsl:text>
		<xsl:text>created:  </xsl:text><xsl:value-of select="created" /><xsl:text>
</xsl:text>
		<xsl:text>url:      </xsl:text><xsl:value-of select="url" /><xsl:text>
</xsl:text>
		<xsl:text>page:     </xsl:text><xsl:value-of select="descpage" /><xsl:text>
</xsl:text>

		<xsl:text>size:     </xsl:text>
			<xsl:value-of select="width" /><xsl:text>×</xsl:text><xsl:value-of select="height" />
			<xsl:text>
</xsl:text>

		<xsl:text>animated: </xsl:text>
			<xsl:if test="animated=1">
				<xsl:text>yes (</xsl:text><xsl:value-of select="frames" /><xsl:text> frames)</xsl:text>
			</xsl:if>
			<xsl:if test="animated=0">
				<xsl:text>no</xsl:text>
			</xsl:if>
		<xsl:text>
</xsl:text>
	</xsl:template>
</xsl:stylesheet>
XSLT
}

function xml2totozviewinfo() {
	xsltproc /dev/fd/3 - 3<<XSLT
<?xml version="1.0" encoding="UTF-8" ?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0">
	<xsl:output method="text" indent="yes" />

	<xsl:template match="/">
		<xsl:apply-templates select="totoz" />
	</xsl:template>

	<xsl:template match="totoz">
		<xsl:text>author:   </xsl:text><xsl:value-of select="username" /><xsl:text>
</xsl:text>
		<xsl:text>created:  </xsl:text><xsl:value-of select="created" /><xsl:text>
</xsl:text>

		<xsl:text>weight:   </xsl:text>
			<xsl:choose>
				<xsl:when test="weight > 2048">
					<xsl:value-of select="round(weight div 1024)" /><xsl:text> kb</xsl:text>
				</xsl:when>
				<xsl:otherwise>
					<xsl:value-of select="weight" /><xsl:text> b</xsl:text>
				</xsl:otherwise>
			</xsl:choose>
			<xsl:text>
</xsl:text>

		<xsl:text>size:     </xsl:text>
			<xsl:value-of select="width" /><xsl:text>×</xsl:text><xsl:value-of select="height" />
			<xsl:text>
</xsl:text>

		<xsl:text>animated: </xsl:text>
			<xsl:if test="animated=1">
				<xsl:text>yes (</xsl:text><xsl:value-of select="frames" /><xsl:text> frames)</xsl:text>
			</xsl:if>
			<xsl:if test="animated=0">
				<xsl:text>no</xsl:text>
			</xsl:if>
		<xsl:text>
</xsl:text>
	</xsl:template>
</xsl:stylesheet>
XSLT
}

function xml2totoztags() {
	xsltproc /dev/fd/3 - 3<<XSLT
<?xml version="1.0" encoding="UTF-8" ?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0">
	<xsl:output method="text" indent="yes" />

	<xsl:template match="/">
		<xsl:apply-templates select="totoz" />
	</xsl:template>

	<xsl:template match="totoz">
		<xsl:apply-templates select="tags" />
	</xsl:template>

	<xsl:template match="tags">
		<xsl:apply-templates select="tag" />
	</xsl:template>

	<xsl:template match="tag">
		<xsl:value-of select="." /><xsl:text>
</xsl:text>
	</xsl:template>
</xsl:stylesheet>
XSLT
}

function get() {
	URL=$(echo -n "$@" | sed 's/ /%20/g')
	curl --fail --silent -A "totozfinder/${VERSION}" "$URL"
}

function search() {
	get "${URL}/search/totoz.xml/${@}" | xml2list
}

function similar() {
	totozexists "$@" || error "No such totoz"

	get "${URL}/totoz/${@}/similar.xml" | xml2list | \
		while read relationship totoz ; do echo "$relationship $totoz" ; [ $PREVIEW -eq 1 ] && [ "$relationship" != "=" ] && $0 view "$totoz" ; done
}

function bookmarks() {
	userexists "$@" || error "No such user"

	get "${URL}/users/${@}/bookmarks.xml" | xml2list
}

function view() {
	totozexists "$@" || error "No such totoz"

	test -x "$(which notify-send)" || error "You need libnotify to display totozes"

	info=$(get "${URL}/totoz/${@}/info.xml" | xml2totozviewinfo)
	get "${URL}/${@}.gif" > "/tmp/totoz.$$"
#	img2txt /tmp/totoz.$$
	notify-send "[:${@}]" "<span font_desc='monospace'>$info</span>" -i "/tmp/totoz.$$"
	rm "/tmp/totoz.$$"
}

function info() {
	totozexists "$@" || error "No such totoz"

	get "${URL}/totoz/${@}/info.xml" | xml2totozinfo
	echo
	echo "tags: "
	get "${URL}/totoz/${@}/info.xml" | xml2totoztags | column
	echo
	echo "similar: "
	similar "$@" | column
}

function totozexists() {
	test "$(get "${URL}/totoz/${@}/info.xml")" != ""
}

function userexists() {
	test "$(get "${URL}/users/${@}/bookmarks.xml")" != ""
}

function error() {
	echo "$@"
	exit 1
}

PREVIEW=0
while getopts "v" flag
do
	case $flag in 
		v) PREVIEW=1 ;;
	esac
done
shift $(($OPTIND - 1))

case $1 in
	search)
		search "$2"
		;;
	similar|like)
		similar "$2"
		;;
	bookmarks|bm*)
		bookmarks "$2"
		;;
	view)
		view "$2"
		;;
	info)
		info "$2"
		;;
	*)
		search "$@"
		;;
esac
