#!/bin/sh
#
# Script to act as a backend for Shift-PrtSc and Alt-PrtSc window manager
# hotkeys that allows for taking a snapshot of either a window (selected by
# the user), or the root window, and saves it to a file.
#
# parameters
# $1 - mode: screen or window
# $2 - captured file folder
# $3 - silent mode indicator / number of seconds to sleep before capture
#
# rcs:
# 0 - successful
# 1 - incorrect mode parameter
# 2 - file folder is not a directory
# 3 - capture failed
#
# Change log:
#
# 11/01/2007 Jim Kunz     -00 initial release
#

mode=$1
rootfolder=$2
silentmode=$3

okbutton=$"OK"
automsg=$"This window will close automatically in 30 seconds."

# display usage info
function usage()
{
	if [ -z "$silentmode" ]; then
		msg=$"Usage is: `basename $1` {screen|window} capturefolder silent-mode-sleep-seconds." >&2
		title=$"Usage"
		echo -n "$msg $automsg" | fold -s -w 80 | xmessage -button "$okbutton:0" -center -title "$title" -timeout 30 -file -
	fi
}

# check mode parameter
if [ \( "$mode" != "screen" \) -a \( "$mode" != "window" \) ]; then
	usage $0
	exit 1
fi

# check root folder parameter
if [ -z "$rootfolder" ]; then
	usage $0
	exit 1
fi

# make sure location for captured files is a directory
if [ \( ! -d ${rootfolder}/ \) -a \( -e ${rootfolder} \) ]; then
	if [ -z "$silentmode" ]; then
		msg=$"Parameter error. ${rootfolder} is not a directory." >&2
		title=$"Parameter Error"
		echo -n "$msg $automsg" | fold -s -w 80 | xmessage -button "$okbutton:0" -center -title "$title" -timeout 30 -file -
	fi
	exit 2
fi

# create directory for captured files if not there already
if [ ! -d ${rootfolder}/ ]; then
	mkdir ${rootfolder}/
fi

# if silent mode sleep for time indicated
if [ -n "$silentmode" ]; then
	sleep $silentmode
fi

# compose file name
host=`hostname`
date=`date +%Y%m%d`
time=`date +%H%M%S`
seq=`/bin/ls ${rootfolder}/$host-$date-$time-*.xwd 2> /dev/null | wc -l | tr -d '[[:space:]]'`

converttopnm=0
if ! which xwdtopnm 2>&1 | grep -q -i ' no xwdtopnm '; then
	converttopnm=1
	outfile="${rootfolder}/$host-$date-$time-$seq-$mode.png"
else	
	outfile="${rootfolder}/$host-$date-$time-$seq-$mode.xwd"
fi


# if screen just capture whole screen to the outfile
if [ "$mode" = "screen" ]; then
        if [ "$converttopnm" == "1" ]; then
			if ! xwd -root | xwdtopnm | pnmtopng > $outfile; then
				if [ -z "$silentmode" ]; then
					msg=$"The screen could not be captured to $outfile. Try again or try capturing a specific window."
					title=$"Capture Screen Failed"
					echo -n "$msg $automsg" | fold -s -w 80 | xmessage -button "$okbutton:0" -center -title "$title" -timeout 30 -file -
				fi
				exit 3
			fi
        else
			if ! xwd -root -out $outfile; then
				if [ -z "$silentmode" ]; then
					msg=$"The screen could not be captured to $outfile. Try again or try capturing a specific window."
					title=$"Capture Screen Failed"
					echo -n "$msg $automsg" | fold -s -w 80 | xmessage -button "$okbutton:0" -center -title "$title" -timeout 30 -file -
				fi
				exit 3
			fi
		fi
		title=$"Screen Captured"
	    msg=$"The entire screen has been captured in file $outfile."
else
# if window, explain to the user how to get the window and use the crosshairs
		if [ -z "$silentmode" ]; then
			msg=$"After this window is closed, the cursor will change to crosshairs. Use the Alt+Tab keys to bring the window you want to capture to the forefront, then click anywhere on that window to capture it."
			title=$"Capture Window Instructions"
			echo -n "$msg $automsg" | fold -s -w 80 | xmessage -button "$okbutton:0" -center -title "$title" -timeout 30 -file -
		fi

        # A little explanation first.
        # I could just run xwd | convert xwd:- $outfile, but I wanted some
        # visual feedback using osd_cat (On Screen Display). I wanted to
        # get the title of the window, and the id as well, as I can use xwd
        # by giving it a window id.
        # The xwininfo lets me click on a window (shows a crosshair cursor)
        # Using the output, I harvest the parts of the window that have
        # the id and window title, and save them to variables.
        # If you do not have osd_cat, the program will still work, you just
        # won't get any visual feedback after its completed.

        tmp=`xwininfo | sed -ne 's/^xwininfo: .* \(0x[0-9a-f]*\) "\(.*\)"/\1:\2/p'`
        windowid=`echo $tmp | cut -d: -f1`
        windowtitle=`echo $tmp | cut -d: -f2-` #Title may include :
        if [ "$converttopnm" == "1" ]; then
			if ! xwd -id $windowid | xwdtopnm | pnmtopng > $outfile; then
				if [ -z "$silentmode" ]; then
					msg=$"The window could not be captured to $outfile. Try again or try capturing the whole screen."
					title=$"Capture Window Failed"
					echo -n "$msg $automsg" | fold -s -w 80 | xmessage -button "$okbutton:0" -center -title "$title" -timeout 30 -file -
				fi
				exit 3
	        fi
        else
			if ! xwd -id $windowid -out $outfile; then
				if [ -z "$silentmode" ]; then
					msg=$"The window could not be captured to $outfile. Try again or try capturing the whole screen."
					title=$"Capture Window Failed"
					echo -n "$msg $automsg" | fold -s -w 80 | xmessage -button "$okbutton:0" -center -title "$title" -timeout 30 -file -
				fi
				exit 3
	        fi
        fi
		title=$"Window Captured"
        msg=$"The window with title '$windowtitle' has been captured in file $outfile."
fi

# display info on the file the screen was captured to
if [ -z "$silentmode" ]; then
	cleanup=$"To delete capture files, use the Cleanup Temporary Files task and select Print Screen Files."
	echo -n "$msg $cleanup $automsg" | fold -s -w 80 | xmessage -button "$okbutton:0" -center -title "$title" -timeout 30 -file -
fi
exit 0