#!/bin/sh

# ToDo:
# ist start und enddatum gleich
# kein_end_datum -> bool (true/false)

URL="http://www.klassik-stiftung.de/index.php?type=1342446768"
FILE="/tmp/myfeed.json"
FILE_OUT="$1"

[ -n "$FILE_OUT" ] || {
	echo "Usage: $0 <outfile>	# will rewrite the feed"
	exit 1
}

TAB="	"
TABIDX=0

wget -qO "$FILE" "$URL" || {
	logger -s "fetching feed failed"
	rm "$FILE"
	exit 1
}

sed -i	-e 's/{/{\n/g'		\
	-e 's/",/",\n/g'	\
	-e 's/},/\n},\n/g'	\
	-e 's/:{/: {/g'		\
	-e 's/":"/": "/g'	\
	-e 's/:null/: null/g'	\
	-e 's/\&/\&amp;/g'	\
	-e 's/u00fc/\&uuml;/g'	\
	-e 's/u00dc/\&Uuml;/g'	\
	-e 's/u00f6/\&ouml;/g'	\
	-e 's/u00d6/\&Ouml;/g'	\
	-e 's/u00e4/\&auml;/g'	\
	-e 's/u00df/\&szlig;/g'	\
	-e 's/u00bb/\&raquo;/g'	\
	-e 's/u00ab/\&laquo;/g'	\
	-e 's/u00e9/\&eacute;/g'\
	-e 's/u2013/-/g'\
	-e 's/null}/null\n}/g'	\
	"$FILE"

# fixme! till we found the webkit-bug, always output "-" instead of '&*dash;'
# -e 's/u2013/\&emdash;/g'

tabout()
{
	local i=$TABIDX
	while [ $i -gt 0 ]; do {
		echo -n "$TAB"
		i=$(( $i - 1 ))
	} done
}

nicedate()
{
	local date="$1"		# 2012-08-09	-> 9. August 2012
	local year month day

	year="$(  echo "$date" | cut -d'-' -f1 )"
	month="$( echo "$date" | cut -d'-' -f2 )"
	day="$(   echo "$date" | cut -d'-' -f3 )"

	case "$month" in
		01|1) month="Januar" ;;
		02|2) month="Februar" ;;
		03|3) month="M&auml;rz" ;;
		04|4) month="April" ;;
		05|5) month="Mai" ;;
		06|6) month="Juni" ;;
		07|7) month="Juli" ;;
		08|8) month="August" ;;
		09|9) month="September" ;;
		  10) month="Oktober" ;;
		  11) month="November" ;;
		   *) month="Dezember" ;;
	esac

	echo "${day}. $month $year"
}

shuffle_picture()
{
	local url="http://www.klassik-stiftung.de/fileadmin/user_upload/GNM-Banner"

	case "$PICTURE_NAME" in
		Lebensfluten)
			PICTURE_NAME="Archivschatz"
			PICTURE_URL="$url/2.jpg"
		;;
		Archivschatz)
			PICTURE_NAME="Cranach"
			PICTURE_URL="$url/3.jpg"
		;;
		Cranach)
			PICTURE_NAME="Galilei"
			PICTURE_URL="$url/4.jpg"
		;;
		Galilei)
			PICTURE_NAME="Pudel"
			PICTURE_URL="$url/5.jpg"
		;;
		Pudel|*)
			PICTURE_NAME="Lebensfluten"
			PICTURE_URL="$url/1.jpg"
		;;
	esac
}

include_fullscreen_picture()
{
	local indentation="$1"

	shuffle_picture		# set NAME + URL
	PICTURE_ID=$(( ${PICTURE_ID:-1000} + 1 ))

	cat <<EOF
${indentation}{
${indentation}${TAB}"id" : "$PICTURE_ID",
${indentation}${TAB}"institution" : "1",
${indentation}${TAB}"start" : "$( date +%Y-%m-%d )",
${indentation}${TAB}"title" : "$PICTURE_NAME",
${indentation}${TAB}"full_screen_picture" : "$PICTURE_URL"
${indentation}},
EOF
}

while read LINE; do {
#	echo "'$LINE'"
#	continue

	case "$LINE" in
		*"barrierinfo"*)
#			logger -s "LINEin: '$LINE'"

			case "$LINE" in
				'"barrierinfo": ""'*)
					echo "$( tabout )\"barrierinfo\": null"
#					logger -s "LINE-correcter: '\"barrierinfo\": null'"

				case "$LINE" in
					*"}")
						LINE="}"
#						logger -s "LINE+: '$LINE'"
					;;
					*)
						continue
					;;
				esac
			esac
		;;
	esac

	case "$LINE" in
		*'"Termine": {'*)
			LINE='"Termine": ['
		;;
		"}")
			DIRTY1="true"
		;;
		"},")
			[ -n "$DIRTY1" ] && {
				[ -n "$DIRTY2" ] || LINE="],"
				DIRTY2="true"
			}
		;;
		*\"[0-9][0-9][0-9][0-9]\":*|*\"[0-9][0-9][0-9][0-9][0-9]\":*)		# 3 or 4 or 5 digits
			LINE="$( echo "$LINE" | sed -n 's/^\(.*\): {/\1,/p' )"	# cut of last: ": {"

			include_fullscreen_picture "$( tabout )"

			echo "$( tabout ){"
			LINE="\"id\": ${LINE}"
			TABIDX=$(( $TABIDX + 1 ))
		;;
	esac

	case "$LINE" in
		*"}"*)
			TABIDX=$(( $TABIDX - 1 ))
		;;
		*'"endtime":'*)
			case "$LINE" in
				*'"23:59"'*)
					echo "$( tabout )\"noendtime\": true,"
				;;
				*)
					echo "$( tabout )\"noendtime\": false,"
				;;
			esac
		;;
		*'"start":'*)		# "start": "2012-08-28",
			DATE_START="$( echo "$LINE" | cut -d'"' -f4 )"
			echo "$( tabout )\"start_nice\": \"$( nicedate "$DATE_START" )\","
		;;
		*'"end":'*)
			DATE_READY="$( echo "$LINE" | cut -d'"' -f4 )"
			echo "$( tabout )\"end_nice\": \"$( nicedate "$DATE_READY" )\","

			[ "$DATE_START" = "$DATE_READY" ] && {
				echo "$( tabout )\"same_start_and_end\": true,"
			}

			DATE_START=
			DATE_READY=
		;;
	esac

	echo "$( tabout )$LINE"

	case "$LINE" in
		*"{"*)
			TABIDX=$(( $TABIDX + 1 ))
		;;
	esac

} done <"$FILE" >"$FILE_OUT"

sed -i '${s/,$//}' "$FILE_OUT"		# remove last comma in last line

echo "${TAB}}" >>"$FILE_OUT"
echo "}" >>"$FILE_OUT"

rm "$FILE"
