#! /bin/sh

# name:      xdumpwindow  --  non-interactively dump an X11 window into a file
# author:    Ch. L. Spiel  <cspiel@hammersmith-consulting.com>
# last rev.: Tue Sep 18 07:54:46 UTC 2001
# Bash ver.: 2.04.0


myname=$(basename $0)

function usage
{
    cat <<EOF
usage:
        $myname [convert_options] x11_window_name output_filename

The name of an X11 window can be inquired with xwininfo(1).
See convert(1) for applicable convert_options.

EOF

    exit 0
}


#
# main
#


if [ $# -lt 2 ]; then
    echo "$myname: expecting two ore more arguments." 1>&2
    exit 1
fi

tmpfile=$(mktemp /tmp/$myname.XXXXXX)
if [ $? -ne 0 ]; then
    echo "$myname: Cannot create temp file." 1>&2
    exit 1
fi

num_opts=$(expr $# - 2)
if [ $num_opts -gt 0 ]; then
    for opt in $(seq $num_opts); do
	options="$options $1"
	shift
    done
fi

window_name="$1"
output_file="$2"

xwd -silent -screen -out $tmpfile -name "$window_name"
convert $options xwd:$tmpfile "$output_file"
/bin/rm $tmpfile

exit 0
