#!/bin/bash

# A refined version of the the technique at:
#	 http://www.linuxjournal.com/content/tech-tip-create-automatically-scrolling-reader

echo Pretty-scrolling all c sources in $1

#I'm sure there is a way to do this without xdottool
#	I think it involves echoing d to the parent's stdout as the watch item

#Set the terminal title for xdotool
echo -ne "\033]0;Scroller2\007"

(watch -n 3 xdotool search --name Scroller2 key d > /dev/null &)
#Gets PID of subshell not watch... unhelpful
#WPID=$!
#echo Scroll command coming from $WPID

for f in $1/*.[cs] 
do 
	echo Pretty-scrolling $f
	source-highlight -fesc -o STDOUT -i $f | less -RMNe
	wait
done

#This is SO WRONG, but it does kill off watch on termination...
WPID=`ps a | grep "watch -n 3 xdotool search --name Scroller2 key d" | grep -v grep | awk '{print $1}'`
echo Killing watch on PID $WPID
kill -9 $WPID

