#! /bin/sh
#
# Copyright (C) 2005 Tobias Toedter <t.toedter@gmx.net>
#   Portability changes by Ralf Hoffmann <ralf@boomerangsworld.de>
# Copyright (C) 2014 Ralf Hoffmann <ralf@boomerangsworld.de>
#   merge with displaywrapper_worker
#
# This file is part of worker.
# 
# Worker is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2 of the License, or
# (at your option) any later version.
# 
# Worker is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
# 
# You should have received a copy of the GNU General Public License
# along with Worker; if not, write to the Free Software
# Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA

# we require exactly seven arguments to the script
if [ $# != 7 ]; then
	echo "This is the $0 program. It belongs to the worker filemanager."
	echo "  This is not a standalone program. It will normally be called from"
	echo "  worker to display pictures in it."
	echo "  The arguments are:"
	echo "  1. X-coordinate"
	echo "  2. Y-coordinate"
	echo "  3. Width of the area"
	echo "  4. Height of the area"
	echo "  5. Hex-number of the window"
	echo "  6. The filename to show"
	echo "  7. The background color in X11-format"
	echo
	exit 
fi

xli_mode=0
display_mode=0

if [ $(basename "$0") = "xliwrapper_worker" ]; then
    if $(which xli >/dev/null 2>/dev/null); then
        xli_mode=1
    elif $(which display >/dev/null 2>/dev/null); then
        display_mode=1
    fi
elif [ $(basename "$0") = "displaywrapper_worker" ]; then
    if $(which display >/dev/null 2>/dev/null); then
        display_mode=1
    elif $(which xli >/dev/null 2>/dev/null); then
        xli_mode=1
    fi
fi

if [ "$xli_mode" -eq 1 ]; then
    pic_info=$(xli -identify "$6")
    width=$(echo $pic_info | sed -ne "s/.* is a \([0-9]*\)x\([0-9]*\).*/\1/p")
    height=$(echo $pic_info | sed -ne "s/.* is a \([0-9]*\)x\([0-9]*\).*/\2/p")

    if [ -z "$width" -o -z "$height" ]; then
	    exit
    fi

    if [ "$width" -gt 0 -a "$height" -gt 0 ]; then
        x_zoom_factor=$(expr \( $3 \* 100 \) / $width)
        y_zoom_factor=$(expr \( $4 \* 100 \) / $height)
        if [ $x_zoom_factor -gt $y_zoom_factor ]; then
        	zoom_factor=$y_zoom_factor
	    else
        	zoom_factor=$x_zoom_factor
	    fi
	    if [ $zoom_factor -lt 1 ]; then
		    zoom_factor=1
	    fi
	    result_x=$(expr \( $1 \* 100 \) / $zoom_factor )
	    result_y=$(expr \( $2 \* 100 \) / $zoom_factor )
	    result_width=$(expr \( $3 \* 100 \) / $zoom_factor + 1 )
	    result_height=$(expr \( $4 \* 100 \) / $zoom_factor + 1 )

	    xli -quiet -border $7 -clip $result_x,$result_y,$result_width,$result_height -zoom $zoom_factor -windowid 0x$5 "$6"
    fi
elif [ "$display_mode" -eq 1 ]; then
    d=$(which display 2>/dev/null)
    if test -n "$d"; then
        e=$(identify -ping "$6" 2>/dev/null)
        if test -n "$e"; then
            convert "$6" png:- | display -geometry $3x$4+$1+$2 -foreground "$7" -window 0x$5 - >/dev/null 2>/dev/null
        fi
    fi
fi
