#!/bin/sh

datadir=/etc/alterator/logs
awkdesktopfile=/usr/share/alterator-menu/desktop.awk
cachefile=/var/cache/alterator/alterator-logs.txt
po_domain="alterator-logs"

. alterator-sh-functions

log_basename()
{
    local n=${1##*/}
    echo "${n%%.desktop}"
}

desktop_awk()
{
    [ -f "$1" ] &&
	awk -f "$awkdesktopfile" \
    	    -v lang="$in_language" \
	    "$1"
}

cat_log()
{
    find "$(dirname $1)" -type f -name "$(basename $1)*"|
	while read f; do
	    t="$(file -b "$f")"
	    if [ -z "${t##bzip2 compressed data*}" ]; then
		bzcat "$f"
	    elif [ -z "${t##gzip compressed data*}" ]; then
		zcat "$f"
	    else
		cat "$f"
	    fi
	done
}

on_message()
{
	case "$in_action" in
		write)
		    write_nop
		    ;;
		read)
    		    echo '('
		    local name="$in_name"
		    local desktopfile="$datadir/$name.desktop"

		    if [ ! -n "$name" ] ;then
			desktopfile="$(find "$datadir" -name '*.desktop'|head -n1)"
			name="$(log_basename "$desktopfile")"
		    fi

		    #dump + total size
		    local path="$(desktop_awk "$desktopfile"|cut -f2)"
		    local total="$(cat_log "$path"|tee "$cachefile"|wc -l)"

		    #main data: page size and start shift
		    local start="$in_start"
		    local size="$in_size"
		    local stop=

		    [ -n "$size" ] || size=20
		    [ -n "$start" ] || start=1

		    #change size: back to first page
		    if [ -n "$in_new_size" -a "$in_new_size" != "$in_size" ];then
			size="$in_new_size"
			start=1
		    fi

		    #next/back/first/last
		    if [ -n "$in_next" ]; then
			start="$(($start + $size))"
		    fi

		    if [ -n "$in_back" ]; then
			start="$(($start - $size))"
			[ "$start" -gt 0 ] || start=1
		    fi

		    if [ -n "$in_first" ]; then
			start=1
		    fi

		    if [ -n "$in_last" ]; then
			start="$(( ( $total / $size ) * $size ))"
			[ "$start" -gt 0 ] || start=1
			stop="$total"
		    else
			stop="$(($start + $size))"
		    fi

		    printf 'text "%s"' "$(sed -n -e "$start,$stop p" -e "$stop q" "$cachefile"|string_quote)"
		    [ "$stop" -lt "$total" ] || stop="$total"

		    local format="`_ "Lines %s-%s of %s"`"
		    printf "range \"$format\"\n" "$start" "$stop" "$total"

		    #save information for next page switch
		    if [ "$stop" -eq "$total" ] ;then
			start="$(($start - $size))"
			[ "$start" -gt 0 ] || start=1
		    fi
		    printf 'name  "%s"\n' "$name"
		    printf 'start "%s"\n' "$start"
		    printf 'size "%s"\n' "$size"
		    printf 'new_size "%s"\n' "$size"

		    echo ')'
		    ;;
		list)
		    echo '('
		    case "$in__objects" in
			avail_size)
		    	    write_enum_item "10" "`_ "10 lines"`"
		    	    write_enum_item "20" "`_ "20 lines"`"
		    	    write_enum_item "50" "`_ "50 lines"`"
		    	    write_enum_item "100" "`_ "100 lines"`"
		    	    ;;
			avail_log)
			    find "$datadir" -name '*.desktop'|
				while read n;do
				    printf '%s\t' "$(log_basename "$n")"
				    desktop_awk "$n"
				done|
				while read desktopfile weight path help icon description;do
				    [ -f "$path" ] || continue
				    write_enum_item "$desktopfile" "$description"
				done
			    ;;
		    esac
		    echo ')'
		    ;;
		*)
		    echo '#f'
		    ;;
	esac
}

message_loop
