#!/bin/bash -e

usage()
{
cat <<EOF
phoonsX - run pngphoon for every X11 display available
usage: $0 (name of directory to generate files in, default is current)
file will have the format of phoon_<displayname>:<display#>.<screen#>.png
(displayname will be most probably an empty string, if not it's the hostname)
EOF
}

if [ -z "${DISPLAY}" ]; then
   usage
   echo 'This script needs an active X11 session.'
   exit 1
fi

targetdir='.'
if [ -n "${1}" ]; then
   targetdir="${1}"
fi

if [ ! -d "${targetdir}" -o ! -w "${targetdir}" ]; then
   usage
   exit 1
fi
cd "${targetdir}"


xdpyinfo | grep -e '^name of display:' -e '^screen #' -e '^  dimensions:' |
while read line; do
case "${line}" in
name\ of\ display:*) display="$(echo ${line}|sed 's/[^:]*: *\(.*\)/\1/')";;
screen\ \#*:*) screen="$(echo ${line}|sed 's/.*#\(.*\):.*/\1/')" ;;
dimensions:*) dimensions="$(echo ${line}|sed 's/[^:]*: *\([0-9]*\)x\([0-9]*\).*/-w \1 -h \2/')"
   pngphoon ${dimensions} -f phoon_${display}.${screen}.png
;;
esac
done
