#!/bin/bash

BASE="https://wiki.linux-forks.de"

mode="find_description"

thumbnail=""
categories=""

data=`curl -s "$1"`

temp=`echo "$data" | sed -n 's/<p>\(.\+\).*/\1/p' | grep -v -e '^<br />$' | sed 's#^</p>##' | head -n1`
if [ "$temp" != "" ]; then
	temp=`echo "$temp" | sed "s#href=\"#href=\"$BASE#g" | sed 's/"/\\\\"/g' | sed 's/\t//g'`
	description="$temp"
	mode="find_infobox";
fi

IFS=$'>';
for line in $data; do
	if [ "$mode" == "find_infobox" ]; then
		if [ "`echo \"$line\" | grep 'infobox'`" != "" ]; then
			mode="image";
		fi
	elif [ "$mode" == "image" ]; then
		temp=`echo "$line" | sed -n 's/.*img.*src="\([^"]\+\).*/\1/p'`;
		if [ "$temp" != "" ]; then
			thumbnail="$BASE$temp"
			mode="find_cat"
		fi
	elif [ "$mode" == "find_cat" ]; then
		if [ "`echo \"$line\" | grep 'mw-normal-catlinks'`" != "" ]; then
			mode="cat";
		fi
	elif [ "$mode" == "cat" ]; then
		temp=`echo "$line" | sed -n 's/.*title="Category:\([^"]\+\).*/\1/pg' | grep -v 'page does not exist'`
		if [ "$temp" != "" ]; then
			if [ "$categories" != "" ]; then
				categories="$categories,"
			fi
			categories="$categories\"$temp\""
		fi
	fi
done
IFS=" ";

echo "\"categories\": [$categories],"
echo "\"image\": \"$thumbnail\","
echo "\"description\": \"$description\""